8 Inheritance
8 Inheritance
Overriding
Inheritance
• Introduction
• Inheritance is the process of creating a new class from one or more existing classes
• The new classes are created from existing classes in which the properties of the existing
classes are simply extend to the new classes
• The new classes are called derived or sub or child or descendent classes and the existing
classes are called as base or super or parent or ancestor classes
• The relationship between the base and derived classes is known as kind of relationship
• The object of derived class can access the members of the base as well as derived classes
but object of base class cannot access members of derived classes
Inheritance
• Reusability
• Reusability means the reuse
of properties of the base class
in the derived classes
• We can add extra features in
the existing class
• Reusability is achieved using
inheritance
Inheritance
• Access specifiers in Inheritance
• Inheritance Syntax
class baseclass
{…………….};
class derivedclass : access-specifier baseclass
{
…………….
};
Types of Inheritance
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance
• Multipath inheritance
Single Inheritance
• In Single inheritance a class is derived from only one base class
• Protected inheritance
• Private in heritance
Single Inheritance
int main()
// public inheritance {
#include<iostream>
derived d1;
using namespace std;
d1.read();
class base
d1.print();
{ protected:
d1.c=20;
int a; int b;
//d1.b=20;
public:
d1.sum();
int c;
d1.sumprint();
void read()
return 0;
{ cin>>a>>b>>c; }
}
void print()
{ cout<<a<<" "<<b<<" "<<c; }
Input:
};
11 22 10
class derived : public base
Output:
{ private:
11 22 10
int s;
sum=53
public:
void sum()
{ s=a+b+c; }
void sumprint()
{ cout<<"\nsum="<<s; }
Single Inheritance
//protected inheritance public:
void sum()
#include<iostream> { read();
using namespace std; s=a+b+c;
class base }
{ void sumprint()
protected: { print();
int a; cout<<"\nsum="<<s;
int b; }
public: };
int c; int main()
void read() { derived d1;
{ cin>>a>>b>>c; } d1.sum();
void print() d1.sumprint();
{ cout<<a<<" "<<b<<" "<<c;} return 0;
}; }
class derived : protected base Output:
{ 11 22 10
private: 11 22 10
int s; sum=43
Single Inheritance
// private inheritance void sumprint()
#include<iostream> { sum();
using namespace std; print();
class base cout<<"\nsum="<<s;
{ protected: }
int a; int b; };
void read() int main()
{ cin>>a>>b>>c; } { derived d1;
public: d1.sumprint();
int c; return 0;
void print() }
{ cout<<a<<" "<<b<<" "<<c;}
}; Output:
class derived : private base 11 22 10
{ private: 11 22 10
int s; sum=43
protected:
void sum()
{ read();
s=a+b+c;
}
Multilevel Inheritance
• 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.
Multilevel Inheritance
#include <iostream> int main()
using namespace std; {
class Vechicle Honda h1;
{ h1.vdetails();
public: h1.twdetails();
void vdetails() h1.hdetails();
{ cout<<"Moving"<<endl; } return 0;
}; }
class TwoWheeler: public Vechicle
{
public: Output:
void twdetails() Moving
{ cout<<"Two wheelers"<<endl; } Two wheelers
}; Honda Two wheelers
class Honda: public TwoWheeler
{
public:
void hdetails()
{
cout<<"Honda Two wheelers";
}
Multiple Inheritance
• Multiple inheritance is the process of deriving a new class from two or
more base classes
class D : visibility B-1, visibility B-2, …….
{
// Body of the class;
}
Multiple Inheritance
#include <iostream> {
using namespace std; cout<<id<<endl<<name<<endl<<salary;
class emppersonal }
{ };
protected: int main()
int id; {
char name[20]; A3 obj;
}; obj.read();
class empsalary obj.print();
{ return 0;
protected: }
float salary;
}; Input:
class A3: public emppersonal, public empsalary 120
{ Jamuna
public: 20000
void read() Output:
{ 120
cin>>id>>name>>salary; Jamuna
} 20000
Hybrid Inheritance
• Hybrid inheritance is the combination of more than one
type of inheritance
• Deriving a class using more types of inheritance
emppersonal empsalary
bonus
emp
Hybrid Inheritance
#include <iostream> cout<<"Total salary:"<<(salary+bonus);
using namespace std; }
class emppersonal };
{ protected: int main()
int id; {
char name[20]; emp obj;
}; obj.read();
class empsalary obj.print();
{ protected: return 0;
float salary; }
};
class bonus : public empsalary Input:
{ protected: 101
float bonus; Keerthi
}; 20000
class emp: public emppersonal, public bonus 2000
{ public: Output:
void read() id:101
{ cin>>id>>name>>salary>>bonus; } name:Keerthi
void print() Total salary:22000
{ cout<<"id:"<<id<<endl;
Hierarchical Inheritance
• Hierarchical inheritance is defined as the process of deriving more than one
class from a base class
emp
empsalary empbonus
Hierarchical Inheritance
#include <iostream> };
using namespace std; int main()
class emppersonal { empsalary e1;
{ protected: int id; char name[20]; empbonus e2;
public: e1.read(); e1.read1();
void read() e2.read2();
{ cin>>id>>name; } e1.print(); e1.print1();
void print() e2.print2();
{cout<<"id:"<<id<<endl; return 0;
cout<<"name:"<<name<<endl; } }
}; Input:
class empsalary : public emppersonal 121
{ protected: float salary; Sriram
public: 25000
void read1() { cin>>salary; } 2500
void print1() Output:
{ cout<<"salary:"<<salary<<endl;} id:121
}; name:Sriram
class empbonus : public emppersonal salary:25000
{ protected:float bonus; bonus:2500
public: void read2() {cin>>bonus;}
Multipath Inheritance
• When a class is derived from two or more classes, those are derived from the
same base class
• Multipath inheritance also consists of many types of inheritance such as
multiple, multilevel and hierarchical
Multipath Inheritance
#include<iostream> { A4 a;
using namespace std; a.print();
class A1 }
{ protected:
int a1;
}; Output:
class A2: public A1 F:\CPP Programs\demo\multipathinheritance.cpp
{ protected: [Error] reference to 'a1' is ambiguous
int a2;
};
class A3: public A1
{ protected:
int a3;
};
class A4: public A2,A3
{ protected:
int a4;
public:
void print()
{ cout<<a1<<" "<<a2<<" "<<a3<<" "<<a4; }
};
Virtual Base Classes
• To overcome the ambiguity occurring due to multipath inheritance, the C+
+ provides the keyword virtual
• The keyword virtual declares the specified classes virtual
• When classes are declared as virtual, the compiler takes essential caution
to avoid the duplication of member variables
• Thus, we make a class virtual if its is a base class that has been used by
more than one derived class as their base class
Virtual Base Classes
#include<iostream> int main()
using namespace std; { A4 a;
class A1 a.print();
{ protected: }
int a1=10;
}; Output:
class A2: virtual public A1 10 20 30 40
{ protected:
int a2=20;
};
class A3: virtual public A1
{ protected:
int a3=30;
};
class A4: public A2,A3
{ protected:
int a4=40;
public:
void print()
{ cout<<a1<<" "<<a2<<" "<<a3<<" "<<a4; }
};
Constructors, Destructors and Inheritance
• Constructor- Initialize member variables of the object, Destructor-Destroy
the object
• Compiler automatically invokes constructors and destructors
• The derived class does not require a constructor, if the base class contains
a zero-argument constructor
• If base classes has a parameterized constructor, then it is essential for the
derived class to have a constructor
• The derived class constructor passes arguments to the base class
constructor
• Destructors are executed in reverse order of constructor execution
• The destructors are executed when an object goes out of scope
Constructors, Destructors and Inheritance
#include<iostream> ~C()
using namespace std; {cout<<"Class C-Destructor"<<endl; }
class A };
{ public: int main()
A() {
{cout<<"Class A-Zero Argument constructor"<<endl; C a;
} C b(10,20);
A(int a)
{cout<<"Class A-1 Argument constructor:"<<a<<endl; }
}
~A() Output:
{cout<<"Class A-Destructor"<<endl; } Class A-Zero Argument constructor
}; Class C-Zero Argument constructor
class C:public A Class A-1 Argument constructor:10
{ public: Class C-1 Argument constructor:20
C() Class C-Destructor
{cout<<"Class C-Zero Argument constructor"<<endl; Class A-Destructor
} Class C-Destructor
C(int a,int b) :A(a) Class A-Destructor
{cout<<"Class C-1 Argument
constructor:"<<b<<endl; }
Constructors in derived class
• When a class is declared, a constructor is also declared inside the class in
order to initialize data members
• It is not possible to use a single constructor for more classes
• Every class has its own constructor and destructor with a similar name as
the class
• When a class is derived from another class, it is possible to define a
constructor in the derived classes, and the data members of both base and
derive classes can be initialized
• It is not essential to declare a constructor in a base class
• Thus, the constructor of the derived class works for its base class; such
constructors are called constructors in the derived class or common
constructors
Constructors in derived class
#include <iostream> B b;
using namespace std; b.print();
class A return 0;
{ }
protected: Output:
int x,y; 123
};
class B : public A
{
public:
int z;
B()
{ x=1;
y=2;
z=3;
}
void print()
{ cout<<x<<" "<<y<<" "<<z;
}
};
int main( ) {
Function Overloading
#include <iostream>
using namespace std;
• Function overloading is a class Shape{
public:
concept of defining two void area(int side)
or more functions in the {
same class with the same cout<<"Area of Square : "<< side*side<<endl;
}
name but differed by void area(int len, int width)
number of arguments {
cout<<"Area of Rectangle : "<< len * width<<endl;
number or type of }
arguments. };
int main() {
Shape obj;
obj.area(10);
obj.area(5,10);
return 0;
}
Output:
Area of Square : 100
Area of Rectangle : 50
#include <iostream>
Function Overriding using namespace std;
class Shape{
public:
void display()
• Function overriding is a {
concept using which we cout<<" I am base class";
}
define two functions with
the same name and };
same parameters with a class Rectangle: public Shape{
public:
condition that one void display()
function must present in {
cout<<"I am overriding display function of parent class";
a base class and other }
function in a derived };
class. int main() {
Rectangle obj;
obj.Shape::display();
obj.display();
return 0;
}
Output:
I am overriding display function of parent class
Overloading vs Overriding
Function Overloading Function Overriding
Functions have same name but different Functions have same name ,same
number or different type of parameters. number and same type of parameters.
Overloading of the functions take place Overriding of the functions take place at
at compile time. run time.
It is also known as compile time It is also known as run time
polymorphism. polymorphism.
The functions that are overloaded are The functions that are overridden are
present in same class. present in different class.
It can occur without inheritance. It cannot occur without inheritance.
Functions can have different data types. Functions should have same data types.