0% found this document useful (0 votes)
3 views33 pages

INHERITANCE

Inheritance is a key concept in object-oriented programming that allows new classes (derived classes) to be created from existing ones (base classes), enabling property reuse. Access specifiers (public, private, protected) determine how members of the base class can be accessed in derived classes, and various types of inheritance (single, multiple, hierarchical, multilevel, hybrid, multipath) define the relationships between classes. The document provides syntax examples and programs demonstrating different inheritance types and access specifier behaviors.

Uploaded by

m acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views33 pages

INHERITANCE

Inheritance is a key concept in object-oriented programming that allows new classes (derived classes) to be created from existing ones (base classes), enabling property reuse. Access specifiers (public, private, protected) determine how members of the base class can be accessed in derived classes, and various types of inheritance (single, multiple, hierarchical, multilevel, hybrid, multipath) define the relationships between classes. The document provides syntax examples and programs demonstrating different inheritance types and access specifier behaviors.

Uploaded by

m acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

INHERITANCE

Introduction
Inheritance is one of the most useful and essential characteristics of
object-oriented programming. The existing classes are main
components inheritance. The new classes are created from existing
one. The properties of existing classes are simply extended to the new
classes. The new classes created using such methods are known as
derived classes and the existing classes are known as base classes.
The relationship between the base and derived class is known as kind
of relationship. The object of the derived class can access members
of base class as well as derived class. The object of base class can not
access the members of derived class.
-Reusability means reuse of properties of base class in the derived
class.
-The base class is called as super class, parent or ancestor and the
derived class as subclass, child or descendant.
-The procedure of creating a new class from one or more existing
classes is termed as inheritance.
Access Specifiers
-The public members of a class can be accessed by objects directly
outside the class without member function of the class.
-The private members of a class can be accessed by the public
member functions of the same class.
-The protected members of a class can be accessed in derived class
without member function of the same class.
Syntax for creating Derived class
Class derived_class name : Access specifier Base class_name
{
----------------------
---------------------- // Body of derived class
----------------------
};
-The derived class and base class are separated by colon(:) .The
access specifier may be private , public or protected. In the absence of
the access specifier, the default is private.
-The access specifiers decide whether the characteristics of the base
class are derived privately or publicly.
-The derived class also has own set of member data and member
functions.
Syntax for Types of declaration
(1) Class B : public A
{
// members of class B
};
(2) Class B : private A
{
//members of class B
};
(3) Class B : A //By default private
{
//members of class B
};
(4) Class B : protected A
{
//members of class B
};
(5) Struct B : A //By default public
{
//members of B
};

Program 1:Write a program to derive a class publicly from base


class. Declare the base class with its member under public section.

#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Public:
Int x;
};
Class B : public A //derived class
{
Public:
Int y;
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.x=20;
b.y=30;
cout<<”\n Member of A :”<<b.x;
cout<<”\n Member of B :”<<b.y;
return 0;
}
O/P : Member of A : 20
Member of B : 30

Program 2:Write a program to derive a class publicly from base


class. Declare the base class with its member under private section.

#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Private:
Int x;
Public:
A()
{
X=20;
}
Void show()
{
Cout<<”\n x = “ << x;
}
};
Class B : public A //derived class
{
Public:
Int y;
B()
{
Y=30;
}
Void show()
{
Cout<<”\n y = “<<y;
}
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.show();
return 0;
}
O/P : x=20
Y=30

Program 3:Write a program to derive a class privately from base


class. Declare the base class with its member under public section.

#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Public:
Int x;
};
Class B : private A //derived class
{
Public:
Int y;
B()
{
X=20;
Y=30;
}
Void show()
{
Cout<<”\n x = “<<x;
Cout<<”\n y = “<<y;
}
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.show();
return 0;
}
O/P : x=20
Y=30

Program 4:Write a program to declare a protected data in base


class. Access data of base class declare under protected section using
member functions of derived class.

#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Protected:
Int x;
};
Class B : private A //derived class
{
Int y;
Public:
B()
{
X=20;
Y=30;
}
Void show()
{
Cout<<”\n x = “<<x;
Cout<<”\n y = “<<y;
}
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.show();
return 0;
}
O/P : x=20
Y=30

Access Specifier with their scope


Sl Base Class Access Derived Class Access Modes
NO. Mode Private Public Protected
1 Public Private Public protected
2 Private Not Not Not
inherited inherited inherited
3 protected Private protected Protected

