INTRODUCTION
 Inheritance is the most important feature of OOP. It is
a form of a program re-usability in which new classes
are made by using code of existing classes.
Program/Software re-usability saves times of
programmer in program development.
 The word “inherit” means “to receive”.
Definition
 Inheritance is the process of creating new classes,
called “derived class” from existing classes called “base
classes”.
 The base class is also known as super class or parent
class while derived class is also known as sub class or
child class.
LOC of base class
LOC of derived
class
Figure Explained…
 The above figure shows the relationship between a
derived class and the base class. The arrow is drawn
from the derived class to the base class. The direction
of the arrow indicates that the derived class can access
members of the base class but base class cannot access
members of its derived class.
 The derived class inherits all the capabilities of the
base class. It means that it can use the data members
and member function of its base class. It can also have
its own data members and member functions.
Cont…
 So the derived class can be larger than its base class.
 Each derived class it self becomes a candidate to be a
base class for some future derived classes.
Categories Of Inheritance
 A new class can be derived from one or more existing
classes. Therefore inheritance is further divided into
different categories given below:
1) Single Inheritance
2) Multiple Inheritance
3) Multilevel Inheritance
Single Inheritance
 With single inheritance, a new class is derived from
one base class. It is very simple and straight forward
process.
 For example: A new class B is derived from base class
A. The derived class inherits all the attributes(data
members) and operations (member function) of the
base class.
Member 1
Member 2
Member 1
Member 2
Member 3
Member 4
Class A
Class B
Explanation
 In figure, the derived class has two members of its own
and the members shown with dark shadow represents
the members of the base class. The derived class can
also access these two members of the base class. Thus
an object of the base class can access only two
members, while an object of the derived class can
access four members (two members of the base class
and tow of the derived class)
Defining Derived Class with Single
Inheritance
 A derived class is defined in the similar way as base
class is defined. The syntax for defining a derived class
is slightly different from syntax of base class definition.
 The general syntax for defining a derived class is:
class derived_class_name : access_specifier base_class
{
Body of derived class
}
Example:
#include<iostream>
using namespace std;
class std_address //base class
{
private:
int code;
string name, phone_no;
public:
void input();
void display();
};
// derived class
class std_result : public std_address
{
private:
float sub1, sub2,sub3, average ,
total;
public:
void input_marks();
void display_marks();
};
Cont…
//Defining Member Functions of Base
Class
void std_address :: input()
{
cout<<“Enter code of student”<<endl;
cin>>code;
cout<<“Enter name of student”<<endl;
cin>>name;
cout<<“Enter phone of student”<<endl;
cin>>phone_no;
}
void std_address :: display()
{
cout<<“Code : “<<code<<endl;
cout<<“Name : “<<name<<endl;
cout<<“Phone : “<<phone_no<<endl;
}
//Defining Member Functions of Derived Class
void std_result :: input_marks();
{
cout<<“Enter marks of first subject?”<<endl;
cin>>sub1;
cout<<“Enter marks of second subject?”<<endl;
cin>>sub2;
cout<<“Enter marks of third subject?”<<endl;
cin>>sub3;
total = sub1+sub2+sub3;
Average = total/3;
}
void std_result :: display_marks()
{
cout<<“Marks of first subject: “<<sub1;
cout<<“Marks of second subject:“<<sub2;
cout<<“Marks of third subject: “<<sub3;
cout<<“Total Marks :“<<total;
cout<<“Average marks : “<<average;
}
//main function
int main()
{
std_result res;
res.input();
res.input_marks();
res.display();
res.display_marks();
}
Multiple Inheritance
 A new class can be derived from more than one base class
(or multiple base classes).
 This type of inheritance is called multiple inheritance.
 In multiple inheritance, the derived class inherits members
of all base classes. It can also have its own data members.
 Multiple inheritance reduces the program size and saves
program development time in single inheritance.
 Multiple inheritance is complex than single inheritance.
