INHERITANCE
description
Transcript of INHERITANCE
![Page 1: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/1.jpg)
![Page 2: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/2.jpg)
A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions can be included as well. (INHERITANCE)
The existing class is referred to as the Base Class/ Parent Class/ Super Class.
The new class the is being defined by inheriting from the existing class is referred to as the Derived Class/ Child Class/ Sub Class
Syntax:Class <name of derived class>:<access specifier> <name of base class>{……
// definition of class ….
};
![Page 3: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/3.jpg)
Eg: Class B : Public A{// new features of class B};
Diagrammatic representation of Inheritance:
A
B
![Page 4: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/4.jpg)
class A{ int x;
public: void setx(const int=0);int getx() const; };
class B: public A{ int y;public: void sety(const int =0);int gety() const; };
void main(){ cout<<sizeof(A)<<endl<<sizeof(B);B B1;B1. setx(1);B1.sety(3);cout<<B1.getx();cout<<endl<<B1.gety(); }
![Page 5: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/5.jpg)
Effects of Inheritance:Inheritance affects the size and
behaviour of the derived class in 2 ways:
1. An object of the derived class will contain:
Data Members of Derived Class+
Data Members of Base ClassThus, An object of the Derived Class is generally
larger than an object of the Base Class.
Exception???
![Page 6: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/6.jpg)
2. We can call a member function of the Derived Class using an object of the Derived Class…. The same object can be used to call the public members of the Base Class also.
Does not happen in some circumstances!!!!____________________________________________________
Inheritance implements a is-a relationship.
The process of Inheritance is also called SPECIALIZATION. Why???
Main benefit of Inheritance???
![Page 7: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/7.jpg)
Container Class: A class that contains an
object of another class or a pointer to a data structure that contains a set of objects of another class.
Containership implements a has-a relationship.
![Page 8: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/8.jpg)
Base and Derived Class Objects: An object of the Derived Class is not at all
related to another simultaneously existing object of the base class.
x
x
y
A1
B1
101
201
![Page 9: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/9.jpg)
Accessing Members of the Base Class in the Derived Class
Only Public members of the Base Class can be accessed in the functions of Derived Class.
Eg: if definition is:void B::sety(){x=y;}
ERROR!!! Private members of the Base Class cannot be accessed.
Eg: ‘A:: setx()’ and ‘A::getx()’ can be accessed in the member functions of the Derived Class . Why???
![Page 10: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/10.jpg)
Private members of the Base Class remain Private with respect to the member functions of the Derived Class.
Eg: void B::sety(const int q){
y=q;setx(y)
}
Thus, Data Security is implemented in C++.
Inheritance is used to add facilities to an existing class without reprogramming or recompiling it.
![Page 11: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/11.jpg)
class B;class A
{ friend class B;int x; };
class B{ void fB (A *p)
{ p->x=0; } };class C : public B
{ void fC(A *p) { p->x=0; } };
FRIENDSHIP IS NOT INHERITED!
![Page 12: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/12.jpg)
BASE CLASS AND DERIVED CLASS POINTERS
• A Base Class Pointer can point to an object of Derived Class.
• A Derived Class Pointer cannot point to an object of the Base Class.
• A Base Class Pointer can safely point to an object of a Derived Class without the need foe typecasting.(Exceptions will be considered later)
• A Derived Class Pointer can be made to point at an object of the Base Class only forcibly by typecasting. (Can result in run-time errors!)
![Page 13: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/13.jpg)
class A{ public: int x; };class B : public A{ public : int y; };void main(){ A * Aptr;
B B1;Aptr=&B1;// Base class ptr points to derived class object
Aptr->x=10;//Access Base class member thru base class ptr
Aptr->y=20;//Error! Y not found in class A}
![Page 14: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/14.jpg)
• Aptr is of type ‘A *’. It is supposed to point at objects of base class A. Therefore, It cannot access ‘y’. ( There is no member of name ‘y’ in class A).
• The fact that Aptr points at an object of class B is of no significance.
• Although ‘Aptr ‘ points at ‘B1’(occupies 4 bytes), it is able to access only the value contained in the first 2 bytes.
• Thus, Aptr cannot access area in the memory that hasn’t been allocated.
A Base class pointer can safely point to an object of the derived class.
![Page 15: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/15.jpg)
x
y
AptrB1
201
201
Base class pointer points to an object of the derived class
![Page 16: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/16.jpg)
void main(){
A A1;B * Bptr;Bptr=&A1; // Error!! Cannot convert from B* to A*Bptr->x=10;//ok. Derived class pointer
//accesses base class memberBptr->y=20; //ok. Derived class pointer
//accesses derived class member}
![Page 17: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/17.jpg)
• Bptr is of type ‘B *’. It is supposed to point to objects of derived class B. It can access ‘y’ also.
• Bptr is pointing to A1(occupies 2 bytes). There is a member of name ‘y’ in class B.
• Thus, Bptr is able to access memory that has not been allocated.
A Derived class Pointer cannot safely point to an object of the base class.
![Page 18: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/18.jpg)
x
y
Bptr
A1201
201
Derived class pointer points to an object of the Base class
Y would be here in case of derived class objects
![Page 19: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/19.jpg)
• A derived class pointer can be made to point at an object of base class by explicit typecasting.
void main(){
A A1;B * Bptr; // derived class pointerBptr=(B *)&A1; // Explicit typecasting to make the //derived class pointer point at
//base class object}
Explicit Address manipulation should be avoided and is dangerous!
![Page 20: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/20.jpg)
Consider the following class definitions:class A{ int x;
public: void setx(const int =0);// rest of class A };
class B: public A{ int y;
public: void sety(const int=0);// rest of class B};
![Page 21: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/21.jpg)
void main(){
A * Aptr;B B1;Aptr=&B1;// ok Base class ptr points to derived class objAptr->setx(10); //ok access base class member thru base //class ptrAptr->sety(20); //Error! Sety() is not a member of class A
}
![Page 22: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/22.jpg)
void main(){
A A1;B * Bptr;Bptr=&A1;// Error! Cannot convert A* to B*Bptr->setx(10);//ok derived class pointer accesses base //class memberBptr->sety(20);// ok derived class pointer accesses //derived class member
}
![Page 23: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/23.jpg)
Defining a member function in the derived class with the name and signature matching those of the base class function.
Results in two functions with the same name and signature. ( one in base class + one in derived class)
![Page 24: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/24.jpg)
class A{ public: void show()
{ cout<<“ show function in Class A called”; } };
class B:public A{ public: void show() // overriding A::show()
{ cout<<“ show function in Class B called”; } };
![Page 25: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/25.jpg)
void main(){B B1;B1.show();// B::show() is calledA A1;A1.show();// A::show() is calledB1.A::show();// calling the overridden function
forcibly //with respect to object of the derived class.
}
![Page 26: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/26.jpg)
Whenever a function is called with respect to an object, the compiler first searches for the function prototype in the same class.
Only if the search fails, the compiler goes up the class hierarchy to look for the function prototype.
i.e. The show() function of class A is virtually hidden by the show function of class B (for the first call of show() in prev eg)
![Page 27: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/27.jpg)
Function Overriding is a form of overloading. Justify!
Though the signatures of the overriding function and overridden function are the same, The This pointer brings in the apparent difference.
Actual prototype of A::show() becomes void show(A * const);
Actual prototype of B::show() becomes void show(B * const);
![Page 28: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/28.jpg)
BASE CLASS INITIALIZATION When an object of derived class is created,
the compiler implicitly embeds a call to the base class constructor and then the derived class constructor with respect to the object.
Suppose A is the base class and B is its derived class:
B B1; is converted intoB B1;B1.A();B1.B();
Destructors are called in reverse order.
![Page 29: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/29.jpg)
class A{ int x;public: A (const int=0);
void setx(const int =0);int getx() const; };
A::A(const int p){ x=p;}void A::setx(const int p){ x=p; }int A::getx()const{ return x;}
class B:public A{ int y;public: B (const int=0);
void sety(const int =0);
int gety() const; };B::B(const int q){ y=q;}void B::sety(const int q){ y=q; }int B::gety()const{ return y;}
void main(){ B B1(20);
cout<<B1.getx()<<endl<<B1.gety()<< endl;}
![Page 30: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/30.jpg)
B B1(20); gets converted intoB B1; // Memory allocated for objectB1. A(); //Base class constructor calledB1.B(20); //derived class constructor
called
How should the base class constructor be modified to make proper base class initialization?
Class B : public A{ public: B(const int=0,const int =0); …….};B::B(const int p, const int q):A(p){ y=q; }
![Page 31: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/31.jpg)
void main(){ B B1(10,20);
cout<<B1.getx()<< endl<< B1.gety()<< endl;
}
B B1 (10,20) gets converted to B B1;B1. A(10);B1. B(20);
![Page 32: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/32.jpg)
It is not necessary that the first parameter should be passed to the Base class constructor.
Any of the parameter in the list can be passed to the base class constructor.
![Page 33: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/33.jpg)
The PROTECTED access specifier
‘Protected ‘ members are inaccessible to nonmember functions.
They are accessible to the member functions of their own class and to member functions of the derived classes.
![Page 34: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/34.jpg)
class A{ private: int x;
protected: int y;public: int z;};
class B: public A{ public: void xyz();};
void B::xyz() // member function of derived class{ x=1; // Error: Private member of base classy=2; //Ok: Protected member of base classz=3; // Ok: Public member of base class }
![Page 35: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/35.jpg)
void main(){
A * Aptr;Aptr->x=10;Aptr->y=20; Aptr->z=30;
}
// Non member function
// Error: Private member// Error: Protected member//Ok: Public member
![Page 36: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/36.jpg)
Deriving by Different Access SpecifiersDeriving by the Public Access Specifier
- Deriving by the public access specifier retains the access level of the Base class members.Private Members of the Base class:
* Members of the derived class cannot access them.
* Member functions of the subsequently derived classes cannot access them.
* Non-member functions cannot access them.
![Page 37: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/37.jpg)
Deriving by the Public Access Specifier….
Protected Members of the Base class: * Members of the derived class can
access them. * Member functions of the
subsequently derived classes can also access them.
* Non-member functions cannot access them.
![Page 38: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/38.jpg)
Deriving by the Public Access Specifier….
Public Members of the Base class: * Members of the derived class can
access them. * Member functions of the
subsequently derived classes can access them.
* Non-member functions can access them.
![Page 39: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/39.jpg)
class A { private: int x;protected: int y;public : int z; };
class B:public A{ //B is public derived class of Apublic: void f1()
{ x=1; //ERROR! Private member remains private
y=2; //ok. Protected member remains protected
z=2; } }; //ok. Public member remains public
class c:public B{public: void f2()
{ x=1; //ERROR! Private member remains private
y=2; //ok. Protected member remains protected
z=3;}}; //ok. Public member remains public
![Page 40: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/40.jpg)
void xyz(B *Bptr) // non-member function{
Bptr->x=10; //Error! Private member remains private
Bptr->y=20; //Error! Protected member remains
protectedBptr->z=30; //ok. Public member remains public
}
![Page 41: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/41.jpg)
A base class pointer can point to an object of a derived class that has been derived by using the public access specifier.
void xyz() // non member function{
B B1; //object of public derived classB1.z=100;// ok. z is public member of class B
A *Aptr; //pointer of base classAptr=&B1; //ok. Can make a base class pointer point at //an object of a public derived class.Aptr->z=100; //ok. Can access inherited public
//member of the base class through a //base class pointer.
}
![Page 42: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/42.jpg)
Deriving by Different Access SpecifiersDeriving by the Protected Access
Specifier- Deriving by the protected access specifier reduces the access level of the public Base class members to protected, while the access level of protected and private base class members remain unchanged.
![Page 43: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/43.jpg)
Deriving by the Protected Access Specifier
Private Members of the Base class: * Members of the derived class cannot
access them. * Member functions of the
subsequently derived classes cannot access them.
* Non-member functions cannot access them.
![Page 44: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/44.jpg)
Deriving by the Protected Access Specifier….
Protected Members of the Base class: * Members of the derived class can
access them. * Member functions of the
subsequently derived classes can also access them.
* Non-member functions cannot access them.
![Page 45: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/45.jpg)
Deriving by the Protected Access Specifier….
Public Members of the Base class: * Members of the derived class can
access them. * Member functions of the
subsequently derived classes can access them.
* Non-member functions cannot access them.
![Page 46: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/46.jpg)
class A { private: int x;protected: int y;public : int z; };
class B:protected A{ //B is a protected derived class of Apublic: void f1()
{ x=1; //ERROR! Private member remains private
y=2; //ok. Protected member remains protected
z=2; } }; //ok. Public member remains public
class c:public B{public: void f2()
{ x=1; //ERROR! Private member remains private
y=2; //ok. Protected member remains protected
z=3;}}; //ok. Protected member remains protected
![Page 47: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/47.jpg)
void xyz(B *Bptr) // non-member function{
Bptr->x=10; //Error! Private member remains privateBptr->y=20;//Error! Protected member remains protectedBptr->z=30; //Error! Public member becomes protected
}
![Page 48: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/48.jpg)
A base class pointer cannot point to an object of a derived class that has been derived by using the protected access specifier.
void xyz() // non member function{
B B1; //object of derived classB1.z=100;// ERROR! Cannot access public member of base //class through an object of the protected derived class.
A *Aptr; //pointer of base classAptr=&B1; //ERROR! Cannot make a base class pointer point //at an object of a protected derived class.Aptr->z=100; //ok. Can access inherited public member of the //base class through a base class pointer.
}
![Page 49: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/49.jpg)
Deriving by Different Access SpecifiersDeriving by the Private Access Specifier
- Deriving by the private access specifier reduces the access level of the public and protected Base class members to private, while the access level of private base class members remain unchanged.
![Page 50: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/50.jpg)
Deriving by the Private Access SpecifierPrivate Members of the Base class:
* Members of the derived class cannot access them.
* Member functions of the subsequently derived classes cannot access them.
* Non-member functions cannot access them.
![Page 51: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/51.jpg)
Deriving by the Private Access Specifier….
Protected Members of the Base class: * Members of the derived class cannot
(can) access them. * Member functions of the
subsequently derived classes cannot access them.
* Non-member functions cannot access them.
![Page 52: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/52.jpg)
Deriving by the Private Access Specifier….
Public Members of the Base class: * Members of the derived class cannot
(can) access them. * Member functions of the
subsequently derived classes cannot access them.
* Non-member functions cannot access them.
![Page 53: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/53.jpg)
class A { private: int x;protected: int y;public : int z; };
class B:private A{ //B is a private derived class of Apublic: void f1()
{ x=1; //ERROR! Private member remains private
y=2; //ERROR!Protected member becomes private
z=2; } }; //ERROR!Public member becomes private
class c:public B{public: void f2()
{ x=1; //ERROR! Private member remains private
y=2; //ERROR! Private member remains private
z=3;}}; // ERROR! Private member remains private
![Page 54: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/54.jpg)
void xyz(B *Bptr) // non-member function{
Bptr->x=10; //Error! Private member remains privateBptr->y=20; //Error! Protected member becomes privateBptr->z=30; //Error! Public member becomes private
}
![Page 55: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/55.jpg)
A base class pointer cannot point to an object of a derived class that has been derived by using the private access specifier.
void xyz() // non member function{
B B1; //object of derived classB1.z=100;// ERROR! Cannot access public member of base //class through an object of the private derived class.
A *Aptr; //pointer of base classAptr=&B1; //ERROR! Cannot make a base class pointer point //at an object of a private derived class.Aptr->z=100; //ok. Can access inherited public
//member of the base class through //a base class pointer.}
![Page 56: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/56.jpg)
The following two declarations are equivalent:
Class B:private A{….};
Class B:A{….};
![Page 57: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/57.jpg)
Multiple Inheritance Multi level Inheritance Hierarchical Inheritance Hybrid Inheritance
![Page 58: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/58.jpg)
A class derives from more than one base class.
Class C derives from Class A and Class B
A B
C
![Page 59: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/59.jpg)
The general syntax for multiple inheritance :class <name of derived class>:
<access specifier> <name of first base class>,<access specifier> <name of second base class>,<access specifier> <name of third base class>…{….
};
![Page 60: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/60.jpg)
class A{ int x;public: void setx(const int=0);void getx() const; };class B{ int y;public: void sety(const int=0);void gety() const;};class C: public A,public B{ int z;public: void setz(const int=0);int getz() const; };
![Page 61: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/61.jpg)
void main(){ C c1;cout<<sizeof(c1)<<endl;c1.setx(10);c1.sety(20);c1.setz(30);cout<<c1.getx()<<endl<<c1.gety()<<endl<
<c1.getz();}
![Page 62: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/62.jpg)
Output:12102030
![Page 63: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/63.jpg)
An object of a class defined by multiple inheritance contains not only the data members defined in the derived class, but also the data members defined in all the base classes.
Size of an object: sum of sizes of all the data members of all the base classes + sum of the sizes of data members of all the derived classes.
Thus, we can call the member function of the base class using an object of the derived class as well.
![Page 64: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/64.jpg)
Ambiguities in Multiple Inheritance: Identical members in more than one
base class.class A{ public: void show(){ cout<<“ show of class A called”; } };class B{ public: void show(){ cout<<“ show of class B called”; } };class C: public A, public B {};void main(){ C C1;C1.show();}
![Page 65: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/65.jpg)
Ambiguity can be resolved in 2 ways:
Use the scope resolution operator to specify the class to which the member function belongs.
Override the multiple inherited base class member
![Page 66: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/66.jpg)
Diamond shaped inheritance:
B C
D
A
![Page 67: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/67.jpg)
class A { public: void show(); };class B: public A {};class C: public A{};class D:public B, public C {};void main(){D D1;D1.show();}
Problems???? Solutions????
![Page 68: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/68.jpg)
Occurs when a class inherits from a derived class.
It can be extended to any level
B
C
A
![Page 69: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/69.jpg)
class A{ public: void FA()
{ cout<<“FA called”;}};class B: public A{ public: void FB()
{ cout<<“FB called”;}};class C: public B{ public: void FC()
{ cout<<“FC called”;}};Void main(){ C C1;C1.FA();C1.FB();C1.FC(); }
![Page 70: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/70.jpg)
A single class serves as a base class for more than one derived class.
This gives the best illustration of the virtues of code reusability.
The need to duplicate common features in more than one class is eliminated.
B C
A
![Page 71: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/71.jpg)
class A{public:void FA()
{ cout<<“FA called”;}};class B:public A{public:void FB()
{ cout<<“FB called”;}};class C:public A{public:void FC()
{ cout<<“FC called”;}};
void main(){ B B1;C C1;B1.FA();B1. FB();C1. FA();C1.FC();}
![Page 72: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/72.jpg)
A mixture of all the other kinds of inheritances.
B C
A D
![Page 73: INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022081603/56815156550346895dbf7a1c/html5/thumbnails/73.jpg)
ORDER OF INVOCATION OF CONSTRUCTORS AND DESTRUCTORS
Constructors are invoked in the following order:1. Virtual base class constructors in the order of inheritance.2. Non-virtual base class constructors in the order of inheritance.3. Member objects constructor in the order of declaration.4. Derived class constructor
Destructors are invoked in the reverse order.