1. Amrit Kaur C++ as Object oriented Language
1
Chapter 7: C++ as Object Oriented Language
Contents
8.1 Inheritance .................................................................................................................................. 1
8.2 Declaring Derived Class............................................................................................................... 1
8.3 Types of Inheritance .................................................................................................................... 2
8.3.1 Single Inheritance ................................................................................................................. 2
8.3.2 Hierarchical Inheritance........................................................................................................ 4
8.3.3 Multilevel Inheritance.......................................................................................................... 6
8.3.4 Multiple Inheritance ............................................................................................................. 8
8.3.5 Hybrid Inheritance .............................................................................................................. 11
8.4 Ambiguities with Multiple Base Class......................................................................................... 14
8.5 Virtual Base Class....................................................................................................................... 15
8.1 Inheritance
Inheritance is the process of creating new classes from an existing class. The existing
class is called base class or super class and the new class is knows as derived class or
subclass.
It can also be defined as generalization of common attributes and behaviors in separate
classes (base class) from which various classes can be derived. Thus derived class has
larger set of attributes and behaviour as compared to base class.
Inheritance defines a relationship(Is-a) among classes.
Derived Class: The class that inherits or derives attributes and behaviour from another
class is known as Derived Class. The other names of derived class are sub class or child
class.
Base Class: The class from which derived class inherits the attributes and behaviour, is
known as base class. The other names of base class are super class or parent class.
8.2 Declaring Derived Class
The syntax of defining a derived class is as follows
class base_class_name
{
.....
};
2. Amrit Kaur C++ as Object oriented Language
2
class derived_class : base_class
{
.....
};
A class can inherit attributes and behaviour of more than one class. In this case, base
class declaration includes name of all the base classes separated by commas.
Example 1: Sample code showing inheritance in C++
8.3 Types of Inheritance
In C++, inheritance is classified into following forms
Single inheritance
Hierarchical Inheritance
Multilevel Inheritance
Multiple Inheritance
Hybrid Inheritance
8.3.1 Single Inheritance
When a one derived class inherit from a single base class the relationship is
known as Single Inheritance. The syntax is as follows
class base_class_name
{
.....
};
class derived_class : base_class
{
.....
};
Example 2: Sample Code illustrate single inheritance.
#include<conio.h>
#include<iostream.h>
class Person //BASE CLASS
{ protected:
char name[25];
int age;
char gender[5];
//member function
void input()
{
cout<<"n Enter Name";
cin>>name;
Class A
(Base Class)
Class B
(Derived Class)
3. Amrit Kaur C++ as Object oriented Language
3
cout<<"n ENter Age";
cin>>age;
cout<<"n Enter Gender";
cin>>gender;
}
//member function
void display()
{
cout<<"n Name="<<name;
cout<<"n Age="<<age;
cout<<"n Gender="<<gender;
}
}; //class ends
class Employee : Person //Employee DERIVED CLASS of Person
{
int empCode;
public:
void getData()
{ cout<<"n Employee IS Person n";
input(); //call to base class member function
cout<<"nEnter Employee Code";
cin>>empCode;
}
void showData()
{
cout<<"n Employee IS Personn";
display(); //call to base class member function
cout<<"n Employee Code";
}
};
void main()
{
clrscr();
Employee e;
cout<<"n Enter EMPLOYEE Details";
e.getData(); //call to all member function using dot operator
cout<<"n Display EMPLOYEE Details";
e.showData(); //call to all member function using dot operator
}
4. Amrit Kaur C++ as Object oriented Language
4
8.3.2 Hierarchical Inheritance
When multiple derived class inherit from a single base class the relationship is
known as hierarchical inheritance.
The syntax of hierarchical inheritance is
class base_class_name
{
.....
};
class derived_class1 : base_class
{
.....
};
class derived_class2 : base_class
{
.....
};
Example 3: Sample Code illustrate hierarchical inheritance
#include<conio.h>
#include<iostream.h>
class Person //BASE CLASS
{ protected:
char name[25];
int age;
char gender[5];
//member function
void input()
{
cout<<"n Enter Name";
cin>>name;
cout<<"n ENter Age";
cin>>age;
cout<<"n Enter Gender";
cin>>gender;
}
Person
(Base Class)
Student
(Derived Class)
Sports Man
(Derived Class)
Employee
(Derived Class)
5. Amrit Kaur C++ as Object oriented Language
5
//member function
void display()
{
cout<<"n Name="<<name;
cout<<"n Age="<<age;
cout<<"n Gender="<<gender;
}
}; //class ends
class Employee : Person //Employee 1st DERIVED CLASS of Base
{
int empCode;
public:
void getData()
{
cout<<"n Employee IS Person n";
input(); //call to base class member function
cout<<"nEnter Employee Code";
cin>>empCode;
}
void showData()
{
cout<<"n Employee IS Personn";
display(); //call to base class member function
cout<<"n Employee Code";
}
};
class Student : Person //Student 2nd DERIVED CLASS of Person
{
int rollno;
char branch[3];
char sem[4];
public:
void getData()
{
cout<<"n Student IS Person";
input(); //call to base class member function
cout<<"n Enter Roll Number";
cin>>rollno;
cout<<"n Enter Branch";
cin>>branch;
cout<<"n Enter Semester";
cin>>sem;
}
void showData()
{
cout<<"n Student IS Person";
display(); //call to base class function
cout<<"n Roll No :: "<<rollno;
cout<<"n Branch :: "<<branch;
cout<<"n Semester:: "<<sem;
6. Amrit Kaur C++ as Object oriented Language
6
}
} ;
void main()
{ clrscr();
Employee e;
cout<<"n Enter EMPLOYEE Details";
e.getData(); //call to all member function using dot operator
cout<<"n Display EMPLOYEE Details";
e.showData(); //call to all member function using dot operator
cout<<"nnn Press ANY key to CONTINUEn";
getch();
Student s;
cout<<"n Enter STUDENT Details";
s.getData(); //call to all member function using dot operator
cout<<"n Display STUDENT Details";
s.showData(); //call to all member function using dot operator
}
8.3.3 Multilevel Inheritance
When derived class inherit from a one base class and another class inherits from
the derived class, the relationship is known as multilevel inheritance.Thats is base
class of one is derived class of another.
The syntax of multilevel inheritance is
class base_class_name
{
.....
};
class derived_class : base_class
{
.....
};
class Derived_derived_class : derived_class
{
.....
};
Example 4: Sample Code illustrate multilevel inheritance
#include<conio.h>
#include<iostream.h>
class Person //BASE CLASS
{ protected:
//data members
char name[25];
int age;
Person
(Base Class)
Manager
(Derived Class of Employee)
Employee
(Derived Class of Person)
(Base Class of Manger)
7. Amrit Kaur C++ as Object oriented Language
7
char gender[5];
//member function
void input()
{
cout<<"n Enter Name";
cin>>name;
cout<<"n ENter Age";
cin>>age;
cout<<"n Enter Gender";
cin>>gender;
}
//member function
void display()
{
cout<<"n Name="<<name;
cout<<"n Age="<<age;
cout<<"n Gender="<<gender;
}
}; //Person class ends
class Employee : Person // Derived Class of PERSON, BASE Class of Manager
{
int empCode;
public:
void getData()
{ cout<<"n Employee IS Person n";
input(); //call to base class member function
cout<<"nEnter Employee Code";
cin>>empCode;
}
void showData()
{
cout<<"n Employee IS Personn";
display(); //call to base class member function
cout<<"n Employee Code";
}
}; //Employee Class Ends
class Manager : Employee //DERIVED CLASS of Employee
{
char department[15];
public:
void inputData()
{
cout<<"n Manager IS Employee";
getData(); //call to base class member function
cout<<"n Enter Department";
cin>>department;
}
void displayData()
{
8. Amrit Kaur C++ as Object oriented Language
8
cout<<"n MANAGER IS Employee";
showData(); //call to base class function
cout<<"n Department :: "<<department;
}
} ;
void main()
{ clrscr();
Manager m;
cout<<"n**** Enter Manager Details ****";
m.inputData(); //call to all member function using dot operator
cout<<"n**** Display Manager Details";
m.displayData(); //call to all member function using dot operator
}
8.3.4 Multiple Inheritance
When derived class inherits from two or more base class, the relationship is
known as Multiple Inheritance.
The syntax of multiple inheritance is
class base_class1
{
.....
};
class base_class2
{
.....
};
class derived_class : base_class1, baseclass2
{
.....
};
Example 5: Sample Code to illustrate Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class ScienceCourse //BASE CLASS
{
protected:
//data members
int courseid ;
char coursename[20];
char paper1[20];
char paper2[20];
char paper3[20];
ScienceCourse
(Base Class)
ComputerCourse
(Base Class)
BE_CS_Course
(Derived Class)
9. Amrit Kaur C++ as Object oriented Language
9
//member function
void inputS()
{
cout<<"n Enter Course Id";
cin>>courseid;
cout<<"n Enter Course Name";
cin>>coursename;
cout<<"n Enter Paper 1";
cin>>paper1;
cout<<"n Enter Paper 2";
cin>>paper2;
cout<<"n ENter Paper 3";
cin>>paper3;
}
//member function
void displayS()
{
cout<<"nCourse Code ::"<<courseid;
cout<<"t Course Name ::"<<coursename;
cout<<"n Paper 1 ::"<<paper1;
cout<<"n Paper 2 ::"<<paper2;
cout<<"n Paper 3 ::"<<paper3;
}
};
class ComputerCourse //SECOND BASE CLASS
{
protected:
//data members
int courseId ;
char courseName[20];
char p1[20];
char p2[20];
char p3[20];
//member function
void inputC()
{
cout<<"n Enter Course Id";
cin>>courseId;
cout<<"n Enter Course Name";
cin>>courseName;
cout<<"n Enter Paper 1";
cin>>p1;
cout<<"n Enter Paper 2";
10. Amrit Kaur C++ as Object oriented Language
10
cin>>p2;
cout<<"n ENter Paper 3";
cin>>p3;
}
//member function
void displayC()
{
cout<<"nCourse Code ::"<<courseId;
cout<<"t Course Name ::"<<courseName;
cout<<"n Paper 1 ::"<<p1;
cout<<"n Paper 2 ::"<<p2;
cout<<"n Paper 3 ::"<<p3;
}
};
//MULTIPLE INHERITANCE - 2 base class 1 derived class
class BE_CS_Course : ScienceCourse, ComputerCourse
{ //data members
char pp1[20];
char pp2[20];
public:
void inputCS()
{
cout<<"n BE CS include core Science Subject";
inputS(); //call to member function of ScienceCourse
getch();
cout<<"n BE CS include core Computer Subject";
inputC(); //call to member function of ComputerCourse
getch();
cout<<"n BE CS specific Subject ";
cout<<"n Enter Paper 1";
cin>>pp1;
cout<<"n Enter Paper 2";
cin>>pp2;
}
void displayCS()
{ clrscr();
cout<<"n n *** BE COURSE CURRICULUM ***nn";
cout<<"n Core Science Subjectsn";
displayS(); //call to member function of ScienceCourse
cout<<"nn Core Computer Subjectsn";
displayC(); //call to member function of ComputerCourse
cout<<"nn BE Specific Subjectn";
11. Amrit Kaur C++ as Object oriented Language
11
cout<<"n Paper 1 ::"<<pp1;
cout<<"n Paper 2 ::"<<pp2;
}
};
void main()
{
clrscr();
BE_CS_Course be; //DERIVED CLASS object
be.inputCS(); //call to member function
be.displayCS(); //call to member function
}
8.3.5 Hybrid Inheritance
Deriving a new class from multiple base classes. This involves combination of
more than one form of inheritance such as multilevel, hierarchical and multiple
inheritance.
The syntax of multiple inheritance is
class base_class1
{
.....
};
class base_class2
{
.....
};
class derived_class : base_class1, baseclass2
{
.....
};
Example 6: Sample Code to illustrate Hybrid Inheritance
#include<iostream.h>
#include<conio.h>
class Course //BASE CLASS
{
protected:
//data members
int courseid ;
char coursename[20];
//member function
ScienceCourse
(Base Class)
ComputerCourse
(Base Class)
BE_CS_Course
(Derived Class)
Course
12. Amrit Kaur C++ as Object oriented Language
12
void inputCourse()
{
cout<<"n Enter Course Id";
cin>>courseid;
cout<<"n Enter Course Name";
cin>>coursename;
}
//member function
void displayCourse()
{ cout<<"nCourse Code ::"<<courseid;
cout<<"t Course Name ::"<<coursename;
}
};
//Derived Class of Course
//BASE CLASS of BE_CS_Course
class ScienceCourse: Course
{
protected:
//data member
char paper1[20];
char paper2[20];
char paper3[20];
//member function
void inputS()
{
cout<<"n Course Details";
inputCourse();
cout<<"n Enter Paper 1";
cin>>paper1;
cout<<"n Enter Paper 2";
cin>>paper2;
cout<<"n ENter Paper 3";
cin>>paper3;
}
//member function
void displayS()
{ cout<<"n Course Details";
displayCourse();
cout<<"n Paper 1 ::"<<paper1;
cout<<"n Paper 2 ::"<<paper2;
cout<<"n Paper 3 ::"<<paper3;
}
};
13. Amrit Kaur C++ as Object oriented Language
13
//Derived Class of Course
//BASE CLASS of BE_CS_Course
class ComputerCourse: Course
{
protected:
//data members
char p1[20];
char p2[20];
char p3[20];
//member function
void inputC()
{
cout<<"n Enter Paper 1";
cin>>p1;
cout<<"n Enter Paper 2";
cin>>p2;
cout<<"n ENter Paper 3";
cin>>p3;
}
//member function
void displayC()
{
cout<<"n Paper 1 ::"<<p1;
cout<<"n Paper 2 ::"<<p2;
cout<<"n Paper 3 ::"<<p3;
}
};
//Hybrid Inheritance- 2 base class 1 derived class... base class derived from another class
class BE_CS_Course : ScienceCourse, ComputerCourse
{ //data members
char pp1[20];
char pp2[20];
public:
void inputCS()
{ cout<<"n BE CS include core Science Subject";
inputS(); //call to member function of ScienceCourse
getch();
cout<<"n BE CS include core Computer Subject";
inputC(); //call to member function of ComputerCourse
getch();
cout<<"n BE CS specific Subject ";
cout<<"n Enter Paper 1";
cin>>pp1;
cout<<"n Enter Paper 2";
14. Amrit Kaur C++ as Object oriented Language
14
cin>>pp2;
}
void displayCS()
{ clrscr();
cout<<"n n *** BE COURSE CURRICULUM ***nn";
cout<<"n Core Science Subjectsn";
displayS(); //call to member function of ScienceCourse
cout<<"nn Core Computer Subjectsn";
displayC(); //call to member function of ComputerCourse
cout<<"nn BE Specific Subjectn";
cout<<"n Paper 1 ::"<<pp1;
cout<<"n Paper 2 ::"<<pp2;
}
};
void main()
{
clrscr();
BE_CS_Course be; //DERIVED CLASS object
be.inputCS(); //call to member function
be.displayCS(); //call to member function
}
8.4 Ambiguities with Multiple Base Class
Ambiguity in multiple inheritance and hybrid inheritance arise when the derived class
hase multiple copies of same base class.
In figure, class Derived inherits attributes of two
base classes Base1 and Base 2. Class Base1 and
Base2 inherits attributes from SuperBase. As a
result, class Derived will have two copies of class
SuperBase. The code below illustrate ambiguity
#include<iostream.h>
#include<conio.h>
class SuperBase
{ protected:
void display()
{ cout<<"nSUPER BASE CLASS n"; }
};
class Base1: SuperBase
{ protected:
void displayB1()
class Base1
(Base Class)
class Base2
(Base Class)
class Derived
(Derived Class)
class SuperBase
(Base Class)
15. Amrit Kaur C++ as Object oriented Language
15
{ cout<<"n Inside BASE 1 class"; }
};
class Base2: SuperBase
{ protected:
void displayB2()
{ cout<<"n Inside BASE 2 class"; }
};
class Derived: Base1, Base2
{ public:
void displayD()
{
display(); //ERROR! Ambiguity(2 copies exist) inherited by both Base1 and Base2 ...
displayB1();
displayB2();
cout<<"n Inside Derived Class";
}
};
void main()
{ clrscr();
Derived d;
d.displayD();
}
The above program will display error because display() is ambiguious. This ambiguity is
caused because class Derived inherits one copy of display() through Base1 and another
through Base2.
The ambiguity can be solved by using VIRTUAL BASE Class.
8.5 Virtual Base Class
The main idea behind virtual base class is to have only one copy of base class data
members and member function in memory. When a class inherit from more than one
base class which in turn inherit from another class creates multiple copies of base class
members in memory. By declaring base class inheritance as virtual, only one copy of
base class is inherited. This is done by using virtual qualifier.
Example 2: Sample Code illustrate virtual base class.
#include<iostream.h>
#include<conio.h>
class SuperBase
{ protected:
void display()
{ cout<<"nSUPER BASE CLASS n"; }
};
16. Amrit Kaur C++ as Object oriented Language
16
class Base1: virtual public SuperBase
{ protected:
void displayB1()
{ cout<<"n Inside BASE 1 class"; }
};
class Base2: virtual SuperBase
{ protected:
void displayB2()
{ cout<<"n Inside BASE 2 class"; }
};
class Derived: Base1, Base2
{ public:
void displayD()
{
display(); //OK! Because only one copy is maintained.
displayB1();
displayB2();
cout<<"n Inside Derived Class";
}
};
void main()
{ clrscr();
Derived d;
d.displayD();
}