1 / 20

Inheritance

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.

kiefer
Télécharger la présentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Chapter 9

  2. 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

  3. 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{ /*...*/ };

  4. 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

  5. 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; }

  6. Access Specifier

  7. 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

  8. 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) {...}

  9. 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

  10. 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

  11. 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;

  12. 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

  13. 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(); }

  14. 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 }

More Related