What is inheritance?
The process of creating a new class from the existing class is called inheritance.
The existing class is called super c;asses base class and
the newly created class is called sub class derived ckass as a result the
derive class inherits all part part of feature of base class as well as some
additional feature of it’s own.Their are many types of inheritance in C++.
The base class is the generalization
of the derived class in other sence derive class is the specialization of the
base class.
The main advantages of inheritance is
that every time a new thing will be created which can hold some existing
feature of existing class as well as add some new feature to it.
Inheritance are
of the following types
v Single
inheritance
v Multilevel
inheritance
v Multiple
inheritance
v Hybrid
inheritance
v Hierarchical
inheritance
Syntax for inheritance:
Class derived class
name:public baseclass name
{
----------
}
Syntax for calling a base class member function into the derived class.
Base classname::member function
|
Single inheritance:
In this type of inheritance there is only
one base class as well as one derived class.
Digram of single inheritance:
Base
class(A)
|
Derived
class(B)
//This program illustrates the
single inheritance
.#include<isstream.h>
#include<conio.h>
Class punlication
{
Private:
Char title;
Char pubname;
Int pubyear;
Public:
Void getdata()
{
Cout<<”Enter the title”;
Cin>>title;
Cout<<”Enter the
publication company name”;
Cin>>pubname;
Cout<<”Enter the
publication company year”;
Cin>>pubyear;
}
Void putdata()
{
Cout<<”title=”<<title<<endl;
Cout<<”publication company
name<<pubname<<endl;
Cout<<”publication company
year<<pubyear<<endl;
}
};
Class book;public publication
{
Private:
Int en;
Float price;
Public:
Void readdata()
{
Publication::getdata
Cout<<”Enter edition number”;
Cin>>en;
Cout<<”Enter number of page”;
Cim>>no page
Cout<<”Enter the price:”;
Cin>>price;
}
Void showdata()
{
Publication::putdata()
Cout<<”Edition number”<<endl;
Cout<<”number page=”<<npage<<endl;
Cout<<”price_<<price<<endl;
}
};
Clrscr();
Book b1
b1.readdata();
b1.showdata();
getch();
}
Multilevel inheritance:
In this type of inheritance there is only one base class but
more than one derived class.The common properties of base class inheritance to
it’s derive class.And each derive add some extra feature.
Digram of multilevel inheritance:
base class A
derived class derived class
B C
//This program
illustrate the mu;lti level inheritance
#include<iostraem.h>
#include<conio.h>
Class publication
{
Private:
Char title[15];
Char pubname;
Int pubyear;
Public;
Void getdata()
{
Cout<<”Enter
the title:”;
Cin>>title;
Cout<<”Enter
the publication company name:”;
Cin>>pubyear;
}
Void pudata()
{
Cout<<”title+”<<title<<endl;
Cout<<”publication
company name<<pub name<<endl;
Cout<<”publication
year<<pubyear<<endl;
}
};
Class book:public
publication
{
Private:
Int en;
Int no page;
Public:
Void readdata()
{
Publication::grtdata();
Cout<<”Enter
the edition number:”;
Cin>>en;
Cout<<”Enter
the no. of pages:”;
Cin>>nopage;
}
Void showdata()
Cout<<”Edition
number-”<<endl;
Cout<<”No
of pages-“<<nopage<<endl;
}
};
Class
tape:public publication
[
Private:
Char modelname[15];
Int playtime;
Public:
Void inputdata()
{
Publication::readdata();
Cout<<”enter
the model name:”;
Cin>>modelname:
Cout>.”enter
the playtime:”;
Cin>>playtime;
}
Void outputdata()
{
Publication::pudata();
Cout<<”model
name-“<<modname<<endl;
Cout<<playtime-“<<playtime<<endl;
}
};
Void main(0;
Clrscr()’
Book b1;
Tape t1;
Cout<<”Enter
data about book”<<endl;
Cout<<”--------------------------------”<<endl;
T1.inputdata();
Cout<<”data
on book”<<endl;
Cout<<”-------------------“<<endl;
B1.showdata(0;
Getch(0;
}
Multiple inheritance
In this type
of inheritance their is only one derived
class but more than one base class.
Digram for multiple inheritance
Base class
a
|
Baseclass
b
|
Derived class
c
|
Syntax of
multiple inheritance:-
Class derived
class name:public base1,public base2………
{
--------
---------
}
Hybrid inheritance
The combination of multilevel and multiple inhertance is called hybrid inheritance.
Digram of hybrid inheritance:
Base class
A
derived class derived class
B C
derived class
Great sir ji
ReplyDeleteThank you if you like a post then visit my site for more computer knowledge
Deletewww.computereducation.xyz