SlideShare a Scribd company logo
6
Most read
10
Most read
13
Most read
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
1 | P a g e
Chapter-10
INHERITANCE
 Introduction:
 Inheritance is another important aspect of object oriented programming.
 C++ allows the user to create a new class (derived class) from an existing class (base class).
 Inheritance:
 Inheritance is the capability of one class to inherit properties from another class.
 Base Class: It is the class whose properties are inherited by another class. It is also called Super
class.
 Derived Class: It is the class that inherits the properties from base class. It is also called Sub
class.
 Need of Inheritance:
 Suppose X is a class already defined and we need to redefine another class Y has same properties
of X and in addition its own.
 Suppose if we use direct option without using inheritance, it has following problems.
o Code written in X is repeated again in Y which leads to unnecessary wastage of memory.
o Testing to be done separately for both class X and class Y leads to waste of time.
 The above problem can be solved by using the concept of inheritance.
 If we use the code of X even in Y without rewriting it. The class Y inherits all the properties of X.
 The class X is called base class and the class Y is called derived class.
 The main advantages of Inheritance are:
o Reusing existing code
o Faster development time
o Easy to maintain
o Easy to extend
o Memory Utilization
 The main disadvantage of Inheritance are:
o Inappropriate use of inheritance makes programs more complicated.
o Calling member functions using objects creates more compiler overheads.
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
2 | P a g e
 Defining Derived Classes:
 A derived class is a composite class – it inherits members from the base class and adds member of
its own.
 The general form of the derived class is given below.
IMAGE
 Here,
o class  Keyword
o derived_class_name  Name of the derived class
o :  Shows the derivation from the base class.
o Visibility Mode  Specifies the type of derivation
o base_class_name  Name of the base class.
o The use of a constructor can be cleverly done especially in those problems where it is
necessary to initialize certain data members compulsorily.
 Example:
Public Derived Class Private Derived Class Protected Derived Class
class father //Base class
{
private:
char name[10];
public:
char caste[10];
int age;
void readdata( );
};
class son : public father
{
private:
char gender[5];
public:
void display( );
};
class father //Base class
{
private:
char name[10];
public:
char caste[10];
int age;
void readdata( );
};
class son : private father
{
private:
char gender[5];
public:
void display( );
};
class father //Base class
{
private:
char name[10];
public:
char caste[10];
int age;
void readdata( );
};
class son : protected father
{
private:
char gender[5];
public:
void display( );
};
 Visibility mode:
 Visibility mode can be public, private or protected. The private data of base class cannot be
inherited.
o public: If inheritance is done in public mode, public members of the base class become
the public member of derived class and protected member of base class become the
protected member of derived class..
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
3 | P a g e
o private: If inheritance is done in a private mode, public and protected members of base
class become the private members of derived class.
o protected: If inheritance is done in a protected mode, public and protected members of
base class become the protected members of the base class.
Derived Class
Visibility Mode public private protected
Base class
public public private protected
private Not inherited Not inherited Not inherited
protected protected private protected
 Public Inheritance:
 When a base class is inherited as public, all public members of the base class become public
members of derived class.
 The private members of the base class remain private to that class, and are nor accessible by
members of the derived class.
 Example: A program illustrates public inheritance.
#include<iostream.h>
#include<conio.h>
class shape //Base Class
{
public:
int side1, side2;
};
class rectangle : public shape //Derived Class
{
public:
int area;
void compute( )
{
area = side1 * side2;
}
};
void main( )
{
rectangle R; // R is the object of derived class
R.side1 = 5; // Data directly accessed by object
R.side2 = 6;
R.compute( );
cout<< “Area of the rectangle = “ <<R.area;
}
Important
5 Marks
OUTPUT:
Area of the rectangle = 30
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
4 | P a g e
 Private Inheritance:
 When a base class is inherited as private, then all public and protected members of the base class
become private members of derived class.
 This means that they are still accessible by member of the derived class, but cannot be accessed by
other parts of the program.
 Example: A program illustrates private inheritance.
