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 j 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

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses