Inheritance
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY
School Of Computer Engineering
Dr. Pradeep Kumar Mallick
Associate Professor [II]
School of Computer Engineering,
Kalinga Institute of Industrial Technology (KIIT),
Deemed to be University,Odisha
3 Credit Lecture Note 12
Chapter Contents
2
Inheritance
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
Inheritance
3
In C++, inheritance is a process in which one object acquires all the properties
and behaviors of its parent object automatically.
In such way, you can reuse, extend or modify the attributes and behaviors
which are defined in other class.
Types Of Inheritance:
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
Dynamic constructor
4
Derived Classes:
A Derived class is defined as the class derived from the base class.
The Syntax of Derived class:
class derived_class_name : visibility-mode base_class_name
{
// body of the derived class.
}
Inheritance
5
derived_class_name: It is the name of the derived class.
visibility mode: The visibility mode specifies whether the features of the base
class are publicly inherited or privately inherited. It can be public or private.
base_class_name: It is the name of the base class.
When the base class is privately inherited by the derived class, public members
of the base class becomes the private members of the derived class. Therefore, the
public members of the base class are not accessible by the objects of the derived
class only by the member functions of the derived class.
When the base class is publicly inherited by the derived class, public members of
the base class also become the public members of the derived class. Therefore, the
public members of the base class are accessible by the objects of the derived class
as well as by the member functions of the base class.
Note: 1. In C++, the default mode of visibility is private.
2. The private members of the base class are never inherited.
Single Inheritance
6
Single inheritance is defined as the inheritance in which a derived class is
inherited from the only one base class.
When one class inherits another class, it is known as single level inheritance.
Let's see the example of single level inheritance which inherits the fields only.
Example-1
7
#include <iostream>
using namespace std;
class Account {
public:
float salary ;
};
class Programmer: public Account {
public:
float bonus ;
};
int main(void) {
Programmer p1;
[Link]=60000; [Link]=5000;
cout<<"Salary: "<<[Link]<<endl; cout<<"Bonus: "<<[Link]<<endl;
return 0; }
Single Level Inheritance: Inheriting Methods
8
#include <iostream>
using namespace std;
int main(void) {
class Animal {
Dog d1;
public:
[Link]();
void eat() {
[Link]();
cout<<"Eating..."<<endl;
return 0;
}
}
};
O/P:
class Dog: public Animal
Eating...
{
Barking...
public:
void bark(){
cout<<"Barking...";
}
};
Example-3
9
#include <iostream>
class B : public A
using namespace std;
{
class A
public:
{
void display()
int a , b;
{
public:
cout <<"Multiplication of a and b is : "< < c;
int mul(int x,int y)
}
{
};
a= x ; b= y;
int main()
int c = a*b;
{
B b;
}
[Link]();
};
return 0;
}
How to make a Private Member Inheritable
10
The private member is not inheritable. If we modify the visibility
mode by making it public, but this takes away the advantage of data
hiding.
C++ introduces a third visibility modifier, i.e., protected. The member
which is declared as protected will be accessible to all the member
functions within the class as well as the class immediately derived from
it.
1. Public: When the member is declared as public, it is accessible to all
the functions of the program.
2. Private: When the member is declared as private, it is accessible
within the class only.
3. Protected: When the member is declared as protected, it is accessible
within its own class as well as the class immediately derived from it.
Visibility of Inherited Members
11
Base class visibility Derived class visibility
Public Private Protected
Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
Public Public Private Protected
Multi Level Inheritance Example
12
Multilevel inheritance is a process of deriving a class from another
derived class.
When one class inherits another class which is further inherited by
another class, it is known as multi level inheritance in C++.
Inheritance is transitive so the last derived class acquires all the
members of all its base classes.
Example
13
#include <iostream> class BabyDog: public Dog
using namespace std; {
class Animal { public:
public: void weep() {
void eat() { cout<<"Weeping...";
cout<<"Eating..."<<endl; }
};
}
int main(void) {
};
BabyDog d1;
class Dog: public Animal [Link]();
{ [Link]();
public: [Link]();
void bark(){ return 0;
cout<<"Barking..."<<endl; }
}
};
Multiple Inheritance
14
Multiple inheritance is the process of deriving a new class that inherits
the attributes from two or more classes.
Syntax
15
class D : visibility B-1, visibility B-2, ...
{
// Body of the class-D;
}
Multiple Inheritance
16
#include<iostream>
using namespace std;
class A int main()
{ {
public: C c;
A() { cout << "A's constructor called" << endl; } return 0;
};
}
class B
Output:
{
public: B's constructor called
B() { cout << "B's constructor called" << endl; } A's constructor called
}; C's constructor called
class C: public B, public A // Note the order
{
public:
C() { cout << "C's constructor called" << endl; }
};
Example
17
#include <iostream>
void get_b(int n) int main()
using namespace std;
{ {
class A
{
b = n; C c;
protected: } c.get_a(10);
int a; }; c.get_b(20);
public: class C : public A,public B [Link]();
void get_a(int n) { return 0;
{ public: }
a = n;
void display()
}
{
};
class B cout << "The value of a is : " <<a<< endl;
{ cout << "The value of b is : " <<b<<endl;
protected: cout<<"Addition of a and b is : "<<a+b;
int b; }
public: };
Abiguity Resolution in Inheritance
18
Ambiguity can be occurred in using the multiple inheritance when a function with the
same name occurs in more than one base class.
Abiguity Resolution in Inheritance
19
#include <iostream> class C : public A, public B
using namespace std; {
class A void view()
{
{
public:
display();
void display()
{ }
cout << "Class A" << endl; };
} int main()
}; {
class B C c;
{
[Link]();
public:
return 0;
void display()
{
}
cout << "Class B" << endl; Output:
} error: reference to 'display' is
ambiguous display()
};
Abiguity Resolution in Inheritance : solution
20
class C : public A, public B
{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.
}
};
We can solve this problem by defining a named instance within the derived class ,
using the scope resolution operator.
Or
class C : public A, public B
{
void display() // overrides display( )of A and B.
{
A :: display(); // Calling the display() function of class A. }
};
Abiguity Resolution in Single Inheritance
21
An ambiguity can also occur in single inheritance.
class A
{
public:
void display()
{
cout<<”Class A”;
}
};
class B : public A
{
public:
void display()
{
cout<<”Class B” ;
}
};
Abiguity Resolution in Single Inheritance
22
In the above case, the function of the derived class overrides the method of the base
class. Therefore, call to the display() function will simply call the function defined in
the derived class. If we want to invoke the base class function, we can use the class
resolution operator.
int main()
{
B b;
[Link](); // Calling the display() function of B class.
b.A :: display(); // Calling the display() function defined in A class.
}
C++ Hierarchical Inheritance
23
Hierarchical inheritance is defined as the process of deriving more than one class
from a base class.
Syntax
24
class A
{
// body of the class A.
} ;
class B : public A
{
// body of class B.
} ;
class C : public A
{
// body of class C.
} ;
class D : public A
{
// body of class D.
};
Example-1
25
#include <iostream>
using namespace std; class Triangle : public Shape
{
class Shape // Declaration of base class.
public:
{ int triangle_area()
public: {
int a, b; float result = 0.5*a*b;
void get_data(int n,int m) return result;
}
{ };
a= n; b = m; main(){
} Rectangle r; Triangle t;
}; int length,breadth,base,height;
cout << "Enter the length and breadth of a rectangle: ";
class Rectangle : public Shape
cin>>length>>breadth;
{ r.get_data(length,breadth);
public: int m = r.rect_area();
int rect_area() cout << "Area of the rectangle is : " <<m ;
{ cout << "Enter the base and height of the triangle: " ;
cin>>base>>height;
int result = a*b;
t.get_data(base,height);
return result; float n = t.triangle_area();
} cout <<"Area of the triangle is : " << n ;
}; return 0;
}
Hybrid Inheritance
26
Hybrid inheritance is a combination of more than one type of inheritance.
Virtual base class in C++
27
Virtual base classes are used in virtual inheritance in a way of preventing multiple
“instances” of a given class appearing in an inheritance hierarchy when using
multiple inheritances.
Example
28
class ClassA
class ClassD : public ClassB, public ClassC
{
{
public:
public:
int a;
int d;
};
};
class ClassB : virtual public ClassA
void main()
{ {
public: ClassD obj;
int b; obj.a = 10; //Statement 1
}; obj.a = 100; //Statement 2
class ClassC : virtual public ClassA obj.b=20;obj.c=30; obj.d = 40;
{
cout<< "\n A : "<< obj.a; // 100
public:
cout<< "\n B : "<< obj.b; // 20
int c;
}; cout<< "\n C : "<< obj.c; // 30
cout<< "\n D : "<< obj.d; // 40
}
Order of Constructor Call with Inheritance in C++
29
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default constructor
is executed and then the derived class's constructor finishes execution.
Points to Remember
[Link] derived class's default constructor is called or parameterised is called, base
class's default constructor is always called inside them.
[Link] call base class's parameterised constructor inside derived class's parameterised
constructo, we must mention it explicitly while declaring derived class's
parameterized constructor.
Base class Default Constructor in Derived class
Constructors
30
Default constructor is present in all the classes. In the below example we will see
when and why Base class's and Derived class's constructors are called.
class Base{ class Derived : public Base{
int x; int y;
public: public:
// default constructor // default constructor
Derived()
Base()
{
{ cout << "Derived default
cout << "Base default constructor\n"; constructor\n";
}}; }
// parameterized constructor
Derived(int i)
{
cout << "Derived parameterized
constructor\n";
}};
Base class Default Constructor in Derived class
Constructors
31
int main(){
Base b;
Derived d1;
Derived d2(10);}
Output:
Base default constructor
Base default constructor
Derived default constructor
Base default constructor
Derived parameterized constructor
You will see in the above example that with both the object creation of the Derived
class, Base class's default constructor is called.
Base class Parameterized Constructor in Derived
class Constructor
32
We can explicitly mention to call the Base class's parameterized constructor when
Derived class's parameterized constructor is called
class Base{
class Derived : public Base{
int x; int y;
public: public:
// parameterized constructor // parameterized constructor
Base(int i) Derived(int j):Base(j)
{
{
y = j;
x = i; cout << "Derived Parameterized
cout << "Base Parameterized Constructor\n"; Constructor\n";
}}; }};
int main()
{
Derived d(10) ;
O/P }
Base Parameterized Constructor Derived Parameterized Constructor
Why is Base class Constructor called inside
Derived class?
33
Constructors have a special job of initializing the object properly. A Derived class
constructor has access only to its own class members, but a Derived class object also
have inherited property of Base class, and only base class constructor can properly
initialize base class members. Hence all the constructors are called, else object
wouldn't be constructed properly.
34