SlideShare a Scribd company logo
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
{
.....
};
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)
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
}
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)
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;
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)
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()
{
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)
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";
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";
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
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;
}
};
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";
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)
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"; }
};
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();
}
Ad

More Related Content

What's hot (20)

Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Friend function
Friend functionFriend function
Friend function
zindadili
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
NainaKhan28
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
C functions
C functionsC functions
C functions
University of Potsdam
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
Templates
TemplatesTemplates
Templates
Pranali Chaudhari
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
Rajab Ali
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
tanmaymodi4
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Friend function
Friend functionFriend function
Friend function
zindadili
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
NainaKhan28
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
Rajab Ali
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
tanmaymodi4
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 

Viewers also liked (19)

7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in pl
Amrit Kaur
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOP
Amrit Kaur
 
Chapter 6 OOPS Concept
Chapter 6 OOPS ConceptChapter 6 OOPS Concept
Chapter 6 OOPS Concept
Amrit Kaur
 
10. timestamp
10. timestamp10. timestamp
10. timestamp
Amrit Kaur
 
8. transactions
8. transactions8. transactions
8. transactions
Amrit Kaur
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architecture
Amrit Kaur
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
Amrit Kaur
 
9. index and index organized table
9. index and index organized table9. index and index organized table
9. index and index organized table
Amrit Kaur
 
6. triggers
6. triggers6. triggers
6. triggers
Amrit Kaur
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
Amrit Kaur
 
4. plsql
4. plsql4. plsql
4. plsql
Amrit Kaur
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE
Amrit Kaur
 
Security and Viruses
Security and VirusesSecurity and Viruses
Security and Viruses
Amrit Kaur
 
ComputerBasics
ComputerBasicsComputerBasics
ComputerBasics
Amrit Kaur
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
Amrit Kaur
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
Amrit Kaur
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
Amrit Kaur
 
Lesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMSLesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMS
Amrit Kaur
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 
7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in pl
Amrit Kaur
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOP
Amrit Kaur
 
Chapter 6 OOPS Concept
Chapter 6 OOPS ConceptChapter 6 OOPS Concept
Chapter 6 OOPS Concept
Amrit Kaur
 
8. transactions
8. transactions8. transactions
8. transactions
Amrit Kaur
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architecture
Amrit Kaur
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
Amrit Kaur
 
9. index and index organized table
9. index and index organized table9. index and index organized table
9. index and index organized table
Amrit Kaur
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
Amrit Kaur
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE
Amrit Kaur
 
Security and Viruses
Security and VirusesSecurity and Viruses
Security and Viruses
Amrit Kaur
 
ComputerBasics
ComputerBasicsComputerBasics
ComputerBasics
Amrit Kaur
 
Lesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMSLesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMS
Amrit Kaur
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 
Ad

Similar to Chapter 8 Inheritance (20)

Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
RashidFaridChishti
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
RutujaTandalwade
 
Whats New In C# 3.0
Whats New In C# 3.0Whats New In C# 3.0
Whats New In C# 3.0
Clint Edmonson
 
Chapter 6 and inheritance OOP C++ tu ioe
Chapter 6 and inheritance OOP C++ tu ioeChapter 6 and inheritance OOP C++ tu ioe
Chapter 6 and inheritance OOP C++ tu ioe
EZERR1
 
Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharp
SDFG5
 
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
NALESVPMEngg
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
Almamoon
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
mothertheressa
 
Introduction-to-Csharp programacion orientada a objetos
Introduction-to-Csharp programacion orientada a objetosIntroduction-to-Csharp programacion orientada a objetos
Introduction-to-Csharp programacion orientada a objetos
KilbertChusiHuamani
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
Student DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmStudent DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEm
home
 
Introduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritanceIntroduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritance
huzaifaakram12
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.ppt
psundarau
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
Ganesh Samarthyam
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
classes and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggfclasses and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!
Julien Truffaut
 
Object Calisthenics em Go
Object Calisthenics em GoObject Calisthenics em Go
Object Calisthenics em Go
Elton Minetto
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
Muraleedhar Sundararajan
 
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
Enhancing Productivity and Insight  A Tour of JDK Tools Progress Beyond Java 17Enhancing Productivity and Insight  A Tour of JDK Tools Progress Beyond Java 17
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
Ana-Maria Mihalceanu
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
RashidFaridChishti
 
Chapter 6 and inheritance OOP C++ tu ioe
Chapter 6 and inheritance OOP C++ tu ioeChapter 6 and inheritance OOP C++ tu ioe
Chapter 6 and inheritance OOP C++ tu ioe
EZERR1
 
Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharp
SDFG5
 
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
NALESVPMEngg
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
Almamoon
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
mothertheressa
 
Introduction-to-Csharp programacion orientada a objetos
Introduction-to-Csharp programacion orientada a objetosIntroduction-to-Csharp programacion orientada a objetos
Introduction-to-Csharp programacion orientada a objetos
KilbertChusiHuamani
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
Student DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmStudent DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEm
home
 
Introduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritanceIntroduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritance
huzaifaakram12
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.ppt
psundarau
 
classes and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggfclasses and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!
Julien Truffaut
 
Object Calisthenics em Go
Object Calisthenics em GoObject Calisthenics em Go
Object Calisthenics em Go
Elton Minetto
 
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
Enhancing Productivity and Insight  A Tour of JDK Tools Progress Beyond Java 17Enhancing Productivity and Insight  A Tour of JDK Tools Progress Beyond Java 17
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
Ana-Maria Mihalceanu
 
Ad

More from Amrit Kaur (9)

File Organization
File OrganizationFile Organization
File Organization
Amrit Kaur
 
Introduction to transaction processing
Introduction to transaction processingIntroduction to transaction processing
Introduction to transaction processing
Amrit Kaur
 
ER diagram
ER diagramER diagram
ER diagram
Amrit Kaur
 
Transaction Processing
Transaction ProcessingTransaction Processing
Transaction Processing
Amrit Kaur
 
Normalization
NormalizationNormalization
Normalization
Amrit Kaur
 
Sample Interview Question
Sample Interview QuestionSample Interview Question
Sample Interview Question
Amrit Kaur
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
Amrit Kaur
 
Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++
Amrit Kaur
 
C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
Amrit Kaur
 
File Organization
File OrganizationFile Organization
File Organization
Amrit Kaur
 
Introduction to transaction processing
Introduction to transaction processingIntroduction to transaction processing
Introduction to transaction processing
Amrit Kaur
 
Transaction Processing
Transaction ProcessingTransaction Processing
Transaction Processing
Amrit Kaur
 
Sample Interview Question
Sample Interview QuestionSample Interview Question
Sample Interview Question
Amrit Kaur
 
Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++
Amrit Kaur
 

Chapter 8 Inheritance

  • 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(); }