Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon...

Preview:

DESCRIPTION

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

Citation preview

InheritanceChapter 9

Inheritance - BasicsInheritance 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’dClasses 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

{ /*...*/ };

ReusabilityOnce 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

Exampleclass 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; }

Access SpecifierAccess public protected privatemembers of the same class yes yes yes

members of derived classes

yes yes no

not members yes no no

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=() membersits 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’dAn overloaded constructor can be called

when a new derived object is created as following

derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

Exampleclass 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); return 0; }

OUTPUTmother: no parameters daughter: int parameter

mother: int parameter son: int parameter

Public and Private InheritanceThe 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 public member functions of the base class

Multiple InheritanceA class inherits members from more than one

classThis is done by simply separating the

different base classes with commas in the derived class declaration

class CRectangle: public CPolygon, public COutput; class CTriangle: public CPolygon, public COutput;

Exampleclass 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

2010

Ambiguity in Multiple InheritanceTwo 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 compileobjC.A::show();// resolved by :: scope resolution operatorobjC.B::show();}

Cont’dIf 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}

Recommended