SlideShare a Scribd company logo
1 of 16
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();
}

More Related Content

What's hot

What's hot (20)

Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
Array in c++
Array in c++Array in c++
Array in c++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Java arrays
Java arraysJava arrays
Java arrays
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Manipulators
ManipulatorsManipulators
Manipulators
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 

Viewers also liked

7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in plAmrit Kaur
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOPAmrit Kaur
 
Chapter 6 OOPS Concept
Chapter 6 OOPS ConceptChapter 6 OOPS Concept
Chapter 6 OOPS ConceptAmrit Kaur
 
8. transactions
8. transactions8. transactions
8. transactionsAmrit Kaur
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architectureAmrit 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 databaseAmrit Kaur
 
9. index and index organized table
9. index and index organized table9. index and index organized table
9. index and index organized tableAmrit Kaur
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functionsAmrit Kaur
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATEAmrit Kaur
 
Security and Viruses
Security and VirusesSecurity and Viruses
Security and VirusesAmrit Kaur
 
ComputerBasics
ComputerBasicsComputerBasics
ComputerBasicsAmrit Kaur
 
Lesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMSLesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMSAmrit Kaur
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive dataAmrit Kaur
 

Viewers also liked (19)

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

Similar to Chapter 8 Inheritance

Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharpSDFG5
 
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.pptAlmamoon
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.pptmothertheressa
 
Student DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmStudent DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmhome
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.pptpsundarau
 
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 GoElton 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 17Ana-Maria Mihalceanu
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpg_hemanth17
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Sachin Singh
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpRaga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpSatish Verma
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpsinghadarsh
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharpMody Farouk
 

Similar to Chapter 8 Inheritance (20)

Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Whats New In C# 3.0
Whats New In C# 3.0Whats New In C# 3.0
Whats New In C# 3.0
 
Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharp
 
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...
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
 
Student DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEmStudent DATABASE MANAGeMEnT SysTEm
Student DATABASE MANAGeMEnT SysTEm
 
IntroToCSharpcode.ppt
IntroToCSharpcode.pptIntroToCSharpcode.ppt
IntroToCSharpcode.ppt
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
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)!
 
Object Calisthenics em Go
Object Calisthenics em GoObject Calisthenics em Go
Object Calisthenics em Go
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
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
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
 

More from Amrit Kaur

File Organization
File OrganizationFile Organization
File OrganizationAmrit Kaur
 
Introduction to transaction processing
Introduction to transaction processingIntroduction to transaction processing
Introduction to transaction processingAmrit Kaur
 
Transaction Processing
Transaction ProcessingTransaction Processing
Transaction ProcessingAmrit Kaur
 
Sample Interview Question
Sample Interview QuestionSample Interview Question
Sample Interview QuestionAmrit 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
 

More from Amrit Kaur (9)

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

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