Vous êtes sur la page 1sur 37

Inheritance

6/04/11 & 08/04/11

Definition
It is the process of reusing existing class program which is already been debugged and tested without any modification. The new class is called derived class and the existing class is called base class.

Types of Inheritance
Single Inheritance Multiple Inheritance Multi-Level Inheritance

Single Inheritance
When a class inherits a single class it is called single inheritance. Ex : class D is the derived class and class B is the base class Class B base class access_specifiers (public or protected or private) Derived class Class D

Declaration Syntax
In case of single inheritance, the derived class is declared as : class D : access_specifier B The access specifier is either public or protected or private. ex: for public inheritance the code for declaration of class D be : class D : public B

Single Public Inheritance


In this kind of inheritance the public function of base class may be accessed directly by derived class members, i.e. derived class inherits these functions. The objects of derived class can directly access the public function of base class.

Example
#include<iostream> using namespace std; class B { public : int m, a ; int product1() { return a*m ; } }; // end of base class

Contd..
class D : public B // declaration of derived class D { public : int n; int Product2() { return n*Product1() ; } }; // end of class D

Contd..
int main() { D C1 ; //C1 is an object of class D C1.m = 5; //data directly accessed by object C1.a = 4; C1.n = 3; cout<<Product1 =<<C1.Product1()<<endl; cout<<Product2 =<<C1.Product2()<<endl; return 0 ; }

Base class has public and protected members


Out of private and protected members of base class, only protected members may be accessed by derived class through its own public function members while the private members of base class are not visible in derived class.

Example
#include<iostream> using namespace std; class B //declaration of class B { protected : int m ; public : int k ; int square(){return k*k ; } int Product(){return k*m; } }; // end of class B

Contd..
class D : public B //class D inherited from class B { public : void setvalue1(int a)//public function to access m of B {m=a;} }; // end of class D

Contd..
int main() { D C; //C is declared an object of D C.k = 9 // Direct access to k of class B C.setvalue1(6); //Access to m through public // member of D cout<<Square =<<C.Square()<< , Product = <<C.Product() <<endl ; return 0; }

Public inheritance with base class having private data


#include<iostream> using namespace std ; class B { private : // private access int m ; }; //end of class B

Contd..
class D : public B // Declaration of class D { public : int a; void setvalue() // function for accessing m of class B { m = a; } int n ; }; // end of class D

Contd..
int main() { D C ; // Cis an object of class D C.a = 5; C.n = 4; int product = C.a*C.n ; cout<<Product of members of B and D= <<product<<endl ; cout<<sum of square of members of B & D = <<C.n*Cn + C.a*C.a <<endl ; return 0; }

Output
Cannot access private member declared in class B To access the private data members or private function members of base class, corresponding public function which provide access to private members of base class

Contd..
The private members of a base class can only be accessed through public and protected functions of base class or by friend functions of base class. But friend functions are not inherited.

Contd..
#include<iostream> using namespace std; class B { //base class B private : int x; // private member of B int Square() { return x*x ; }//private member of //class B public : void setvalue (int p) ; // setvalue()- a public member //for accessing the private member x of B

Contd..
int Psquare() { return Square (); } // A public //member of B to access private member Square() int m ; }; // end of class B void B : setvalue (int p) { x = p ; }; //definition of setvalue() class D : public B // declaration of D with public // inheritance { public : int n ; } ; // end of class D

Contd..
int main () { D C // C is an object of class D C.setvalue(5) ; // accessing x of B through setvalue() C.m = 4; // accessing public member of B directly. C.n = 3; // accessing public member of D directly. cout<<Product of m and n =<<C.m*C.n <<endl ; cout <<Square =<<C.Psquare<<endl ; return 0 ; }

Single Protected Inheritance


In case of protected inheritance the public members and the protected members of base class become members of derived class . The private 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
#include<iostream> using namespace std; class B { protected : //Access protected int m; int Square () { return m*m;} public : //Access public int k ; product() { return k*m;} }; // end of class B

Contd..
class D : protected B // Inheritance protected { public : void setvalue1(int a) // public member to access m { m = a; } void setvalue2(int p) { k = p ; }//public member to //access k int dsquare() {return B :: Square();} //For accessing Square()

Contd..
int dproduct() { return B :: product() ; } // For accessing product() } ; // end of class D int main() { D C ; // C is an object of D C.setvalue2(10) ; C.setvalue1(5); cout<< Product of m and k =<<C.dproduct()<<endl; cout<<Square =<<C.dsquare()<<endl; return 0; }

Example
#include<iostream> using namespace std ; class B { //base class private : int m; //private member data int Square() { return m*m ; } // private member function

Contd..
public : void setvalueb (int a) { m = a ; } // public member to access m int bSquare() { return Square(); } // for accessing Square() int k ; int product () { return k*k ;} }; // end of class B

Contd..
class D : protected B //protected inheritance { public : void setvalued (int n) { setvalueb(n); } void setvalue2 (int p) { k = p ; }//For accessing k int dbsquare() { return bSquare() ; } // For accessing bSquare int dproduct() { return product(); } // for accessing product() }; // end of class D

Contd..
int main() { D C; //C is an object of class D C.setvalue2(10); C.setvalued(5); cout<<Product =<<C.dproduct()<<endl; cout<<Square =<<C.dbsquare()<<endl; return 0; }

Single private inheritance


With private inheritance the public and protected members of base class become private members of derived class. Hence they cannot be accessed through directly by an object of derived class. However, they can be accessed through public function members of derived class. The private members of base class are not visible in the derived class, they can be accessed only through public and protected member functions of the base class.

Example for public and protected members of base class become private members in derived class
#include<iostream> using namespace std; class B{ protected : int m ; public : int k; }; // end of class B

contd..
class D : private B // private inheritance { public : int a; void setvalue(){ m = a ; } int n ; }; // end of class D

Contd..
int main() { D C; //C is an object of class D C.k = 6 ; C.a = 5 ; C.n = 4 ; int product = C.a * C.n ; cout<<Product of members of B and D=<<product<<endl; cout<<Sum of squares of members of B and D = <<C.n *C.n + C.a*C.a <<endl; return 0; }

Expected output
An error massage that k cannot be accessed directly. k ; cannot access public member declared in class B

Example for private inheritance


#include<iostream> using namespace std; class B { protected : int m ; public : int k; int Square() { return k*k; } int msquare () { return m*m; } }; // end of class B

Contd..
class D : private B // private inheritance { public : int a ; void setvalue1() // public member of D to access { m = a ; } // protected member of B void setvalue2 (int b) { k = b ;} // public member of D required to access public //member of B. Because of private inheritance. Same applies //to following also. int dmsquare() { return msquare () ; } int Dsquare() { return B : Square () ; } };

Contd..
int main() { D C; // C is an object of class D C.setvalue2(6); C.a = 5 ; cout << Square = << C.Dsquare() << endl ; cout <<square of m = <<C.dmsquare()<<endl; return 0 ; }

Vous aimerez peut-être aussi