Inheritance
Created Wednesday 04 May 2016
Concepts learned in CS170:
- Encapsulation
- Hide the inner workings of a class
- Provide an interface to use the class
- An example in C would be "header files". (In them are declarations.)
- double foo(int);
- int goes in, double comes out. We do not care about the implementation.
- Structs in C
1 //Let's say we have a struct: 2 3 struct Person { 4 char * name; 5 double height, weight; 6 int id; 7 } 8 9 //If we didn't have that, this is what we would need: 10 11 void AddPerson (char *, double, double, int); 12 13 //Instead, now we can just do the following: 14 15 void AddPerson (Person const *); 16 17 /* HOWEVER, BECAUSE STRUCTS DO NOT HAVE ACCESS MODIFIERS, 18 THEY ARE NOT ENCAPSULATED.... THEREFORE WE NEED "PUBLIC, PRIVATE, PROTECTED".*/
Inheritance:
- Why do we need it?
- Code Reuse (?)
- But not really... We already have functions that can allow us to reuse code.
- Is a subconcept of modular design
- Treats objects as basic building blocks
- Compose objects to form bigger objects
- Define some kind of relationship between objects.
- Two kinds of relationships: "has-a" and "is-a".
- Polymorphism -> "has-a"
- Inheritance -> "is-a"
- Code Reuse (?)
1 /* 2 The following is an example of a "HAS-A" relationship: 3 4 Suppose we are designing a Car object. 5 */ 6 7 class Chassis; 8 class Engine; 9 class Wheel; 10 class SteeringWheel; 11 12 class Car 13 { 14 Engine engine; 15 Chassis chassis; 16 Wheel wheel[4]; 17 SteeringWheel sw; 18 } 19 20 class Motorboat 21 { 22 Engine engine; 23 SteeringWheel sw; 24 } 25 26 /* 27 HOWEVER, the steering wheel might need to be different for the motorboat and the car 28 Being able to have the SAME INTERFACE but DIFFERENT BEHAVIOUR is polymorphism. Do not confuse the instance 29 of an object with the type of the object... 30 */ 31 32 class SteeringWheel 33 { 34 public: 35 void steer_left(); 36 void steer_right(); 37 } 38 39 /* 40 Here we have some examples of polymorphism in action: 41 */ 42 43 class BombBird 44 { 45 public: 46 void fly() {std::cout<<"Bomb fly\n";} 47 }; 48 49 class FireBird 50 { 51 public: 52 void fly() { std::cout<<"Fire fly\n";} 53 }; 54 55 //The following is some really shit code: 56 57 void MakeAllBirdsFly 58 (std::vector<BombBird> & bombbirds, 59 std::vector<FireBird>& firebirds, 60 std::vector<DefaultBird>& defaultbirds) 61 { 62 //blah blah 63 }
- Inheritance
- Describes a "is-a" relationship between two classes.
- The class you inherit from is the Base class
- The inheriting class is the Derived class
- Syntax:
1 class B {...}; 2 class D1: public B {..}; //Everyone knows that you are the son of your father 3 class D2: protected B{...}; //Only people who inherit from you know that you are the son of your father 4 class D3: private B{..}; //Only you know that you are the son of your father, not even your 5 //father knows you're his son 6 //Yes, I am aware that this is a screwed up example. 7 8 class B { public: void foo(); } 9 class D: protected B {} 10 11 D d; 12 d.foo(); //This code is outside B and D. Compile failure. D is not publicly son of B. 13 void D::goo() { D d; d.foo(); } //Ok. 14 15 class B {public: void foo(); } 16 class D: private B {} 17 void D::goo() { this->foo(); } //Ok. 18 19 D d; 20 d.foo(); //This code is outside B and D. Compile failure. D is not publicly son of B 21 void D::goo() {D d; d.foo(); } //Not OK. 22 void D::goo() { this->foo(); } //OK.
- When D inherits from B, we say that D "is a" B.
Backlinks: index:CS225 Notes