Friday, 6 April 2018

Inheritance

Inheritance is the process by which object of one class an acquire the properties of another class.

Inheritance helps for software reusability in which new classes are created from existing classes by absorbing  their attributes and behaviors.

If user want data member and member function of existing class, user can inherit data member and member function of old class with the help of inheritance.

The existing class from which new class is derived is known as base class or parent class or super class. The derived class is known as sub class or child class.

The derived class can add data members and member functions of its own so derived class can be larger than its base class.

When a class inherits another , the member of the base class becomes members of the derived class.

The syntax of deriving new class from old class-

class derivedclassname : accessmodifier baseclassname
 {
   //Body of the class
};


 The above syntax contains

1. The keyword class
2. derivedclassname which is user defined name for new class
3. The : (Colon) indicates that the derived class name is derived from the base class.
4. The access modifier is optional. If no access modifier is present, them access modifier is private by default.
5. baseclassname is the existing class name.

Ex:
class base
{
//body of the base class
};
Class derived1:private base   //private Derivation


};
Class derived2:public base     //public Derivation


};
Class derived3:protected base   //protected Derivation
{

};

Public Inheritance

When the access specifier for the a base class is public that is when base class is publicly inherited by a derived class all public member of the base class becomes public members of the derived class and all the protected members of the base class becomes protected members of the derived class.

Base class private elements remain private to the base class an are not accessible by the members of the derived class.

Program for demonstrating public Inheritance:

#include<iostream.h>
#include<conio.h>
class base
{
int i,j;
public:
void set(int a, int b)
{
i = a;
j = b;
}
void show( )
{
cout<<"Value of i is--->"<<i<<"\tValue of k is--->"<<j<<"\n";
}
};
class derived : public base
{
int k;
public:
derived(int x)
{
k = x;
}
void showk( )
{
cout<<"\nValue of k is--->"<<k<<"\n";
}
};
void main( )
{
derived ob(3);
clrscr( );
ob.set(1,2);
ob.show( );
ob.showk( );
getch( );
}

Output:-
Value of i is--->1             Value of k is--->2
Value of k is--->3


Protected Access Specifiers :
   When user declare class members as protected , these class members are private to their own class but can still be inherited and accessed by derived class.

Program  to demonstrate protected access specifies for data members of class.

#include<conio.h>
#include<iostream.h>
class base
{
protected:
int i,j;
void set(int a, int b)
{
 i = a;
j = b;
}
void show( )
{
cout<<"\nValue of i is--->"<<i<<"\tValue of j is--->"<<j;
}
};
class derived : public base
{
int k;
public:
void setk( )
{
k = i*j;
}
void showk( )
{
cout<<"\tValue of the k is--->"<<k<<"\n";
}
};
void main( )
{
derived ob;
clrscr( )
{
ob.set(2,3);
ob.show( );
ob.setk( );
ob.showk( );
getch( );
}

Output:-
Value of i is--->2     Value of j is--->3    Value of the k is--->6

Private Inheritance

When the base class is inherited by using the private access specifiers, all public and protected members of the base class becomes private members of the derived class.


Write a program to demonstrate private

#include<conio.h>
#include<iostream.h>
class base
{
int i,j;
public:
void set(int a, int b)
{
i = a;
j = b;
}
void show( )
{
cout<<\nValue of i is--->"<<i<<"Value of j is--->"<<j<<"\";
}
};
class derived : private base
{
int k;
public:
derive(int x)
{
k = x;
}
void showk( )
{
cout<<"Value of k is--->"<<k<<"\n";
}
};
void main( )
{
derived ob(3);
clrscr( );
ob.set(1,2);   //ERROR, can not access set
ob.show( );   //ERROR, can not access set
getch( );
}

Note: The program will not compile because you are typing to access private members of the object.

Protected Inheritance 

It is possible to inherit a base class as protected.

When this is done all public and protected members of the base class becomes protected members of the derived class.

Program to demonstrate Protected Inheritance

#include<conio.h>
#include<iostream.h>
class base
{
protected:
int i,j;
public:
void setij(int a, int b)
{
i = a;
j = b;
}
void showij( )
{
cout<<"Value of i is--->"<<i<<"\tValue of j is--->"<<j<<"\n";
}
};
class derived : protected base
{
int k;
public:
void setk( )
{
setij(10,12)
k = i*j;
}
void showall( )
{
cout<<"Value of k is--->"<<k<<"\n";
showij( );
}
};
void main( )
{
derived ob;
clrscr( );
ob.setk( );
ob.showall( );
//ob.showij( );  //Ellegal not accessible
getch( );
}

Output:-
Value of k is--->120
Value of  i is--->10     Value of j is--->12

0 Comments:

Post a Comment

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)