200 likes | 350 Views
Inheritance. Chapter 9. Inheritance - Basics. Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits all properties of its superclass and can define its own unique properties.
E N D
Inheritance Chapter 9
Inheritance - Basics • Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class • Subclass inherits all properties of its superclass and can define its own unique properties. • Subclass can redefine inherited methods. Generalization: process of forming a superclass Specialization: forming a subclass
Cont’d • Classes that are derived from others inherit all the accessible members of the base class. • E.g. Base class includes a member A and we derive it to another class with another member called B, the derived class will contain both members A and B. • In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format: class derived_class_name: public base_class_name{ /*...*/ };
Reusability • Once a class has been written, created and debugged, it can be distributed to other programs to reuse in their own programs called ”reusability” • E.g. Library of functions, third party softwares • Inheritance provides important extention to the idea of reusability • Reduces software development time
Example class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; }
What is inherited from the base class? • A derived class inherits every member of a base class except: • its constructor and its destructor • its operator=() members • its friends • The constructors and destructors of the base class are not inherited themselves • Default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed
Cont’d • An overloaded constructor can be called when a new derived object is created as following derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
Example class mother { public: mother () { cout << "mother: no parameters\n"; } mother (int a) { cout << "mother: int parameter\n"; } }; class daughter : public mother { public: daughter (int a) { cout << "daughter: int parameter\n\n"; } }; class son : public mother { public: son (int a) : mother (a) { cout << "son: int parameter\n\n"; } }; int main () { daughter cynthia (0); son daniel(0); return0; } OUTPUT mother: no parameters daughter: int parameter mother: int parameter son: int parameter
Public and Private Inheritance • The keyword public specifies that objects of the derived class are able to access public member functions of the base class • The Keyword private specifies that objects of the derived class cannot access publicmember functions of the base class
Multiple Inheritance • A class inherits members from more than one class • This is done by simply separating the different base classes with commas in the derived class declaration classCRectangle: publicCPolygon, publicCOutput; classCTriangle: publicCPolygon, publicCOutput;
Example class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class COutput { public: void output (int i); }; void COutput::output (int i) { cout << i << endl; } class CRectangle: public CPolygon, public COutput { public: int area () { return (width * height); } }; class CTriangle: public CPolygon, public COutput { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); rect.output (rect.area()); trgl.output (trgl.area()); return 0; } OUTPUT 20 10
Ambiguity in Multiple Inheritance • Two base classes have functions with the same name, how do object of derived class can access the correct base class function Class A{ public void show(){….}}; Class B{ public void show(){….}}; Class C: public A, public B{}; void main(){ C objC; objC.show(); // ambiguous—will not compile objC.A::show();// resolved by :: scope resolution operator objC.B::show(); }
Cont’d • If you derive a class from two classes that are each derived from the same class (Diamond-shape Inheritance) Class A{ public void func();}; Class B:public A{ }; Class C: public A{} Class D: public B, public C{ }; Void main(){ D objD; objD.func(); // ambiguous .. Will not compile }