#include<iostream.h>
#include<conio.h>
class shape //Base Class
{
protected:
int side1, side2;
public:
int area;
void compute( )
{
area = side1 * side2;
}
};
class rectangle : private shape //Derived Class
{
public:
void readdata( )
{
cout << “ Enter the input first side :”;
cin>>side1;
cout << “ Enter the input second side :”;
cin>>side2;
}
void display( )
{
compute( ); // Calling base class
cout<< “Area of the rectangle = “ <<area;
}
};
void main( )
{
rectangle R;
R.readdata( );
R.display( );
}
OUTPUT:
Enter the input first side : 9
Enter the input second side : 5
Area of the rectangle = 45
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
5 | P a g e
 Protected Inheritance:
 When a base class is inherited as protected, then all public and protected members of the base class
become protected members of derived class.
 The private data members of base class are not visible to derived class.
 They can only be accessed through public and protected member functions of base class.
 Example: A program illustrates protected inheritance with the base class having protected and
public members.
#include<iostream.h>
#include<conio.h>
class shape //Base Class
{
protected:
int side1, side2;
public:
int compute( )
{
return(side1 * side2);
}
};
class rectangle : protected shape //Derived Class
{
public:
void readdata( )
{
cout << “ Enter the input first side :”;
cin>>side1;
cout << “ Enter the input second side :”;
cin>>side2;
}
void display( )
{
cout<< “Area of the rectangle = “ <<compute( );
}
};
void main( )
{
rectangle R;
R.readdata( );
R.display( );
}
OUTPUT:
Enter the input first side : 7
Enter the input second side : 8
Area of the rectangle = 56
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
6 | P a g e
 Types of Inheritance:
 Based on the relationship, inheritance can be classified into five forms:
o Single Inheritance
o Multilevel Inheritance
o Multiple Inheritance
o Hierarchical Inheritance
o Hybrid Inheritance
 Single Inheritance:
 Single Inheritance is the process of creating a new class from existing class base class.
 It is very coomon in inheritance that a class is derived from the base class.
 The data members and memberfunction of the base class are data member and member function
of the derived class.
 A derived class with single inheritance is declared as follows:
class Base_Class
{
………..
};
class Derived_class : public Base_calss
{
………..
};
 Example: Program to illustrate single level inheritance.
#include<iostream.h>
#include<conio.h>
class base
{
private:
int rollno;
char name[10];
public:
void read( )
Important
5 Marks
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
7 | P a g e
{
cout << “ Enter Roll Number and Name “<<endl;
cin >> rollno >> name;
}
void display( )
{
cout << “ Roll No : “ << rollno <<endl;
cout << “ Name : “ << name <<endl;
}
};
class derived : public base
{
private:
int m1, m2, t;
public:
void read1( )
{
cout << “ Enter Maths and Computer marks “<<endl;
cin >> m1 >> m2;
t = m1 + m2;
}
void display1( )
{
cout << “ Maths : “ << m1 <<endl;
cout << “ Computer : “ << m2 <<endl;
cout << “Total Marks : “ << t <<endl;
}
};
void main( )
{
derived obj;
clrscr( );
obj.read( );
obj.read1( );
obj.display( );
obj.display1( );
getch( );
}
 Multilevel Inheritance:
 Derivation of a class from another derived class is called multilevel inheritance.
 In the figure class A is the base class for class AB and class AB is the base class for class ABC.
 The class AB provides a link for the inheritance between A and ABC, and is known as
OUTPUT:
Enter Roll Number and Name
1234 RAM
Enter Maths and Computer marks
80 90
Roll No : 1234
Name : RAM
Maths : 80
Computer : 90
Total Marks : 170
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
8 | P a g e
intermedidate base class.
 A derived class with multilevel inheritance is declared as follows:
class A
{
………..
};
class AB : public A
{
………..
};
class ABC : public AB
{
………..
};
 Example: Program to illustrate multilevel inheritance.