Some object oriented programming language do not
support this feature but C++ support this feature.
Diagrammatic Representation
Class A
Class C
Class B
Example
#include<iostream>
using namespace std;
class std_address
{
private:
string name, city;
public:
void input();
void print();
};
class std_marks
{
private:
float sub1, sub2, sub3, average, total;
public:
void input_marks()
void print_marks()
};
class std_result : public std_address, public std_marks
{
public:
void display()
{
cout<<“COMPLETE RECORD”<<endl;
print();
print_marks();
}
};
int main()
{
std_result res;
res.input();
res.input_marks();
res.display();
}
void std_address:: input()
{
cout<<“Enter student name”<<endl;
cin>>name;
cout<<“Enter name of city”<<endl;
cin>>city;
void std_address :: print()
{
cout<<“Name: “<<name<<endl;
cout<<“City: “<<city<<endl;
}
void std_marks :: input_marks()
{
cout<<“Enter marks of first subject”<<endl;
cin>>sub1;
cout<<“Enter marks of second subject”<<endl;
cin>>sub2;
cout<<“Enter marks of third subject”<<endl;
cin>>sub3;
total = sub1+sub2+sub3;
average = total/3;
}
void std_marks :: display()
{
cout<<“Marks of first subject: “<<sub1<<endl;
cout<<“Marks of second subject: “<<sub2<<endl;
cout<<“Marks of third subject: “<<sub3<<endl;
cout<<“Total marks: “<<total<<endl;
cout<<“Average marks:” <<average<<endl;
}
Multilevel Inheritance
 A class can be derived from another derived class. The
type of inheritance is called multilevel inheritance. For
example if a class “C” is derived from base class “B” and
a new class “D” is derived from derived class C. Then
the derived class D will inherit all the capabilities of C
and B.
Syntax
class A
{
body of class A;
};
class B : public A
{
body of derived class B;
};
class C : public B
{
body of derived class C;
};
Example
#include<iostream>
class values
{
private:
float m, n;
public:
float assign(float x, float y)
{
m = x;
n = y;
}
};
class sum : public values
{
public:
float sum()
{
cout<<“sum of”<<m<<“and”<<n<<“=“<<m+n;
}
};
class product : public sum
{
float pro()
{
cout<<“product of”<<m<<“and”<<n<<“=“<<m*n;
}
};
main()
{
product x;
x.assign(24,45);
x.sum();
x.pro();
}
Types of inheritance
The accessibility of members of the base class can be
controlled by using private, public and protected
Specifier. Thus, the inheritance can be divide on the
basis of accessing members of the base class.
Therefore, types of inheritance are:
 Private Inheritance
 Public Inheritance
 Protected Inheritance
Private Inheritance
 With private inheritance public and protected
members of the base class become the private
members of the derived class. The public and
protected members of the base class can only be
accessed inside the derived class but private members
of base class cannot be accessed inside the derived
class.
Public Inheritance
 With public inheritance public members of the base
class become the public members of derived class.
Similarly, protected members of the base class
becomes the protected members of the derived class.
Protected Inheritance
 The protected inheritance is similar to private
inheritance. It is rarely used. With protected
inheritance, public and protected members of the base
class become the protected member of derived class.
The object of derived class can access only protected
members of base class.

Inheritance

  • 2.
    INTRODUCTION  Inheritance isthe most important feature of OOP. It is a form of a program re-usability in which new classes are made by using code of existing classes. Program/Software re-usability saves times of programmer in program development.  The word “inherit” means “to receive”.
  • 3.
    Definition  Inheritance isthe process of creating new classes, called “derived class” from existing classes called “base classes”.  The base class is also known as super class or parent class while derived class is also known as sub class or child class.
  • 4.
    LOC of baseclass LOC of derived class
  • 5.
    Figure Explained…  Theabove figure shows the relationship between a derived class and the base class. The arrow is drawn from the derived class to the base class. The direction of the arrow indicates that the derived class can access members of the base class but base class cannot access members of its derived class.  The derived class inherits all the capabilities of the base class. It means that it can use the data members and member function of its base class. It can also have its own data members and member functions.
  • 6.
    Cont…  So thederived class can be larger than its base class.  Each derived class it self becomes a candidate to be a base class for some future derived classes.
  • 7.
    Categories Of Inheritance A new class can be derived from one or more existing classes. Therefore inheritance is further divided into different categories given below: 1) Single Inheritance 2) Multiple Inheritance 3) Multilevel Inheritance
  • 8.
    Single Inheritance  Withsingle inheritance, a new class is derived from one base class. It is very simple and straight forward process.  For example: A new class B is derived from base class A. The derived class inherits all the attributes(data members) and operations (member function) of the base class.
  • 9.
    Member 1 Member 2 Member1 Member 2 Member 3 Member 4 Class A Class B
  • 10.
    Explanation  In figure,the derived class has two members of its own and the members shown with dark shadow represents the members of the base class. The derived class can also access these two members of the base class. Thus an object of the base class can access only two members, while an object of the derived class can access four members (two members of the base class and tow of the derived class)
  • 11.
    Defining Derived Classwith Single Inheritance  A derived class is defined in the similar way as base class is defined. The syntax for defining a derived class is slightly different from syntax of base class definition.  The general syntax for defining a derived class is: class derived_class_name : access_specifier base_class { Body of derived class }
  • 12.
    Example: #include<iostream> using namespace std; classstd_address //base class { private: int code; string name, phone_no; public: void input(); void display(); }; // derived class class std_result : public std_address { private: float sub1, sub2,sub3, average , total; public: void input_marks(); void display_marks(); };
  • 13.
    Cont… //Defining Member Functionsof Base Class void std_address :: input() { cout<<“Enter code of student”<<endl; cin>>code; cout<<“Enter name of student”<<endl; cin>>name; cout<<“Enter phone of student”<<endl; cin>>phone_no; } void std_address :: display() { cout<<“Code : “<<code<<endl; cout<<“Name : “<<name<<endl; cout<<“Phone : “<<phone_no<<endl; } //Defining Member Functions of Derived Class void std_result :: input_marks(); { cout<<“Enter marks of first subject?”<<endl; cin>>sub1; cout<<“Enter marks of second subject?”<<endl; cin>>sub2; cout<<“Enter marks of third subject?”<<endl; cin>>sub3; total = sub1+sub2+sub3; Average = total/3; }
  • 14.
    void std_result ::display_marks() { cout<<“Marks of first subject: “<<sub1; cout<<“Marks of second subject:“<<sub2; cout<<“Marks of third subject: “<<sub3; cout<<“Total Marks :“<<total; cout<<“Average marks : “<<average; } //main function int main() { std_result res; res.input(); res.input_marks(); res.display(); res.display_marks(); }
  • 15.
    Multiple Inheritance  Anew class can be derived from more than one base class (or multiple base classes).  This type of inheritance is called multiple inheritance.  In multiple inheritance, the derived class inherits members of all base classes. It can also have its own data members.  Multiple inheritance reduces the program size and saves program development time in single inheritance.  Multiple inheritance is complex than single inheritance. Some object oriented programming language do not support this feature but C++ support this feature.
  • 16.
  • 17.
    Example #include<iostream> using namespace std; classstd_address { private: string name, city; public: void input(); void print(); }; class std_marks { private: float sub1, sub2, sub3, average, total; public: void input_marks() void print_marks() }; class std_result : public std_address, public std_marks { public: void display() { cout<<“COMPLETE RECORD”<<endl; print(); print_marks(); } };
  • 18.
    int main() { std_result res; res.input(); res.input_marks(); res.display(); } voidstd_address:: input() { cout<<“Enter student name”<<endl; cin>>name; cout<<“Enter name of city”<<endl; cin>>city; void std_address :: print() { cout<<“Name: “<<name<<endl; cout<<“City: “<<city<<endl; } void std_marks :: input_marks() { cout<<“Enter marks of first subject”<<endl; cin>>sub1; cout<<“Enter marks of second subject”<<endl; cin>>sub2; cout<<“Enter marks of third subject”<<endl; cin>>sub3; total = sub1+sub2+sub3;
  • 19.
    average = total/3; } voidstd_marks :: display() { cout<<“Marks of first subject: “<<sub1<<endl; cout<<“Marks of second subject: “<<sub2<<endl; cout<<“Marks of third subject: “<<sub3<<endl; cout<<“Total marks: “<<total<<endl; cout<<“Average marks:” <<average<<endl; }
  • 20.
    Multilevel Inheritance  Aclass can be derived from another derived class. The type of inheritance is called multilevel inheritance. For example if a class “C” is derived from base class “B” and a new class “D” is derived from derived class C. Then the derived class D will inherit all the capabilities of C and B.
  • 21.
    Syntax class A { body ofclass A; }; class B : public A { body of derived class B; }; class C : public B { body of derived class C; };
  • 22.
    Example #include<iostream> class values { private: float m,n; public: float assign(float x, float y) { m = x; n = y; } }; class sum : public values { public: float sum() { cout<<“sum of”<<m<<“and”<<n<<“=“<<m+n; } }; class product : public sum { float pro() { cout<<“product of”<<m<<“and”<<n<<“=“<<m*n; } };
  • 23.
  • 25.
    Types of inheritance Theaccessibility of members of the base class can be controlled by using private, public and protected Specifier. Thus, the inheritance can be divide on the basis of accessing members of the base class. Therefore, types of inheritance are:  Private Inheritance  Public Inheritance  Protected Inheritance
  • 26.
    Private Inheritance  Withprivate inheritance public and protected members of the base class become the private members of the derived class. The public and protected members of the base class can only be accessed inside the derived class but private members of base class cannot be accessed inside the derived class.
  • 27.
    Public Inheritance  Withpublic inheritance public members of the base class become the public members of derived class. Similarly, protected members of the base class becomes the protected members of the derived class.
  • 28.
    Protected Inheritance  Theprotected inheritance is similar to private inheritance. It is rarely used. With protected inheritance, public and protected members of the base class become the protected member of derived class. The object of derived class can access only protected members of base class.