NOTES :
- All private members of the class are accessible to public
members of the same class. They can not be inherited.
- The derived class can access private members of the base class
using member function of the base class.
- All the protected members of the class are available to its
derived classes and can be accessed without the use of member
functions of base class
- If any class is prepared for deriving classes , it is advisable to
declare all members of base class as protected so that derived
classes can access the members directly .
- All the public members of the class are accessible toits derived
class . There is no restrictions for accessing elements.
- The access specifier required while deriving classes is either
private or public .If not specified , private is default for class and
public for structures.
- Constructors and destructors are declared in public section of
the class. If declared in private section the object declared will
not be initialized and compiler flag an error.
- The private , public and protected(visibility sections) can be
defined several times in any sequence.

Types of Inheritances
The class can be inherited from another class depending on the
following points:
1.Number of base classes: The program can use one or more base
classes to derive a single class.
2.Nested derivation : the derived class can be used as base class
and new class can be derived from it.
Depending on the above points inheritance is classified as
follows :
 Single Inheritance
 Multiple Inheritance
 Hierarchical Inheritance
 Multilevel Inheritance
 Hybrid Inheritance
 Multi path Inheritance

Single Inheritance
When single class is derived from a single base class and the derived
class is not used as base class ,such type of inheritance is called as
Single Inheritance .
BASE CLASS

DERIVED CLASS

Program 5: Write a program to show single inheritance between two


classes.
#include<stdio.h>
#include<conio.h>
Class ABC
{
Protected:
Char name[20];
Int age;
};
Class abc :public ABC
{
Float ht;
Float wt;
Public:
Void getdata()
{
Cout<<”\n Enter name and age :”;
Cin>>name>>age;
Cout<<”\n Enter Height and weight :”;
Cin>>ht>>wt;
}
Void show()
{
Cout<<”\n Name :”<<name ;
Cout<<”\n Age :”<<age<<”years” ;
Cout<<”\n Height :”<<ht<<”feet” ;
Cout<<”\n Weight :”<<wt<<”kg”;
}
};
Int main()
{
Clrscr();
abc x;
x.getdata();
x.show();
return 0;
}
O/P:
Enter name and age: Santosh 24
Enter Height and weight : 5.5 50
Name : Santosh
Age :24 years
Height :5.5 feet
Weight : 50 kg

Multiple Inheritance
When two or more base classes are used to derive a single class is
called , is called as Multiple Inheritance.

BASE CLASS BASE CLASS

DERIVED CLASS

Program 6 : Write a program to derive a class from multiple base


classes.

#include<iostream.h>
#include<conio.h>
{
Class A
{
Protected :
Int a;
};
Class B
{
Protected :
Int b ;
};
Class C
{
Protected :
Int c;
};
Class D
{
Protected :
Int d;
};
Class E : public A ,B , C , D
{
Int e ;
Public :
Void getdata()
{
Cout<<”\n Enter value of a , b , c , d and e :” ;
Cin>>a>>b>>c>>d>>e ;
}
Void showdata()
{
Cout<<”\n a=”<<a<<”b=”<<b<<”c=”<<c
<< ”d=”<<d<<”e=”<<e ;
}
};
Int main()
{
Clrscr();
Ex;
x.getdata();
x.showdata();
}

Hierarchical Inheritance
When a single class is used for derivation of two or more classes , it is
known as Hierarchical Inheritance.

BASE CLASS

DERIVED CLASS DERIVED CLASS DERIVED CLASS

Program 7: Write a program to show hierarchical interface.


#include<iostream.h>
#include<iostream.h>
Class red
{
Public:
red()
{
Cout<<”Red “ ;
}
};
Class yellow
{
Public:
Yellow()
{
Cout<<”yellow “ ;
}
};
Class blue
{
Public:
Blue()
{
Cout<<”Blue “ ;
}
};
Class orange :public red ,public yellow
{
Public:
Orange()
{
Cout<<” = orange”;
}
};
Class green : public blue , public yellow
{
Public :
green()
{
Cout<<” =green”;
}
};
Class violet : public red , public blue
{
Public :
Violet()
{
Cout<<” =violet”;
}
};
Class reddishbrown : public orange ,public violet
{
Public:
reddishbrown()
{
Cout<<” =Reddishbrown”;
}
};
Class yellowishbrown : public orange ,public green
{
Public:
yellowishbrown()
{
Cout<<” =Yellowishbrown”;
}
};
Class bluishbrown : public green,public violet
{
Public:
bluishbrown()
{
Cout<<” =Bluishbrown”;
}
};
Int main()
{
Clrscr();
Reddishbrown r;
Endl;
Bluishbrown b;
Endl;
Yellowishbrown y;
Endl ;
}
O/P :
Red Yellow=Orange Red Blue=Violet=Reddishbrown
Red Blue=Violet Blue Yellow=Green=Bluishbrown
Blue Yellow=Green Red Yellow=Orange=Yellowishbrown

Multilevel Inheritance
When a class is derived from another derived class i.e. derived class
acts as a base , such type of inheritance is known as Multilevel
Inheritance.

BASE CLASS

DERIVED CLASS
CHILD CLASS

Program 8: Write a program to create multilevel inheritance . Create


classes A1,A2,and A3.
#include<iostream.h>
#include<conio.h>
Class A1
{
Protected :
Char name[20];
Int age ;
};
Class A2 : public A1
{
Protected:
Float ht;
Float wt;
};
Class A3 : public A2
{
Protected:
Char sex;
Public:
Void get()
{
Cout<<”Name :” ;
Cin>> name ;
Cout<<”Age :” ;
Cin>> age;
Cout<<”Sex :” ;
Cin>> sex ;
Cout<<”Height :” ;
Cin>> ht;
Cout<<”Weight :” ;
Cin>> wt;
}
Void show()
{
Cout<<”\n Name : “ <<name ;
Cout<<”\n Age : “ <<age <<”years” ;
Cout<<”\n Sex : “ <<sex ;
Cout<<”\n Height : “ <<ht<<”feet” ;
Cout<<”\n Weight : “ <<ht<<”kg” ;
}
};
Int main()
{
Clrscr();
A3 x ;
x.get();
x.show();
return 0;
}
O/P :
Name : Balaji
Age : 26
Sex: M
Height : 5
Weight : 45

Name : Balaji
Age : 26
Sex: M
Height : 5 feet
Weight : 45kg

Hybrid Inheritance
The combination one or two type of inheritance is known as Hybrid
Inheritance.
BASE CLASS
DERIVED CLASS BASE CLASS

CHILD CLASS

Program 9: Write a program to create a derived class from multiple


base classes. ( Hybrid Inheritance)
#include<iostream.h>
#include<conio.h>
Class Player
{
Protected:
Char name[15];
Char gender;
Int age;
};
Class Physique : public Player
{
Protected :
Float height;
Float weight;
};
Class location
{
Protected:
Char city[10];
Char pin[7];
};
Class game : public Physique , public Location
{
Protected :
Char game[15];
Public:
Void getdata()
{
Cout<<”Enter Following information \n “;
Cout<<”Name :”;
Cin>>name;
Cout<<”Gender :”;
Cin>>gender;
Cout<<”Age :”;
Cin>>age;
Cout<<”Height :”;
Cin>>height;
Cout<<”Weight :”;
Cin>>weight;
Cout<<”City :”;
Cin>>city;
Cout<<”Pincode :”;
Cin>>pin;
Cout<<”Game :”;
Cin>>game;
}
Void show()
{
Cout<< “\n Entered information “ ;
Cout<<”\n Name :”
Cout<<name ;
Cout<<”\nGender :”
Cout<<gender ;
Cout<<”\n Age :”
Cout<<age;
Cout<<”\n Height :”
Cout<<height;
Cout<<”\n Weight :”
Cout<<weight ;
Cout<<”\n City :”
Cout<<city ;
Cout<<”\n Pincode :”
Cout<<pin ;
}
};
Int main()
{
Clrscr();
Game g;
g.getdate();
g.show();
return 0 ;
}

O/P :
Enter following information
Name : Mahesh
Gender : M
Age : 25
Height : 5.2
Weight : 55
City : BBSR
Pincode : 751001
Game : Cricket
Entered Information
Name : Mahesh
Gender : M
Age : 25
Height : 5.2
Weight : 55
City : BBSR
Pincode : 751001
Game : Cricket

Multipath Inheritance
When a class is derived from two or more classes that are derived
from same base class , such type of inheritance is called Multipath
Inheritance.
BASE CLASS

DERIVED CLASS DERIVED CLASS

CHILD CLASS
Multipath inheritance consists of many types of inheritances such as
multiple , multilevel and hierarchical .
Consider the following example :
Class A1
{
Protected :
Int a1;
};
Class A2:public A1
{
Protected :
Int a2;
};
Class A3 : public A1
{
Protected :
Int a3;
};
Class A4 : public A2 ,A3
{
Int a4;
};

In the above example , class A2 and A3 are derived from same base
class i.e. A1 by hierarchical inheritance. The class A1 and A2 both
access variable a1 of class A1. The class A4 is derived from class A2
and A3 by multiple inheritance. The derived class A4 has two sets of
data members of class A1, which creates ambiguity problem. If we try
to access variable a1 of class A1 in A4 class ,the compiler shows
error.

Virtual Base Class


To overcome the ambiguity occurred due to multipath inheritance, C+
+ provides the keyword virtual. The keyword virtual declares the
specified classes virtual .
EX:
Class A1
{
Protected :
Int a1;
};
Class A2 : virtual public A1 //virtual class declaration
{
Protected :
Int a2;
};
Class A3 : virtual public A1 //virtual class declaration
{
Protected :
Int a3;
};
Class A4 : public A2, A3
{
Int a4;
};

When classes are declared as virtual , the compiler takes necessary


precaution to avoid duplication of member variables. Thus ,we make
a class virtual if it is a base class that has been used by more than one
derived class as their base class .
Explanation :
1) A base class cannot be specified more than once in a derived
class.
Class A
{ -----------
-------------- };
Class A1 : A , A { ------} ; //illegal declaration
Here, class A is specified twice , it is illegal .
2) A base class can be indirectly passed to the derived class more
than once .
Class L : public A { ……};
Class K : public A{ …….};
Class J : public L , public K {………}; // legal declaration
Here, each object of class J will contain two sub-objects of class
A through classes L and K.
3) To avoid the duplication we can add the keyword the virtual to a
base class specifier .
Class L :virtual public A {…..};
Class K : virtual public A{ …….};
Class J : public L , public K {………};
The virtual keyword always appears before the class name. A
is now a virtual base class , and class J has only one sub-objects
of class A.
Program 10:Write a program to declare virtual base classes. Derive
a class using two virtual classes.
#include<iostream.h>
#include<conio.h>
Class A1
{
Protected :
Int a1;
};
Class A2 : virtual public A1 //virtual class declaration
{
Protected :
Int a2;
};
Class A3 : virtual public A1 //virtual class declaration
{
Protected :
Int a3;
};
Class A4 : public A2, A3 //virtual declaration
{
Int a4;
Public:
Void get()
{
Cout<<” Enter values of a1 , a2 , a3 and a4 :” ;
Cin>> a1>>a2>>a3>>a4 ;
}
Void put ()
{
Cout<<”a1=”<<a1<<”a2=”<<a2<<”a3=”<<a3<<”a4=”<<a4;
}
};
Void main ()
{
Clrscr();
A4 a;
a.get();
a.put();
}
O/P :
Enter values of a1 , a2 , a3 and a4 : 5 8 7 6
a1=5 a2=8 a3=7 a4=6

CONSTRUCTORS , DESTRUCTORS AND INHERITANCE


The constructors are used to initialize member variables of the object
and destructor is used to destroy the object. The compiler
automatically invokes the constructors and destructors. The derived
class does not require a constructors if the base class contains a zero-
argument constructors. If the base class has parameterized constructor
then it is essential for the derived class to have a constructor. The
derived class constructor passes arguments to the base class
constructor. When an object of derived class is declared ,the
constructor of base and derived classes are executed .
- In inheritance , destructors executed in reverse order of
constructor execution.
- The destructor is executed when an object goes out of scope .
Program 11: Write a program to show sequence of execution of
constructor and destructor in multiple inheritance .

#include<iostream.h>
#include<conio.h>
Class A //base class
{
Public :
A()
{
Cout<<”\n zero-argument constructor of base class A” ;
}
~A()
{
Cout<<”\n Destructor of the class A “ ;
}
};
Class B //base class
{
Public :
B()
{
Cout<<”\n zero-argument constructor of base class B” ;
}
~B()
{
Cout<<”\n Destructor of the class B “ ;
}

};
Class C : public A, public B //derivation of class
{
Public :
C()
{
Cout<<”\n zero-argument constructor of base class C” ;
}
~C()
{
Cout<<”\n Destructor of the class C “ ;
}

};
Void main ()
{
Clrscr();
C ob1; //object declaration
}
O/P :
zero-argument constructor of base class A
zero-argument constructor of base class B
zero-argument constructor of base class C
Destructor of the class C
Destructor of the class B
Destructor of the class A

