chapter 9. inheritance - basics inheritance is a mechanism that allows you to base a new class upon...
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 { /*...*/ };TRANSCRIPT
![Page 1: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/1.jpg)
InheritanceChapter 9
![Page 2: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/3.jpg)
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
{ /*...*/ };
![Page 4: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/4.jpg)
![Page 5: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/6.jpg)
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; }
![Page 7: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/7.jpg)
Access SpecifierAccess public protected privatemembers of the same class yes yes yes
members of derived classes
yes yes no
not members yes no no
![Page 8: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/8.jpg)
![Page 9: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/9.jpg)
![Page 10: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/10.jpg)
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
![Page 11: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/11.jpg)
Cont’dAn overloaded constructor can be called
when a new derived object is created as following
derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
![Page 12: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/14.jpg)
![Page 15: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/15.jpg)
![Page 16: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/16.jpg)
![Page 17: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/17.jpg)
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;
![Page 18: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/19.jpg)
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();}
![Page 20: 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](https://reader030.vdocuments.net/reader030/viewer/2022012911/5a4d1af57f8b9ab059980bce/html5/thumbnails/20.jpg)
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}