2. Outline
Why we need Inheritance?
What is Inheritance?
Access specifier
Access mode or visibility mode
Types of inheritance
Execution order of constructor and destructor
Virtual base class
2
Inheritance in C++
3. Why we need Inheritance?
Reusability: Instead of writing the
same code again and again the
programmers reuse the existing code
to reduce time and avoid the mistakes
class Triangle
{ private:
float xCoord, yCoord;
public:
void set(float x, float y);
float area();
};
Inheritance in C++ 3
Shape
Triangle Rectangle
class Shape
{ private:
float xCoord, yCoord;
public:
void set(float x, float y);
};
class Rectangle
{
private:
float xCoord, yCoord;
public:
void set(float x, float y);
float area();
};
4. What is Inheritance?
Property by which one class can inherit the property of another class
New classes are created from existing classes
the existing class is called base class/super class
the new class is called derived class/sub class
4
Inheritance in C++
BASE CLASS
DERIVED CLASS
Arrow indicates derived from
Syntax
class derived_classname : access_mode base_classname
{………….}
5. Access specifier
It is used to set the availability of class members
There are 3 types of access specifier in C++
public , private, protected
class Simple
{ int v; //private access specifier by default
private:
int a; //private access specifier
protected :
int b; //protected access specifier
public :
int c; //public access specifier
};
Inheritance in C++ 5
Access public protected private
Same class Yes Yes Yes
Derived class Yes Yes No
Outside class Yes No No
Accessibility Table
6. Access mode or visibility mode
how the access specifiers of base class members are viewed in derived class
Access Mode: public, private or protected.
Inherit as public
class Child: public Parent
{
}
Inherit as protected
class Child: protected Parent
{
}
Inherit as private
class Child: private Parent
{
} 6
Inheritance in C++
7. Inherit as public
7
Inheritance in C++
Base Derived
Private members Inaccessible
Protected Members Protected members
Public Members Public members
8. Example for Inherit as Public
class Base
{public:
int m_public;
private:
int m_private;
protected:
int m_protected;
};
class Derived: public Base // note: public inheritance
{
// Public inheritance means:
// Public inherited members stay public (so m_public is treated as public)
// Protected inherited members stay protected (so m_protected is treated as protected)
// Private inherited members stay inaccessible (so m_private is inaccessible)
public:
Derived()
{
m_public = 1; // okay: m_public was inherited as public
m_private = 2; // not okay: m_private is inaccessible from derived class
m_protected = 3; // okay: m_protected was inherited as protected
}
};
8
Inheritance in C++
9. Cont…
int main()
{
// Outside access uses the access specifiers of the class being accessed.
Base base;
base.m_public = 1; // okay: m_public is public in Base
base.m_private = 2; // not okay: m_private is private in Base
base.m_protected = 3; // not okay: m_protected is protected in Base
Derived pub;
pub.m_public = 1; // okay: m_public is public in Pub
pub.m_private = 2; // not okay: m_private is inaccessible in Pub
pub.m_protected = 3; // not okay: m_protected is protected in Pub
}
9
Inheritance in C++
10. #include<iostream>
using namespace std;
class Base
{
int pri;
protected:
int pro;
public:
int pub;
void get_pri()
{cout<<"pri: ";cin>>pri;}
void get_pro()
{cout<<"pro: ";cin>>pro;}
void get_pub()
{cout<<"pub: ";cin>>pub;}
void put_pri()
{cout<<"pri: "<<pri;}
void put_pro()
{cout<<"pro: "<<pro;}
void put_pub()
{cout<<"pub: "<<pub;}
};
class Derived1:public Base
{
int derpri;
public:
void get()
{
cout<<"derpri: ";cin>>derpri;
//cin>>pri; //not ok -private datamember of base class
get_pri();//ok -public member of base class
cout<<"pro: ";cin>>pro;
cout<<"pub: "; cin>>pub;
}
void put()
{
cout<<"nderpri:"<<derpri;
//cout<<pri; //not ok -private datamember of base class
put_pri();//ok -public member of base class
cout<<"pro:"<<pro;
cout<<"pub:"<<pub;
}
};
11. int main()
{
Base bobj;
Derived1 dobj;
//bobj.pri=1 //not ok -According to Base class this is private data member
//bodj.pro=2 //not ok -According to Base class this is protected data member
cout<<"n getting bobj values n";
bobj.get_pri();
bobj.get_pro();
cout<<"pub: " ;
cin>>bobj.pub;//ok -According to Base class this is public data member
cout<<"n getting dobj values n";
//dobj.pri=1 //not ok -According to Derived class this is private data member of base class
//dodj.pro=2 //not ok-According to Derived class this is protected data member
dobj.get(); //ok - this is public member of Derived class
cout<<"pub: " ;
cin>>dobj.pub; //ok -According to class Derived this is public data member
bobj.put_pri(); //ok this is public member of Base class
bobj.put_pro(); //ok this is public member of Base class
bobj.put_pub(); //ok this is public member of Base class
dobj.put(); //ok - this is public member of Derived class
}
12. Inherit as protected
12
Inheritance in C++
Base Derived
Private members Inaccessible
Protected Members Protected members
Public Members Protected members
13. class Base
{public:
int m_public;
private:
int m_private;
protected:
int m_protected;
};
class Derived: protected Base // note: protected inheritance
{ // Protected inheritance means:
// Public inherited members become protected (so m_public is treated as protected)
// Protected inherited members stay protected (so m_protected is treated as protected)
// Private inherited members stay inaccessible (so m_private is inaccessible)
public:
Derived()
{ m_public = 1; // okay: m_public was inherited as protected
m_private = 2; // not okay: m_private is inaccessible from Derived class
m_protected = 3; // okay: m_protected was inherited as protected
}
};
Example for Inherit as protected
13
Inheritance in C++
14. Cont…
int main()
{
// Outside access uses the access specifiers of the class being accessed.
Base base;
base.m_public = 1; // okay: m_public is public in Base
base.m_private = 2; // not okay: m_private is private in Base
base.m_protected = 3; // not okay: m_protected is protected in Base
Derived Pro;
Pro.m_public = 1; // not okay: m_public is protected in Pro
Pro.m_private = 2; // not okay: m_private is inaccessible in Pro
Pro.m_protected = 3; // not okay: m_protected is protected in Pro
}
14
Inheritance in C++
15. #include<iostream>
using namespace std;
class Base
{
int pri;
protected:
int pro;
public:
int pub;
void get_pri()
{cout<<"pri: ";cin>>pri;}
void get_pro()
{cout<<"pro: ";cin>>pro;}
void get_pub()
{cout<<"pub: ";cin>>pub;}
void put_pri()
{cout<<"pri: "<<pri;}
void put_pro()
{cout<<"pro: "<<pro;}
void put_pub()
{cout<<"pub: "<<pub;}
};
class Derived1:protected Base
{
int derpri;
public:
void get()
{
cout<<"derpri: ";cin>>derpri;
//cin>>pri; //not ok -private datamember of base class
get_pri();//ok -public member of base class
cout<<"pro: ";cin>>pro;
cout<<"pub: "; cin>>pub;
}
void put()
{
cout<<"nderpri:"<<derpri;
//cout<<pri; //not ok -private datamember of base class
put_pri();//ok -public member of base class
cout<<"pro:"<<pro;
cout<<"pub:"<<pub;
}
};
16. int main()
{
Base bobj;
Derived1 dobj;
//bobj.pri=1 //not ok -According to Base class this is private data member
//bodj.pro=2 //not ok -According to Base class this is protected data member
cout<<"n getting bobj values n";
bobj.get_pri();
bobj.get_pro();
cout<<"pub: " ;
cin>>bobj.pub;//ok -According to Base class this is public data member
cout<<"n getting dobj values n";
//dobj.pri=1 //not ok -According to Derived class this is private data member of base class
//dodj.pro=2 //not ok-According to Derived class this is protected data member
dobj.get(); //ok - this is public member of Derived class
cout<<"pub: " ;
cin>>dobj.pub; //not ok -According to class Derived this is protected data member
bobj.put_pri(); //ok this is public member of Base class
bobj.put_pro(); //ok this is public member of Base class
bobj.put_pub(); //ok this is public member of Base class
dobj.put(); //ok - this is public member of Derived class
}
17. Inherit as private
17
Inheritance in C++
Base Derived
Private members Inaccessible
Protected Members Private members
Public Members Private members
18. class Base
{public:
int public_data;
private:
int private_data;
protected:
int protected_data;
};
class Derived: private Base // note: private inheritance
{ // Private inheritance means:
// Public inherited members become private (so public_data is treated as private)
// Protected inherited members become private (so protected_data is treated as private)
// Private inherited members stay inaccessible (so private_data is inaccessible)
public:
Derived()
{ public_data = 1; // okay: m_public is now private in Derived
private_data = 2; // not okay: Derived classes can't access private members in the base class
protected_data = 3; // okay: protected_data is now private in Derived
}};
Example for Inherit as private
18
Inheritance in C++
19. Cont…
int main()
{
Base base;
base.public_data = 1; // okay: public_data is public in Base
base.private_data = 2; // not okay: private_data is private in Base
base. protected_data = 3; // not okay: protected_data is protected in Base
Derived pri;
pri.public_data = 1; // not okay: public_data is now private in pri
pri.private_data = 2; // not okay: private_data is inaccessible in pri
pri. protected_data = 3; // not okay: protected_data is now private in pri
}
19
Inheritance in C++
23. Single inheritance
class A
{ int a; };
class B: public A
{ int b; };
int main()
{ A objA;
B objB;
cout<<sizeof(objA)<<" "<<sizeof(objB);
return 0;
} output :
4 8
a
0
Inheritance in C++ 23
a b
0 0
300 304 308
objB
class A
{ int a;
public:
void get_a()
{ cin>>a; }
};
class B: public A
{ int b;
public:
void get()
{ cin>>a;//[Error] 'int A::a' is private
cin>>b;
}
};
int main()
{ A objA;
B objB;
objA.get_a();
objB.get();
return 0;
}
200 204
objA
24. Single inheritance
class A
{ int a;
public:
void get_a()
{ cin>>a; }
void put_a()
{ cout<<a<<" "; }
};
class B: public A
{ int b;
public:
void get()
{ get_a();
cin>>b;
}
void put()
{ put_a();
cout<<b; }
};
Inheritance in C++
24
a
5
200 202
objA
a b
6 7
300 302 304
objB
int main()
{ A objA;
B objB;
cout<<"Entet values for OBJA & OBJBn";
objA.get_a();
objB.get();
cout<<"The values of OBJA & OBJBn";
objA.put_a();
objB.put();
return 0;
}
output
Entet values for OBJA & OBJB
5 6 7
The values of OBJA & OBJB
5 6 7
25. Single Inheritance
//Example
class Shape //base class
{
float x,y;
public:
void set()
{
cout<<"Enter x coordinate and y coordinaten";
cin>>x>>y;
}
float get_x()
{ return x; }
float get_y()
{ return y; }
};
OUTPUT:
Enter x coordinate and y coordinate
5 6
area=15
25
Inheritance in C++
class Triangle: public Shape //derived
{
float area;
public:
float tri_area()
{
area=0.5*get_x()*get_y();
return area;
}
};
int main()
{
Triangle obj;
obj.set();
cout<<"narea="<<obj. tri_area();
return 0;
}
x y area
5 6 15
700 704 708 712
obj
26. Function overriding in single inheritance
Giving new implementation of base class method into derived class is called function overriding.
Signature of base class method and derived class must be same.
Signature involves:
• Number of arguments
• Type of arguments
• Sequence of arguments
class BaseClass
{ public:
void disp()
{ cout<<“Base Class";}
};
class DerivedClass: public BaseClass
{ public:
void disp()
{ cout<<" Derived Class"; }
};
Inheritance in C++ 26
Output:
Derived Class
int main()
{ DerivedClass obj;
obj.disp();
}
27. Cont…
class BaseClass
{ public:
void disp()
{ cout<<"Base Classn"; }
};
class DerivedClass: public BaseClass{
public:
void disp()
{ BaseClass::disp();
cout<<"Derived Classn";
}
};
int main()
{ DerivedClass obj = DerivedClass();
obj.disp();
}
class BaseClass
{ public:
void disp()
{ cout<<"Base Classn"; }
};
class DerivedClass: public BaseClass
{ public:
void disp()
{ cout<<"Derived Classn“; }
};
int main() {
DerivedClass obj = DerivedClass();
obj.BaseClass::disp();
obj.disp();
}
Output:
Base Class
Derived Class
Inheritance in C++ 27
Output:
Base Class
Derived Class
28. Cont…
Multiple Inheritance:
-one derived class is inherited from more than one base class
28
Inheritance in C++
BASE CLASS- 1 BASE CLASS-2
DERIVED CLASS
BASE CLASS-N
29. Cont…
multiple inheritance
Inheritance in C++ 29
no1 no2
4 5
300 302 304
obj
class Sum: public Number1,public Number2
{
public:
int add()
{
return no1+no2;
}
};
int main()
{
Sum obj;
cout<<"Enter number1 and number2 for additionn";
obj.get_no1();
obj.get_no2();
cout<<"sum= "<<obj.add();
return 0;
}
class Number1
{
protected:
int no1;
public:
void get_no1()
{
cin>>no1;
}
};
class Number2
{
protected:
int no2;
public:
void get_no2()
{
cin>>no2;
}
};
Output:
Enter number1 and number2 for addition
4 5
sum= 9
30. Function overriding in multiple inheritance
class Base1
{ public:
void fun()
{ cout<<"Base1n";}
};
class Base2
{public:
void fun()
{ cout<<"Base2n";}
};
class Derived : public Base1, public Base2
{ public:
void fun( )
{cout<<"Derivedn"; }
};
int main()
{ Derived obj;
obj.fun();
}
Inheritance in C++ 30
Output:
Derived Class
31. Function overriding in multiple inheritance
Ambiguity in Multiple Inheritance
Suppose, two base classes have a same function which is not overridden in derived class. If that function is called by using the
derived class object ,the compiler shows error because the compiler doesn't know which function to call
class Base1
{ public:
void fun()
{ cout<<"Base1n";}
};
class Base2
{ void fun( )
{cout<<"Base2n"; }
};
class Derived : public Base1, public Base2
{ };
int main()
{
Derived obj;
obj.fun(); // Error!
}
Inheritance in C++ 31
32. Ambiguity in Multiple Inheritance can be solved using scope resolution
class Base1
{ public:
void fun()
{ cout<<"Base1n";}
};
class Base2
{ public:
void fun()
{ cout<<"Base2n";} };
class Derived : public Base1, public Base2
{ };
int main()
{ Derived obj;
//obj.fun();
obj. Base1::fun();
obj.Base2::fun();
}
Inheritance in C++ 32
class Base1
{ public:
void fun()
{ cout<<"Base1n";}
};
class Base2
{ public:
void fun()
{ cout<<"Base2n";} };
class Derived : public Base1, public Base2
{ public:
void fun( )
{ Base1::fun();
Base2::fun();
cout<<"Derivedn";
}
};
int main()
{ Derived obj;
obj.fun();
}
Output
Base1
Base2
Output
Base1
Base2
Derived
33. Cont…
Multilevel Inheritance
-a class inherits its properties from another derived class
33
Inheritance in C++
BASE CLASS
DERIVED CLASS-1
DERIVED CLASS-2
DERIVED CLASS-N
BASE CLASS
DERIVED CLASS-1
DERIVED CLASS-2
34. Cont…
Inheritance in C++ 34
Person
Employee
programmer
Person Employee programmer
name company name Number of programming language known
age salary
35. Cont…
// Multilevel inheritance
class person
{
char name[10];
int age;
public:
void get_person()
{
cout<<"Name: ";
cin>>name;
cout<<"Age: ";
cin>>age;
}
void put_person()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
}
};
class employee: public person
{
char company[10];
float salary;
public:
void get_employee()
{
get_person();
cout<<"Name of Company: ";
cin>>company;
cout<<"Salary: Rs.";
cin>>salary;
}
void put_employee()
{
put_person();
cout<<"Name of Company:”
cout<<company<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};
Inheritance in C++ 35
36. class programmer: public employee
{int number;
public:
void get_programmer()
{ get_employee();
cout<<"Number of programming language known: ";
cin>>number;
}
void put_programmer()
{ put_employee();
cout<<"Number of programming language known: "<<number;
}
};
int main()
{ programmer p;
cout<<"Enter data"<<endl;
p.get_programmer();
cout<<endl<<"Displaying data"<<endl;
p.put_programmer();
}
Output:
Enter data
Name: aaa
Age: 24
Name of Company: TCS
Salary: Rs.900000
Number of programming language known: 5
Displaying data
Name: aaa
Age: 24
Name of Company: TCS
Salary: Rs.900000
Number of programming language known: 5
Inheritance in C++ 36
name age company salary number
aaa 24 TCS 25000 5
800 810 812 822 826 828
p
37. //Function overriding in multilevel inheritance
class Base
{ public:
void fun()
{ cout<<"Basen";}
};
class derived1:public Base
{ public:
void fun()
{ cout<<"derived1n";}
};
class derived2 : public derived1
{ };
int main()
{ derived2 obj;
obj.fun();
}
class Base
{ public:
void fun()
{ cout<<"Basen";}
};
class derived1:public Base
{ public:
void fun()
{ cout<<"derived1n";}
};
class derived2 : public derived1
{public:
void fun()
{ cout<<"derived2n";} };
int main()
{ derived2 obj;
obj.fun();
}
Inheritance in C++ 37
Output
derived2
Output
derived1
class Base
{ public:
void fun()
{ cout<<"Basen";}
};
class derived1:public Base
{ };
class derived2 : public derived1
{ };
int main()
{ derived2 obj;
obj.fun();
}
Output
Base
38. //Function overriding in multilevel inheritance
class Base
{ public:
void fun()
{ cout<<"Basen";}
};
class derived1:public Base
{ public:
void fun()
{
cout<<"derived1n";
}
};
class derived2 : public derived1
{ public:
void fun()
{
Base::fun();
derived1::fun();
}
};
int main()
{ derived2 obj;
obj.fun();
}
class Base
{ public:
void fun()
{ cout<<"Basen";}
};
class derived1:public Base
{ public:
void fun()
{ cout<<"derived1n";}
};
class derived2 : public derived1
{public:
void fun()
{ cout<<"derived2n";} };
int main()
{ derived2 obj;
obj.Base::fun();
obj.derived1::fun();
obj.fun();
}
Inheritance in C++ 38
Output
Base
derived1
derived2
Output
Base
derived1
derived2
class Base
{ public:
void fun()
{ cout<<"Basen";}
};
class derived1:public Base
{ public:
void fun()
{ Base::fun();
cout<<"derived1n";
}
};
class derived2 : public derived1
{ public:
void fun()
{
derived1::fun();
cout<<"derived2";
}
};
int main()
{ derived2 obj;
obj.fun();
}
Output
Base
derived1
derived2
39. Cont…
Hierarchical Inheritance
- more than one derived class is created from a single base class
39
Inheritance in C++
DERIVED CLASS- 1 DERIVED CLASS-2
BASE CLASS
DERIVED CLASS- N
40. Cont…
Inheritance in C++ 40
Person
Employee student
Person Employee programmer
name company name course
no basicpay
41. Cont…
// Hirarchical inheritance
class Person {
int no;
char name[10];
public:
void getPersonDetails()
{
cout << "nEnter the Person number:";
cin>>no;
cout << "Enter the Person name:";
cin>>name;
}
void person_display()
{
cout <<"nPerson number:"<<no;
cout <<"nPerson name:"<<name;
}
};
class Employee : private Person
{ float bp;
public:
void getEmployeeDetails()
{
getPersonDetails();
cout << "Enter the Basic pay:";
cin>>bp;
}
void employee_display()
{
person_display();
cout <<"nEmployee Basic pay:"<<bp;
}
};
class Student : private Person
{ char course[10];
public:
void getStudentDetails() {
getPersonDetails();
cout << "Enter the Student course Name:";
cin>>course;
}
void student_display()
{person_display();
cout <<"nStudent Course name:"<<course<<endl;
}
};
int main()
{ char ch;
Student s; Employee e;
cout << "nStudent Details n";
s.getStudentDetails();
s.student_display();
cout << "nEmployee Details n";
e.getEmployeeDetails();
e.employee_display();
return 0;
}
Inheritance in C++ 41
42. Cont…
Output:
Student Details
Enter the Person number:1
Enter the Person name:aaa
Enter the Student course Name:BCA
Person number:1
Person name:aaa
Student Course name:BCA
Employee Details
Enter the Person number:2
Enter the Person name:bbb
Enter the Basic pay:29000
Person number:2
Person name:bbb
Employee Basic pay:29000
Inheritance in C++ 42
no name course
1 aaa BCA
300 302 312 322
s
no name bp
2 bbb 29000
600 602 612 616
e
43. Cont…
43
Inheritance in C++
Class A
Class C
Class B
Class D
Hybrid Inheritance:
-the combination of more than one inheritance type
Example1:
Student
Test Sports
Result
Example2:
Hierarchical Inheritance + Multiple Inheritance
Multilevel Inheritance+ Multiple Inheritance
45. Cont…
//sample program to explain Hybrid inheritance
class student
{ protected:
int r_no;
public:
void getRollno()
{ cin >> r_no; }
void putRollno()
{ cout << r_no ; }
};
class test : public student
{ protected:
int part1, part2;
public:
void getMarks()
{ cin >> part1>> part2; }
void putMarks()
{ cout << part1<< part2 << "n"; }
};
class sports
{ protected:
int score;
public:
void getSportsMarks()
{ cin >> score; }
void putSportsMarks()
{ cout << score ;}
};
class result : public test, public sports
{ int total;
public:
void display ()
{ total = part1 + part2 + score;
putRollno();
putMarks();
putSportsMarks();
cout << "Total Score : " << total ;
}
};
int main ()
{ result s1;
s1.getRollno();
s1.getMarks();
s1.getSportsMarks();
s1.display();
return 0;
}
Inheritance in C++ 45
46. Execution order of constructor and destructor
Both base class and derived class can have their own constructor and destructor
functions
The constructor functions are executed in the order of derivation
The destructor functions are executed in the reverse order
Inheritance in C++ 46
Base class
Derived class1
Derived class2
Derived classN
Base class ,Derived class1 ,Derived class2,……………………………Derived classN
Derived class N ,Derived class N-1 ,Derived N-2,……………………………….Derived class1,Base class
47. Cont…
class Base
{ public:
Base()
{ cout<<"base constructorn";}
~Base()
{ cout<<"base destructorn";}
};
class derived1:public Base
{ public:
derived1()
{ cout<<"derived1 constructorn";}
~derived1()
{ cout<<"derived1 destructorn";}
};
class derived2 : public derived1
{public:
derived2()
{ cout<<"derived2 constructorn";}
~derived2()
{ cout<<"derived2 destructorn";}
};
int main()
{ derived2 obj;
}
output
base constructor
derived1 constructor
derived2 constructor
derived2 destructor
derived1 destructor
base destructor
Whenever the derived class’s default constructor is called, the base class’s default
constructor is called automatically
Base class data members are all initialized first, before initializing the derived class
member
Derived class data members are all destroyed first, before destroying the base class
members
Inheritance in C++ 47
48. Cont…
In case of multiple inheritance the order of execution of constructors depend upon the order of
inheritance
The constructor functions are executed in the order of inheritance
The destructor functions are executed in the reverse order
Inheritance in C++ 48
Base class1 Base class2
Derived class
Base class N
Base class 1 ,Base class2 ……….Base classN ,Derived class
Derived class ,Base class N ,Base classN-1………….. Base class1
49. Cont…
class A
{
public:
A()
{ cout<<"A()"<<endl; }
~A()
{ cout<<"~A()"<<endl; }
};
class B
{
public:
B()
{ cout<<"B()"<<endl; }
~B()
{ cout<<"~B()"<<endl; }
};
class C:public A,public B
{
public:
C()
{ cout<<"C()"<<endl; }
~C()
{cout<<"~C()"<<endl;}
};
int main()
{ C obj;
return 0;
}
A()
B()
C()
~C()
~B()
~A()
Inheritance in C++ 49
50. Cont…
To call the parameterized constructor of base class inside the parameterized
constructor of sub class, we have to mention it explicitly otherwise it will call
the default constructor of base class
The parameterized constructor of base class should be called in the
parameterized constructor of sub class
Inheritance in C++ 50
51. Cont…
class A
{
protected:
int count;
public:
A()
{ cout<<"A()"<<endl;}
A(int i):count(i)
{
cout<<"A(int)"<<endl;
}
};
class B:public A
{
public:
B(int c)
{
cout<<"B(int)"<<endl;
}
};
int main()
{
B obj(5);
return 0;
}
class A
{
protected:
int count;
public:
A()
{ cout<<"A()"<<endl;}
A(int i):count(i)
{ cout<<"A(int)"<<endl; }
};
class B:public A
{
public:
B():A(5)
{ cout<<"B()"<<endl; }
};
int main()
{ B obj;
return 0;
}
Inheritance in C++ 51
Output
A()
B(int)
Output
A(int)
B(int)
class A
{
protected:
int count;
public:
A()
{ cout<<"A()"<<endl;}
A(int i):count(i)
{
cout<<"A(int)"<<endl;
}
};
class B:public A
{
public:
B(int c):A(c)
{
cout<<"B(int)"<<endl;
}
};
int main()
{
B obj(5);
return 0;
}
Output
A(int)
B()
53. Virtual base class : diamond problem
Inheritance in C++ 53
B C
A
D
A
B C
A
D
Multiple inheritance will create diamond problem if it is not used carefully
When ‘D’ class object is created then two copies of the ‘A’ class members are available in ‘D’ -
one from ‘B’, and one from ‘C’
54. Virtual base class Cont…
class A
{ int a; };
class B: public A
{ };
class C: public A
{ };
class D: public B, public C
{ };
int main()
{
cout<<"sizeof(A) "<<sizeof(A)<<"n ";
cout<<"sizeof(B) "<<sizeof(B)<<"n ";
cout<<"sizeof(C) "<<sizeof(C)<<"n ";
cout<<"sizeof(D) "<<sizeof(D)<<"n ";
}
Inheritance in C++ 54
Output:
sizeof(A) 2
sizeof(B) 2
sizeof(C) 2
sizeof(D) 4 // diamond problem
55. #include<iostream>
using namespace std;
class A
{ protected:
int a;
public:
A()
{a=0;cout<<"I am A()n"; }
};
class B: public A
{ public:
B()
{ cout<<"I am B()n"; }
};
class C: public A
{ public:
C()
{ cout<<"I am C()n"; }
};
class D: public B, public C
{ public:
D()
{ cout<<"I am D()n"; }
};
int main()
{ D obj; }
I am A()
I am B()
I am A()
I am C()
I am D()
A class constructor is invoke from class B and also from
class C
56. #include<iostream>
using namespace std;
class A
{ protected:
int a;
public:
A()
{a=0;cout<<"I am A()n"; }
A(int x)
{ a=x;cout<<"I am A(int)n";}
};
class B: public A
{ public:
B(int b):A(b)
{ cout<<"I am B(int)n"; }
};
class C: public A
{ public:
C(int c):A(c)
{ cout<<"I am C(int)n"; }
};
class D: public B, public C
{ public:
D(int b,int c):B(b),C(c)
{cout<<"I am D(int)n"; }
};
int main()
{ D obj(1,2); }
I am A(int)
I am B(int)
I am A(int)
I am C(int)
I am D(int)
57. Virtual base class Cont…
class A
{ protected:
int a;
public:
A(int x)
{ a=x;}
};
class B: public A
{ public:
B(int b):A(b)
{ }
};
Inheritance in C++ 57
class C: public A
{ public:
C(int c):A(c)
{ }
};
class D: public B, public C
{
public:
D(int b,int c):B(b) ,C(c)
{ cout<<a; // [Error] reference to 'a' is ambiguous
}
};
int main()
{ D obj(1,2);
}
class D: public B, public C
{ public:
D(int b,int c):B(b) ,C(c)
{
cout<<B::a<<“n”;
cout<<C::a<<n”;
}
};
Output:
1
2
58. #include<iostream>
using namespace std;
class A
{ protected:
int a;
public:
A()
{a=0;cout<<"I am A()n"; }
};
class B: public virtual A
{ public:
B()
{ cout<<"I am B()n"; }
};
class C: virtual public A
{ public:
C()
{ cout<<"I am C()n"; }
};
class D: public B, public C
{ public:
D()
{ cout<<"I am D()n"; }
};
int main()
{ D obj; }
Output:
I am A()
I am B()
I am C()
I am D()
To share a only one copy of base class, simply insert the
“virtual” keyword in the inheritance list of the derived class
Virtual base class Cont…
Note 1:
virtual can be written before or after the visibility mode
‘B’ and ‘C’ constructors still have calls to the ‘A’
constructor. When creating an instance of D, these
constructor calls are simply ignored because ‘D’ is
responsible for creating the ‘A’, not B or C
However, if instance of B or C are created, those
constructor calls would be used, and normal inheritance
rules apply.
59. Virtual base class Cont…
class A
{ protected:
int a;
public:
A()
{a=0;cout<<"I am A()n"; }
A(int x)
{ a=x;cout<<"I am A(int)n";}
};
class B: virtual public A
{ public:
B(int b):A(b)
{ cout<<"I am B(int)n"; }
};
class C: virtual public A
{ public:
C(int c):A(c)
{ cout<<"I am C(int)n"; }
};
class D: public B, public C
{ public:
D(int b,int c):B(b),C(c)
{ cout<<a<<endl;
cout<<"I am D(int,int)n";
}
};
int main()
{ D obj(1,2); }
Inheritance in C++ 59
Output:
I am A()
I am B(int)
I am C(int)
0
I am D(int,int)
60. Virtual base class Cont…
class A
{ protected:
int a;
public:
A()
{cout<<"I am A()n"; }
A(int x)
{ a=x;cout<<"I am A(int)n";}
};
class B:virtual public A
{ public:
B(int b):A(b)
{ cout<<"I am B(int)n"; }
};
class C:virtual public A
{ public:
C(int c):A(c)
{ cout<<"I am C(int)n"; }
};
class D: public B, public C
{ public:
D(int b,int c):A(5),B(b),C(c)
{ cout<<a; }
};
int main()
{ cout<<"n D object creationn";
D obj(1,2);
cout<<"nB abd C object creationn";
B objb(1) ;
C objc(1) ;
}
Output:
D object creation
I am A(int)
I am B(int)
I am C(int)
5
B and C objects creation
I am A(int)
I am B(int)
I am A(int)
I am C(int)
Inheritance in C++ 60
61. Virtual base class Cont…
class A
{ protected:
int a;
public:
A()
{cout<<"I am A()n"; }
A(int x)
{ a=x;cout<<"I am A(int)n";}
};
class B:virtual public A
{ public:
B(int b):A(b)
{ cout<<"I am B(int)n"; }
};
class C:virtual public A
{ public:
C(int c):A(c)
{ cout<<"I am C(int)n"; }
};
class D: public B, public C
{ public:
D(int b,int c):C(b),B(c),A(b)
{ cout<<a; }
};
int main()
{ D obj(1,2);
cout<<"nB abd C object creationn";
B objb(1) ;
C objc(1) ;
}
Output:
I am A(int)
I am B(int)
I am C(int)
5
B and C objects creation
I am A(int)
I am B(int)
I am A(int)
I am C(int)
Inheritance in C++ 61
62. Virtual base class Cont…
class E
{ public:
E()
{ cout<<"I am E()n"; }
};
class A
{ protected:
int a;
public:
A()
{cout<<"I am A()n"; }
};
class B:virtual public A
{ public:
B()
{ cout<<"I am B()n"; }
};
class C:virtual public A
{ public:
C()
{ cout<<"I am C()n"; }
};
class D:public E, public B, public C
{ public:
D()
{ }
};
int main()
{ D obj;
}
Note :
virtual base classes are always created
before non-virtual base classes, which
ensures all bases get created before their
derived classes.
if a class inherits one or more classes
that have virtual parents,
the most derived class is responsible for
constructing the virtual base class
Inheritance in C++ 62
Output:
I am A()
I am E()
I am B()
I am C()
63. Virtual base class Cont…
class A
{ protected:
int a;
public:
A()
{cout<<"I am A()n"; }
};
class B:virtual public A
{ public:
B()
{ cout<<"I am B()n"; }
};
class C:virtual public A
{ public:
C()
{ cout<<"I am C()n"; }
};
class D:public B, public C
{ public:
D()
{ }
};
class E:public D
{ public:
E()
{ cout<<"I am E()n"; }
};
int main()
{ cout<<"D object createdn";
D objD;
cout<<"E object createdn";
E objE;
}
Inheritance in C++ 63
Output:
D object created
I am A()
I am B()
I am C()
E object created
I am A()
I am B()
I am C()
I am E()