#include<iostream.h>
#include<conio.h>
class A
{
public:
void displayA( )
{
cout << “ Base class A”<<endl;
}
};
class AB : public A
{
public:
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
9 | P a g e
void displayAB( )
{
cout << “ Intermediate Base class AB”<<endl;
cout << “ Derived from A” << endl;
}
};
class ABC : public AB
{
public:
void displayABC( )
{
cout << “ Derived Class ABC”<<endl;
cout << “ Derived from AB” << endl;
}
void output( )
{
displayA( );
displayAB( );
displayABC( );
};
void main( )
{
ABC obj;
clrscr( );
obj.output( );
getch( );
}
 Multiple Inheritance:
 A class can be derived from more than one base calss is known as multiple inheritance.
 A derived class with multiple inheritance is declared as follows:
class A //Base Class A
{
………..
};
OUTPUT:
Base Class A
Intermediate Base Class AB
Derived from A
Derived Class ABC
Derived from AB
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
10 | P a g e
class B //Base Class B
{
………..
};
class C //Base Class C
{
………..
};
class Derived_Class : public A, private B, protected C
{
……….. //Members of derived class
};
 Hierarchical Inheritance:
 If a number of classes are derived from a single base class, it is called as hierarchical
inheritance.
 Hierarchical model exhibits top down approach by breaking up a complex class into simpler class.
 Hybrid Inheritance:
 Hybrid Inheritance is combination of Hierarchical and multilevel inheritance.
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
11 | P a g e
 Virtual Base Classes:
 When two or more objects are derived from a common base class, we can prevent multiple copies
of the base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited.
 Such a base class is known as virtual base class.
 This can be achieved by preceding the base class name with the word virtual.
 Example:
Class A
{
---------------------;
---------------------;
};
class B : virtual public A
{
---------------------;
---------------------;
};
class C : virtual public A
{
---------------------;
---------------------;
};
class D : public B, public C
{
---------------------;
---------------------;
};
 Abstract Class:
 An abstract class is one that is not used to create objects.
 An abstract class is designed only to act as a base class (to be inherited by other classes).
 Constructor and Destructors in derived classes:
 A destructor is special member function that is executed when an object of that class is
destroyed.
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
12 | P a g e
 Destroying an object means, de-allocating all the resources such as memory that was allocated for
the object by the constructor.
 It will have like constructor, the name same as that of the class but preceded by a tilde (~).
 Example: Program to illustrate the use of destructors in C++.
class Base
{
public:
Base( ); //Default constructor
{
cout<<”Inside Base Constructor”<<endl;
}
~ Base( ); //Destructor
{
cout<<”Inside Base Destructor”<<endl;
}
};
class Derived : public Base
{
public:
Derived( ); //Default constructor
{
cout<<”Inside Derived Constructor”<<endl;
}
~ Derived( ); //Destructor
{
cout<<”Inside Derived Destructor”<<endl;
}
};
void main( )
{
Derived x;
a.display( );
}
CHAPTER 10 – Inheritance BLUE PRINT
VSA (1 marks) SA (2 marks) LA (3 Marks) Essay (5 Marks) Total
- - - 01 Question 01 Question
- - - Question No 34 05 Marks
Important Questions
OUTPUT:
Inside Base Constructor
Inside Derived Constructor
Inside Derived Destructor
Inside Base Destructor
Chapter 10- Inheritance II PUC, MDRPUC, Hassan
13 | P a g e
5 Marks Question:
1. What is Inheritance? Explain any two types of Inheritance.
2. What are visibility modes? Explain.
3. What is the difference between public, private and protected access specifiers?
4. Explain single inheritance with a suitable C++ program.
5. What is Multilevel Inheritance? Explain with a suitable program example.
6. What is virtual base class? Give example.
7. What are the advantages of Inheritance? (Any five)
****************

More Related Content

PDF
Discrete Mathematics Lecture Notes
PDF
Computer Assisted Instruction
PDF
PHY PUC 2 Notes Electromagnetic induction
PPT
Action research related to Classroom problems
PPT
C++ Data Structure PPT.ppt
PDF
Digital electronics logic design complete notes.pdf
PDF
chapter-7-classes-and-objects.pdf
PPTX
Diabetes Mellitus
Discrete Mathematics Lecture Notes
Computer Assisted Instruction
PHY PUC 2 Notes Electromagnetic induction
Action research related to Classroom problems
C++ Data Structure PPT.ppt
Digital electronics logic design complete notes.pdf
chapter-7-classes-and-objects.pdf
Diabetes Mellitus

What's hot (20)

PDF
chapter-11-pointers.pdf
PPTX
Inheritance in java
PPTX
classes and objects in C++
PPTX
Inheritance in c++theory
PPTX
Polymorphism In c++
PPTX
Polymorphism in c++(ppt)
PPTX
Functions in c
PPT
16717 functions in C++
 
PPTX
16 dynamic-memory-allocation
PPTX
Inheritance in c++
PPT
Friends function and_classes
PDF
C programming notes
PPT
Class and object in C++
PPTX
Abstract class in c++
PPT
Storage classes
PPTX
PDF
Function overloading ppt
PPT
Class and Objects in PHP
PPT
Structure of C++ - R.D.Sivakumar
PPTX
Static Data Members and Member Functions
chapter-11-pointers.pdf
Inheritance in java
classes and objects in C++
Inheritance in c++theory
Polymorphism In c++
Polymorphism in c++(ppt)
Functions in c
16717 functions in C++
 
16 dynamic-memory-allocation
Inheritance in c++
Friends function and_classes
C programming notes
Class and object in C++
Abstract class in c++
Storage classes
Function overloading ppt
Class and Objects in PHP
Structure of C++ - R.D.Sivakumar
Static Data Members and Member Functions
Ad

Similar to chapter-10-inheritance.pdf (20)

PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPTX
OOPS IN C++
PDF
PPTX
Inheritance
PDF
Inheritance
PDF
Inheritance chapter-6-computer-science-with-c++ opt
DOCX
Introduction to object oriented programming concepts
PPTX
Inheritance
PPTX
Multiple Inheritance
PPT
11 Inheritance.ppt
PPT
Inheritance in C++
PPT
Inheritance
PPT
Inheritance in C++
PPTX
OOP unit II inheritance.pptx object oriented programming
ODP
Ppt of c++ vs c#
PDF
Constructor & destructor
PPTX
Introduction to inheritance and different types of inheritance
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
PPTX
Inheritance and Interfaces
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
Chapter 6 and inheritance OOP C++ tu ioe
OOPS IN C++
Inheritance
Inheritance
Inheritance chapter-6-computer-science-with-c++ opt
Introduction to object oriented programming concepts
Inheritance
Multiple Inheritance
11 Inheritance.ppt
Inheritance in C++
Inheritance
Inheritance in C++
OOP unit II inheritance.pptx object oriented programming
Ppt of c++ vs c#
Constructor & destructor
Introduction to inheritance and different types of inheritance
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
Inheritance and Interfaces
MODULE2_INHERITANCE_SESSION1.ppt computer
Ad

More from study material (20)

PDF
II PUC Reduced syllabus(NCERT ADOPTED SUBJECTS).pdf
PDF
12th English Notes.pdf
PDF
Organic_Chemistry_Named_Reaction_inDetail_by_Meritnation.pdf
PDF
chem MCQ.pdf
PDF
pue alcholn ethers.pdf
PDF
2023 Physics New Pattern
PDF
PHY PUC 2 Notes-Electromagnetic waves
PDF
PHY PUC 2 Notes-Alternating current
PDF
PHY PUC 2 NOTES:- MAGNETISM AND MATTER
PDF
PHY PUC 2 MOVING CHARGE AND MAGNETISM
PDF
PHY CURRENT ELECTRICITY PUC 2 Notes
PDF
physics El.potential & capacitance notes
PDF
important question of current electricity
PDF
09.Ray optics.pdf
PDF
01 Electric Fieeld and charges Notes.pdf
PDF
chapter-4-data-structure.pdf
PDF
chapter-14-sql-commands.pdf
PDF
chapter-16-internet-and-open-source-concepts.pdf
PDF
chapter-17-web-designing2.pdf
PDF
chapter-wise- 1 mark Q.pdf
II PUC Reduced syllabus(NCERT ADOPTED SUBJECTS).pdf
12th English Notes.pdf
Organic_Chemistry_Named_Reaction_inDetail_by_Meritnation.pdf
chem MCQ.pdf
pue alcholn ethers.pdf
2023 Physics New Pattern
PHY PUC 2 Notes-Electromagnetic waves
PHY PUC 2 Notes-Alternating current
PHY PUC 2 NOTES:- MAGNETISM AND MATTER
PHY PUC 2 MOVING CHARGE AND MAGNETISM
PHY CURRENT ELECTRICITY PUC 2 Notes
physics El.potential & capacitance notes
important question of current electricity
09.Ray optics.pdf
01 Electric Fieeld and charges Notes.pdf
chapter-4-data-structure.pdf
chapter-14-sql-commands.pdf
chapter-16-internet-and-open-source-concepts.pdf
chapter-17-web-designing2.pdf
chapter-wise- 1 mark Q.pdf

Recently uploaded (20)

PPTX
2. Earth - The Living Planet Module 2ELS
PDF
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
PPTX
Microbiology with diagram medical studies .pptx
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PPT
Chemical bonding and molecular structure
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PDF
AlphaEarth Foundations and the Satellite Embedding dataset
PDF
Sciences of Europe No 170 (2025)
PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
PPT
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
PPTX
famous lake in india and its disturibution and importance
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
2. Earth - The Living Planet earth and life
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PPTX
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
Comparative Structure of Integument in Vertebrates.pptx
PDF
The scientific heritage No 166 (166) (2025)
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
2. Earth - The Living Planet Module 2ELS
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
Microbiology with diagram medical studies .pptx
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
Chemical bonding and molecular structure
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
AlphaEarth Foundations and the Satellite Embedding dataset
Sciences of Europe No 170 (2025)
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
famous lake in india and its disturibution and importance
7. General Toxicologyfor clinical phrmacy.pptx
2. Earth - The Living Planet earth and life
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
Comparative Structure of Integument in Vertebrates.pptx
The scientific heritage No 166 (166) (2025)
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf

chapter-10-inheritance.pdf

  • 1. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 1 | P a g e Chapter-10 INHERITANCE  Introduction:  Inheritance is another important aspect of object oriented programming.  C++ allows the user to create a new class (derived class) from an existing class (base class).  Inheritance:  Inheritance is the capability of one class to inherit properties from another class.  Base Class: It is the class whose properties are inherited by another class. It is also called Super class.  Derived Class: It is the class that inherits the properties from base class. It is also called Sub class.  Need of Inheritance:  Suppose X is a class already defined and we need to redefine another class Y has same properties of X and in addition its own.  Suppose if we use direct option without using inheritance, it has following problems. o Code written in X is repeated again in Y which leads to unnecessary wastage of memory. o Testing to be done separately for both class X and class Y leads to waste of time.  The above problem can be solved by using the concept of inheritance.  If we use the code of X even in Y without rewriting it. The class Y inherits all the properties of X.  The class X is called base class and the class Y is called derived class.  The main advantages of Inheritance are: o Reusing existing code o Faster development time o Easy to maintain o Easy to extend o Memory Utilization  The main disadvantage of Inheritance are: o Inappropriate use of inheritance makes programs more complicated. o Calling member functions using objects creates more compiler overheads.
  • 2. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 2 | P a g e  Defining Derived Classes:  A derived class is a composite class – it inherits members from the base class and adds member of its own.  The general form of the derived class is given below. IMAGE  Here, o class  Keyword o derived_class_name  Name of the derived class o :  Shows the derivation from the base class. o Visibility Mode  Specifies the type of derivation o base_class_name  Name of the base class. o The use of a constructor can be cleverly done especially in those problems where it is necessary to initialize certain data members compulsorily.  Example: Public Derived Class Private Derived Class Protected Derived Class class father //Base class { private: char name[10]; public: char caste[10]; int age; void readdata( ); }; class son : public father { private: char gender[5]; public: void display( ); }; class father //Base class { private: char name[10]; public: char caste[10]; int age; void readdata( ); }; class son : private father { private: char gender[5]; public: void display( ); }; class father //Base class { private: char name[10]; public: char caste[10]; int age; void readdata( ); }; class son : protected father { private: char gender[5]; public: void display( ); };  Visibility mode:  Visibility mode can be public, private or protected. The private data of base class cannot be inherited. o public: If inheritance is done in public mode, public members of the base class become the public member of derived class and protected member of base class become the protected member of derived class..
  • 3. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 3 | P a g e o private: If inheritance is done in a private mode, public and protected members of base class become the private members of derived class. o protected: If inheritance is done in a protected mode, public and protected members of base class become the protected members of the base class. Derived Class Visibility Mode public private protected Base class public public private protected private Not inherited Not inherited Not inherited protected protected private protected  Public Inheritance:  When a base class is inherited as public, all public members of the base class become public members of derived class.  The private members of the base class remain private to that class, and are nor accessible by members of the derived class.  Example: A program illustrates public inheritance. #include<iostream.h> #include<conio.h> class shape //Base Class { public: int side1, side2; }; class rectangle : public shape //Derived Class { public: int area; void compute( ) { area = side1 * side2; } }; void main( ) { rectangle R; // R is the object of derived class R.side1 = 5; // Data directly accessed by object R.side2 = 6; R.compute( ); cout<< “Area of the rectangle = “ <<R.area; } Important 5 Marks OUTPUT: Area of the rectangle = 30
  • 4. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 4 | P a g e  Private Inheritance:  When a base class is inherited as private, then all public and protected members of the base class become private members of derived class.  This means that they are still accessible by member of the derived class, but cannot be accessed by other parts of the program.  Example: A program illustrates private inheritance. #include<iostream.h> #include<conio.h> class shape //Base Class { protected: int side1, side2; public: int area; void compute( ) { area = side1 * side2; } }; class rectangle : private shape //Derived Class { public: void readdata( ) { cout << “ Enter the input first side :”; cin>>side1; cout << “ Enter the input second side :”; cin>>side2; } void display( ) { compute( ); // Calling base class cout<< “Area of the rectangle = “ <<area; } }; void main( ) { rectangle R; R.readdata( ); R.display( ); } OUTPUT: Enter the input first side : 9 Enter the input second side : 5 Area of the rectangle = 45
  • 5. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 5 | P a g e  Protected Inheritance:  When a base class is inherited as protected, then all public and protected members of the base class become protected members of derived class.  The private data members of base class are not visible to derived class.  They can only be accessed through public and protected member functions of base class.  Example: A program illustrates protected inheritance with the base class having protected and public members. #include<iostream.h> #include<conio.h> class shape //Base Class { protected: int side1, side2; public: int compute( ) { return(side1 * side2); } }; class rectangle : protected shape //Derived Class { public: void readdata( ) { cout << “ Enter the input first side :”; cin>>side1; cout << “ Enter the input second side :”; cin>>side2; } void display( ) { cout<< “Area of the rectangle = “ <<compute( ); } }; void main( ) { rectangle R; R.readdata( ); R.display( ); } OUTPUT: Enter the input first side : 7 Enter the input second side : 8 Area of the rectangle = 56
  • 6. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 6 | P a g e  Types of Inheritance:  Based on the relationship, inheritance can be classified into five forms: o Single Inheritance o Multilevel Inheritance o Multiple Inheritance o Hierarchical Inheritance o Hybrid Inheritance  Single Inheritance:  Single Inheritance is the process of creating a new class from existing class base class.  It is very coomon in inheritance that a class is derived from the base class.  The data members and memberfunction of the base class are data member and member function of the derived class.  A derived class with single inheritance is declared as follows: class Base_Class { ……….. }; class Derived_class : public Base_calss { ……….. };  Example: Program to illustrate single level inheritance. #include<iostream.h> #include<conio.h> class base { private: int rollno; char name[10]; public: void read( ) Important 5 Marks
  • 7. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 7 | P a g e { cout << “ Enter Roll Number and Name “<<endl; cin >> rollno >> name; } void display( ) { cout << “ Roll No : “ << rollno <<endl; cout << “ Name : “ << name <<endl; } }; class derived : public base { private: int m1, m2, t; public: void read1( ) { cout << “ Enter Maths and Computer marks “<<endl; cin >> m1 >> m2; t = m1 + m2; } void display1( ) { cout << “ Maths : “ << m1 <<endl; cout << “ Computer : “ << m2 <<endl; cout << “Total Marks : “ << t <<endl; } }; void main( ) { derived obj; clrscr( ); obj.read( ); obj.read1( ); obj.display( ); obj.display1( ); getch( ); }  Multilevel Inheritance:  Derivation of a class from another derived class is called multilevel inheritance.  In the figure class A is the base class for class AB and class AB is the base class for class ABC.  The class AB provides a link for the inheritance between A and ABC, and is known as OUTPUT: Enter Roll Number and Name 1234 RAM Enter Maths and Computer marks 80 90 Roll No : 1234 Name : RAM Maths : 80 Computer : 90 Total Marks : 170
  • 8. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 8 | P a g e intermedidate base class.  A derived class with multilevel inheritance is declared as follows: class A { ……….. }; class AB : public A { ……….. }; class ABC : public AB { ……….. };  Example: Program to illustrate multilevel inheritance. #include<iostream.h> #include<conio.h> class A { public: void displayA( ) { cout << “ Base class A”<<endl; } }; class AB : public A { public:
  • 9. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 9 | P a g e void displayAB( ) { cout << “ Intermediate Base class AB”<<endl; cout << “ Derived from A” << endl; } }; class ABC : public AB { public: void displayABC( ) { cout << “ Derived Class ABC”<<endl; cout << “ Derived from AB” << endl; } void output( ) { displayA( ); displayAB( ); displayABC( ); }; void main( ) { ABC obj; clrscr( ); obj.output( ); getch( ); }  Multiple Inheritance:  A class can be derived from more than one base calss is known as multiple inheritance.  A derived class with multiple inheritance is declared as follows: class A //Base Class A { ……….. }; OUTPUT: Base Class A Intermediate Base Class AB Derived from A Derived Class ABC Derived from AB
  • 10. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 10 | P a g e class B //Base Class B { ……….. }; class C //Base Class C { ……….. }; class Derived_Class : public A, private B, protected C { ……….. //Members of derived class };  Hierarchical Inheritance:  If a number of classes are derived from a single base class, it is called as hierarchical inheritance.  Hierarchical model exhibits top down approach by breaking up a complex class into simpler class.  Hybrid Inheritance:  Hybrid Inheritance is combination of Hierarchical and multilevel inheritance.
  • 11. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 11 | P a g e  Virtual Base Classes:  When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited.  Such a base class is known as virtual base class.  This can be achieved by preceding the base class name with the word virtual.  Example: Class A { ---------------------; ---------------------; }; class B : virtual public A { ---------------------; ---------------------; }; class C : virtual public A { ---------------------; ---------------------; }; class D : public B, public C { ---------------------; ---------------------; };  Abstract Class:  An abstract class is one that is not used to create objects.  An abstract class is designed only to act as a base class (to be inherited by other classes).  Constructor and Destructors in derived classes:  A destructor is special member function that is executed when an object of that class is destroyed.
  • 12. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 12 | P a g e  Destroying an object means, de-allocating all the resources such as memory that was allocated for the object by the constructor.  It will have like constructor, the name same as that of the class but preceded by a tilde (~).  Example: Program to illustrate the use of destructors in C++. class Base { public: Base( ); //Default constructor { cout<<”Inside Base Constructor”<<endl; } ~ Base( ); //Destructor { cout<<”Inside Base Destructor”<<endl; } }; class Derived : public Base { public: Derived( ); //Default constructor { cout<<”Inside Derived Constructor”<<endl; } ~ Derived( ); //Destructor { cout<<”Inside Derived Destructor”<<endl; } }; void main( ) { Derived x; a.display( ); } CHAPTER 10 – Inheritance BLUE PRINT VSA (1 marks) SA (2 marks) LA (3 Marks) Essay (5 Marks) Total - - - 01 Question 01 Question - - - Question No 34 05 Marks Important Questions OUTPUT: Inside Base Constructor Inside Derived Constructor Inside Derived Destructor Inside Base Destructor
  • 13. Chapter 10- Inheritance II PUC, MDRPUC, Hassan 13 | P a g e 5 Marks Question: 1. What is Inheritance? Explain any two types of Inheritance. 2. What are visibility modes? Explain. 3. What is the difference between public, private and protected access specifiers? 4. Explain single inheritance with a suitable C++ program. 5. What is Multilevel Inheritance? Explain with a suitable program example. 6. What is virtual base class? Give example. 7. What are the advantages of Inheritance? (Any five) ****************