Inheritance
The relationship between classes to inherit the features of a base class in to sub class is known as inheritance . The base class is also known as parent class or old class and sub class is known as child class or new class or derived class . The derived class contains all the features can be added to derived class .
Features of Inheritance
- Reuseability : It can allow the code to be reused with other class . After compiling the program , the features of base class are not useful , again . But with the help of inheritance we can create many derived class from the base class.
- Save time and effort : The technique of Reuseability of code saves the effort and time of the programmer.
- Reliability : It can provide the better result to programmer with reliability.
Types of Inheritance
A derived class inherits the features of one class as well as the features of more than one class . C++ provides five types of Inheritance .
- Single Inheritance : When a child class inherits the features of a single parent class , then this mechanism is called single inheritance . Also we can say that in single inheritance there is a single base class and single derived .
- Multi Level : The mechanism of deriving a class from another derived class is known as multi level inheritance.
- Multiple Inheritance : In Multiple Inheritance there is a single derived class but more than one base class . In other words , in a multiple inheritance a single derived class is inherited from several base class.
- Hierarchical Inheritance : In such form of Inheritance there is a one base class and more than one derived classes . That is several derived classes are inherited from a single base class. Also we can say that a base class with several derived class is known as hierarchical Inheritance.
- Hybrid Inheritance : The combination of two or more form of inheritance is called
Hybrid Inheritance .
Base classes and derived classes : A base is a class from which other sub class are derived .
There are two types of base class.
- Direct base class : It is a class from which a derived class inherits directly or immediately. In single inheritance class A is direct base class from class B .
- Indirect base class : It is a class B is derived a A class and a C class is derived from B class . Then class A for class C is called indirect base class.
Class Access control : The member functions and data members of both base class and derived class can be access by the object of derived class .In c++ , the three levels of access control .
- Private : It is a visibility specifier or access specifier for the class member . The member function and data member declared under private specifier are not accessible to any function except the member function of the class in which it is declared.
- Protected : like private , protect is also a visibility specifier for the class members. The member function data members declared under protected specifier are accessible not only to the member function of the class but also to the member function of the derived class.
- Public : This is the third visibility for the class member . The members declared under public specifier in the class are accessible . That is any function can access a public member of the class.
* Single Inheritance (Public derivation) *?
# include <iostream .h>#include <conio.h>
Class base
{
Public : int a , b ;
Void getab( )
{
a = 10 , b = 20;
}
Void showab ( )
{
cout << "a ="<<a<<"\t b ="<<b<<"\n\n"
}
};
Class derived : Public base
{
Public :
Int c ;
Void multi ( )
{
c = a * b ;
}
Void result ( )
{
cout << "====================\n\n";
cout << "multiplication = "<<c ;
}
};
Void main ( )
{
Derived D ;
Clrscr ( );
D.getab ( );
D.multi ( );
D.result ( );
getch ( );
}
* single inheritance with private specifier * (Public derivation)
Class B{
Private : Float height , width ;Public :
Void getdata ( )
{
height = 6.12 , width = 9.14 ;
}
Void returnheight ( )
{
return height ;
}
Void returnwidth ( )
{
return width ;
}
Void show ( )
{
cout <<"height = "<<height <<"\t
width = "<<width <<"\n\n ;
}
};
Class A : Public B
{
Private :
Void calculate ( )
{
AOT = (return height ( ) *return width )/2;
}
Void result ( )
{
cout << "==================\n\n";
cout << "area of triangle = "<<AOT << "square meter";
}
};
* single Inheritance with private derivation *?
Class parent
{Public : int AMT , Rate , Time ;
Void getdata ( )
{
cout << "enter amount = " ;
cin >> AMT ;
cout << "enter rate = " ;
cin >> rate ;
cout << "enter time = ";
cin >> time ;
}
Void show ( )
{
cout << "Amount = "<<AMT<<"\t rate ="<<rate<<"\t time ="<<time<<"\n\n;
}
};
Class child : Private parent
{
Public : int s1 ;
Void interest ( )
{
getdata ( );
s1 =(AMT * Rate *Time )/100 ;
}
Void display ( )
{
show ( );
cout << "===============\n\n";
cout <<"simple interest = "<<SI ;
}
};
Void main ( )
{
Child A ;
Clrscr ( );
A.interest ( ) ;
A .display ( );
getch ( );
}
www.programmingking.in
Protected Members : The data members and member function declared under protected specifier are called protected member and are accessible to only friend function or member function of the derived class . with the help of following table we can understand the concept of derived .Derivation
Access specifier Public Protected PrivatePublic : Public Protected Private
Protected : Protected Protected Private
Private : Not accessible Not accessible Not accessible
* Program to [ multilevel Inheritance] making public member * ?Class Super
{
Protected :
Int Rn
Void getRn ( )
{
cout <<"enter roll number =";
cin >> Rn ;
}
Void showRn ( )
{
Cout << "roll no = "<<Rn<<"\n\n";
}
};
Class sub1 : Public Super
{
Protected :
Int VB , HTML , Java ;
Void getmarks ( int i , int j ,int k)
{
VB = i ; HTML = j ; Java = k ;
}
Void showmarks ( )
{
Cout << "Visual Basics = "<<VB <<"\t HTML="<<HTML <<"Java = "<<Java <<"\n\n ;
}
};
Class base 2 :Public base1
{
Protected :
int T ;
Void display ( )
{
T = VB + HTML +Java ;
getRn ( );
getmarks ( 50 ,65 ,70 );
ShowRn ( );
Showmarks ( );
cout <<"=======================\n\n";
cout <<"Total marks = "<<T;
}
};
Void main ( )
{
base2 B;
Clrscr ( );
B.display ( );
getch ( ) ;
}
Output :
Enter roll no
Roll no = 101
VB : 50
HTML :65
Java : 70
==============
Total =185
Protected Base class inheritance : when a derived class inherited from base class with protected inheritance then it is called protected base class inheritance and in this inheritance all the public or protected members of base class become protected in derived class .
* Program to understand the use of protected derivation * ?
Class A{
Private :int a;
Public :int b ;
Protected : int c'
Void set a ( int i )
{
a = i ;
}
int return a ( )
{
return a ;
}
};
Class B : Protected A
{
Public :
Void setvalue a( )
{
set a (10);
cout <<"value of a ="<<return a ( ) <<"\n\n";
}
Void setvalueb(int j )
{
b = j ;
cout << "value of b ="<<b<< "\n\n";
}
Void setvalue c(int k)
{
c = k ;
cout <<" value of c = "<< c << "\n\n";
}
};
Void main ( )
{
B b1 ;
clrscr ( ) ;
b1. setvalue a ( 10 );
b1 . setvalue b ( 20 );
b1 .setvalue c ( 30 );
getch ( );
}
Output :Value of a = 10
Value of b = 20
Value of c = 30
Multiple Inheritance
Inheritance multiple base classes : In multiple inheritance a derived class inherits the features of more than one base classes .
Class base
{Protected :
Int TA ;
Int DA;
Void gettada( )
{
TA = 500 ;
DA = 600 ;
}
};
Class base2
{
Protected :
Int PA ;
Int HRA;
Void hrapf( )
{
HRA = 1000 ;
HRA = 750;
}
};
Class derived :Public base1 , Public base2
{
Protected :
Int net , basic ;
Public :
Void calculate ( )
{
Basic = 10000;
getdata( );
gethrapf ( );
Net = Basic +Ta +Da +Hra-Pf;
}
Void dispaly ( )
{
cout <<"output :\n\n";
cout <<"net salary ="<<Basic<<"+TA<<"+"<<DA"+"<<HRA<<"-"<<PF<<"="net;
}
};
Void main( )
{
Derived D ;
Clrscr( );
D.calculate ( );
D.display ( );
getch ( );
}
Ambiguity in multiple base class Inheritance : Inheritance when a same function appear in more than one base class , then the problem of ambiguity occur in the program. In this situation when the derived class inherits base class then the derived class contain same function and the time of running the compiler does not specify which function should be invoked . To solve this problem the function should be invoked by using the class name , object of derived class dot operator and scope resolution operator .
Write a Program to solve the ambiguity problem in Inheritance ?
Class A{
Protected : int n;
Public : Void input ( )
{
cout<<"enter number for factorial =";
cin >> n;
}
Void Calculate ( )
{
int i , f =1 ;
for ( i =1 ; i<n ;i++)
{
f * = i;
}
cout <<"fact of no ="<<f<<"\n\n";
}
};
Class B : Public A
{
Protected : int n ;
Void input ( )
{
cout << "enter digit = ";
cin >> n ;
}
Void calculate ( )
{
int rem , s =0;
while (n>0)
{
rem = n%10;
n = n /10;
s = s + rem;
}
cout <<"sum of digit ="<<s<<"\n\n";
}
};
Void main ( )
{
B B1;
clrscr ( ) ;
B1.A ( ) : : input ( );
B1.B : : input ( );
cout << "output \n\n ==================\n\n";
B1.A : : calculate ( );
B1.B : : calculate ( );
getch ( );
}
0 comments:
Post a Comment