Chapter-10 Inheritance: Inheritance Is The Capability of One Class To Inherit Properties From Another Class

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Chapter 10- Inheritance II PUC

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.

1|Page
Chapter 10- Inheritance II PUC

 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 class father //Base class class father //Base class
{ { {
private: private: private:
char name[10]; char name[10]; char name[10];
public: public: public:
char caste[10]; char caste[10]; char caste[10];
int age; int age; int age;
void readdata( ); void readdata( ); void readdata( );
}; }; };
class son : public father class son : private father class son : protected father
{ { {
private: private: private:
char gender[5]; char gender[5]; char gender[5];
public: public: public:
void display( ); void display( ); 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..
2|Page
Chapter 10- Inheritance II PUC

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
public public private protected
Base class 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> Important
#include<conio.h> 5 Marks
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;
OUTPUT:
R.compute( );
cout<< “Area of the rectangle = “ <<R.area; Area of the rectangle = 30
}
3|Page
Chapter 10- Inheritance II PUC

 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( )
{ OUTPUT:
rectangle R; Enter the input first side : 9
R.readdata( ); Enter the input second side : 5
R.display( );
Area of the rectangle = 45
}

4|Page
Chapter 10- Inheritance II PUC

 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( );
}
}; OUTPUT:
void main( )
Enter the input first side : 7
{
rectangle R; Enter the input second side : 8
R.readdata( ); Area of the rectangle = 56
R.display( );
}

5|Page
Chapter 10- Inheritance II PUC

 Types of Inheritance:
 Based on the relationship, inheritance can be classified into five forms:
o Single Inheritance
o Multilevel Inheritance
Important
o Multiple Inheritance
5 Marks
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( )
6|Page
Chapter 10- Inheritance II PUC

{
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;
}
};
OUTPUT:
void main( ) Enter Roll Number and Name
{ 1234 RAM
derived obj; Enter Maths and Computer marks
clrscr( ); 80 90
obj.read( ); Roll No : 1234
obj.read1( ); Name : RAM
obj.display( ); Maths : 80
obj.display1( ); Computer : 90
getch( ); Total Marks : 170
}

 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
7|Page
Chapter 10- Inheritance II PUC

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:

8|Page
Chapter 10- Inheritance II PUC

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( );
OUTPUT:
};
void main( ) Base Class A
{ Intermediate Base Class AB
ABC obj; Derived from A
clrscr( ); Derived Class ABC
obj.output( ); Derived from AB
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
{
………..
};
9|Page
Chapter 10- Inheritance II PUC

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.

10 | P a g e
Chapter 10- Inheritance II PUC

 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.
11 | P a g e
Chapter 10- Inheritance II PUC

 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;
}
}; OUTPUT:
void main( )
Inside Base Constructor
{
Derived x; Inside Derived Constructor
a.display( ); Inside Derived Destructor
} Inside Base Destructor

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

12 | P a g e
Chapter 10- Inheritance II PUC

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)

****************

13 | P a g e

You might also like