1)Base and Derived classes with constructors


Program 12 : Write a program to declare both base and derived
classes with constructors .
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
I()
{
Cout<<”\n In base constructor ” ;
}
};
Class II : public I //derived class
{
Public :
II()
{
Cout<<”\n In derived class constructor ” ;
}
};
Void main ()
{
Clrscr();
II i; //object declaration
}
O/P :
In base class constructor
In derived class constructor

2)Base class with various constructors and Derived class with one
constructor
Program 13 :Write a program to declare multiple constructors in
base class and single constructor in derived class.
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
Int x;
I()
{
Cout<<”\n zero-argument base class constructor” ;
}
I(int x)
{
Cout<<”\n one argument base class constructor “ ;
}
};
Class II : public I //base class
{
Int y;
Public :
II(int j)
{
Cout<<”\n one argument derived class constructor ” ;
Y=j;
}
};
Void main ()
{
Clrscr();
II i(2); //object declaration
}
O/P :
Zero-argument base class constructor
One argument derived class constructor
3)Base and Derived classes without default constructor
Program 14 : Write a program to declare base and derived class
without default constructor .
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
Int x;
I(int k)
{
X = k;
Cout<<”\n one-argument base class constructor” ;
}
};
Class II : public I //derived class
{
Int y;
Public :
II(int j) : I(j)
{
Cout<<”\n one-argument derived class constructor” ;
Y=j;
}
};
Void main ()
{
Clrscr();
II i(2); //object declaration
}
O/P :
One argument base class constructor
One argument derived class constructors
4)Constructors and Multiple Inheritance
Program 15 :Write a program to derive a class using multiple base
classes .Observe the execution of constructor when object of derived
class is declared .
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
I()
{
Cout<<”\n zero-argument constructor of base class I” ;
}
};
Class II : //base class
{
public :
II()
{
Cout<<”\n zero argument constructor of class II ” ;
}
};
Class III : public II , I
{
Public :
III()
{
Cout<<”\n zero argument constructor of base class III “ ;
};
Void main ()
{
Clrscr();
III i; //object declaration
}
O/P :
Zero argument constructor of base class II
Zero argument constructor of base class I
Zero argument constructor of base class III
5)Constructors in Multiple Inheritances with Explicit call
Program 16 : Write a program to derive a class using multiple base
classes . Invoke the constructors of base classes explicitly.
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
I()
{
Cout<<”\n zero-argument constructor of base class I” ;
}
};
Class II : //base class
{
public :
II()
{
Cout<<”\n zero argument constructor of class II ” ;
}
};
Class III : public II , I
{
Public :
III() : II() , I()
{
Cout<<”\n zero argument constructor of base class III “ ;
};
Void main ()
{
Clrscr();
III i; //object declaration
}
O/P :
zero argument constructor of class II
zero argument constructor of class I
zero argument constructor of class III
6)Multiple Inheritance and Virtual Class
Program 17 :Write a program to derive a class using multiple base
classes. Invoke the constructors of base classes explicitly . Declare
any one base class as virtual.
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
Int x;
I()
{
Cout<<”\n zero-argument constructor of base class I” ;
}
};
Class II //base class
{
Public :
II()
{
Cout<<”\n Zero argument constructor of base class II ” ;
}
};
Class III : public II , virtual I //base class
{
Public :
III() : II() , I()
{
Cout<<”\n zero argument constructor of base class III ” ;
}
};
Void main ()
{
Clrscr();
III i; //object declaration
}
O/P :

zero argument constructor of base class I


zero argument constructor of base class II
zero argument constructor of base class III
7) Execution of Constructors in Multilevel Inheritance
Program 18: Write a program to derive a class using multilevel
inheritance and observe the execution sequence of constructors.
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
I()
{
Cout<<”\n zero-argument constructor of base class I” ;
}
};
Class II : public I //derived class
{
Public :
II()
{
Cout<<”\n Zero argument constructor of base class II ” ;
}
};
Class III : public II //child class
{
Public :
III()
{
Cout<<”\n zero argument constructor of base class III ” ;
}
};
Void main ()
{
Clrscr();
III i; //object declaration
}
O/P :
zero argument constructor of base class I
zero argument constructor of base class II
zero argument constructor of base class III

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy