Date post: | 05-Apr-2018 |
Category: |
Documents |
Upload: | abhishek-modi |
View: | 219 times |
Download: | 0 times |
of 37
7/31/2019 Inheritance 1 & 2
1/37
Inheritance
6/04/11 & 08/04/11
7/31/2019 Inheritance 1 & 2
2/37
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.
7/31/2019 Inheritance 1 & 2
3/37
Types of Inheritance
Single Inheritance
Multiple Inheritance
Multi-Level Inheritance
7/31/2019 Inheritance 1 & 2
4/37
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
base class
access_specifiers (public or protected or private)Derived class
Class B
Class D
7/31/2019 Inheritance 1 & 2
5/37
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 fordeclaration of class D be :
class D : public B
7/31/2019 Inheritance 1 & 2
6/37
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.
7/31/2019 Inheritance 1 & 2
7/37
Example
#include
using namespace std;
class B
{
public :
int m, a ;
int product1() { return a*m ; }
}; // end of base class
7/31/2019 Inheritance 1 & 2
8/37
Contd..
class D : public B // declaration of derived class D
{
public :int n;
int Product2() { return n*Product1() ; }
}; // end of class D
7/31/2019 Inheritance 1 & 2
9/37
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
7/31/2019 Inheritance 1 & 2
10/37
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.
7/31/2019 Inheritance 1 & 2
11/37
Example
#include
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
7/31/2019 Inheritance 1 & 2
12/37
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
7/31/2019 Inheritance 1 & 2
13/37
Contd..
int main()
{
D C; //C is declared an object of D
C.k = 9 // Direct access to k of class BC.setvalue1(6); //Access to m through public
// member of D
cout
7/31/2019 Inheritance 1 & 2
14/37
Public inheritance with base class
having private data
#include
using namespace std ;
class B{
private : // private access
int m ;}; //end of class B
7/31/2019 Inheritance 1 & 2
15/37
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
7/31/2019 Inheritance 1 & 2
16/37
Contd..
int main()
{ D C ; // Cis an object of class D
C.a = 5;
C.n = 4;
int product = C.a*C.n ;
cout
7/31/2019 Inheritance 1 & 2
17/37
Output
Cannot access private member declared in class B
To access the private data members or privatefunction members of base class, corresponding
public function which provide access to private
members of base class
7/31/2019 Inheritance 1 & 2
18/37
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.
7/31/2019 Inheritance 1 & 2
19/37
Contd..
#include
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
7/31/2019 Inheritance 1 & 2
20/37
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
7/31/2019 Inheritance 1 & 2
21/37
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
7/31/2019 Inheritance 1 & 2
22/37
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.
7/31/2019 Inheritance 1 & 2
23/37
Example
#include
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
7/31/2019 Inheritance 1 & 2
24/37
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()
7/31/2019 Inheritance 1 & 2
25/37
7/31/2019 Inheritance 1 & 2
26/37
Example
#include
using namespace std ;
class B { //base class
private :
int m; //private member data
int Square() {
return m*m ;
} // private member function
7/31/2019 Inheritance 1 & 2
27/37
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
7/31/2019 Inheritance 1 & 2
28/37
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
7/31/2019 Inheritance 1 & 2
29/37
Contd..
int main()
{
D C; //C is an object of class D
C.setvalue2(10);C.setvalued(5);
cout
7/31/2019 Inheritance 1 & 2
30/37
Single private inheritance
With private inheritance the public and protectedmembers of base class become private membersof 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 visiblein the derived class, they can be accessed onlythrough public and protected member functionsof the base class.
l f bli d d
7/31/2019 Inheritance 1 & 2
31/37
Example for public and protected
members of base class become private
members in derived class#include
using namespace std;
class B{protected :
int m ;
public :int k;
}; // end of class B
7/31/2019 Inheritance 1 & 2
32/37
contd..
class D : private B // private inheritance
{
public :
int a;
void setvalue(){ m = a ; }
int n ;}; // end of class D
7/31/2019 Inheritance 1 & 2
33/37
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
7/31/2019 Inheritance 1 & 2
34/37
Expected output
An error massage that k cannot be accessed
directly.
k ; cannot access public member declared in
class B
7/31/2019 Inheritance 1 & 2
35/37
Example for private inheritance
#include
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
7/31/2019 Inheritance 1 & 2
36/37
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 () ; }
};
7/31/2019 Inheritance 1 & 2
37/37
Contd..
int main()
{ D C; // C is an object of class D
C.setvalue2(6);
C.a = 5 ;
cout