Basic introduction          Inheritance is the element which is of object-oriented programing.          It allows you to create a cl...

Inheritance in C++

/
0 Comments



  • Basic introduction
         Inheritance is the element which is of object-oriented programing.
         It allows you to create a class using an existing class means you can use the properties of                    existing class in your class. Like we are using the properties of our parents…
         Shortly …..It is same as the parent-child relationship.

        Suppose in your college program you want maths teacher, English teacher, and a sports teacher

        They have some same properties like a simple teacher have.  teach(), examine() etc.
        So we are able to use those properties in making several teacher’s class.

        Basic Syntax:

        Child class : Parent class{
         ...........................
         };


  • Types of inheritance



1.Single inheritance: 

  In this a class is allowed to inherit from only one class. Like one father has one child
In this B class's object can access in A class function and variable.
 Example:
#include<iostram.h>
Class A
{
…..
}
Class B : public A
{
…..
}


Program:

#include<iostream.h> #include<conio.h> class Parent //Base class { public: int a; }; class Child : public Parent // Sub class inheriting from Base Class(Parent) { public: int b; }; void main() //main function { Child obj; // An object of class child has all data members and member functions of class parent obj.b = 10; obj.a = 20; cout << "Child b is " << obj.b << endl; cout << "Parent a is " << obj.a << endl; getch(); }


Output:

      Child b is 10
Parent a is 20
       

2.Multiple inheritance: 

a class can inherit from more than one class. In this child will be one and parent will be more than  one
In this A class's object can access in B and C class function and variable.

class Parent1
{
….
}
Class Parent2
{

}
Class child : public Parent1, public Parent2
{

}


Program:

#include <iostream.h>

#include<conio.h>

class One // first base class
{
 public:
a()
{
 cout << "This is a class 1" << endl;
}
};

class Two// second base class

{
 public:
b()
{
    cout << "This is a 2nd class" << endl;
}
};

class Three: public One, public Two// sub class derived from two base classes


{


};


void main() // main function


{   

Three c;// creating object of sub class will

    c.a();// invoke the functionsf base classes


    c.b();


    getch();


}



Output:

  This is a class 1
This is a class 2nd



3.Multilevel inheritance:

 In this one derived class is created from another derived class. Like you have your father and your father have your grandfather. 


In this B class's object can access in C class function and variable.n this Aclass's object can access in Bclass function and variable.Its means A's object can access C class variable and function indirectly.

class GrandParent

{

}
class Parent
{

}
class Child
{

}

Program:



#include <iostream.h>



#include<conio.h>





class one// base class


{

public:

void a()

{

 cout << "This is a class 1" << endl;

 }

};

class two: public one

 public:


void b()

   {

cout<<"This is a class 2"<<endl;

}

};



class three: public two // sub class derived from two base classes

{

 public:

c()

{

cout<<"This is a class 3"<<endl;

 }


};



void main() // main function

{   

 three z;//creating object of sub class will

z.a();//invoke the constructor of base classes

z.b();

z.c();

getch();


}


Output:
 This is a class 1
This is a class 2
This is a class 3

4.Hierarchical inheritance: 

In this a tree is made of inherited classes but the base or parent  class is only one. Like Your grandfather have two son your fater and your uncle . And your father and uncle have two sons.
In A& C class object variable can access B and G class function and variables. And this will apply also on D & F class.

Class G
{

}
Class B : public G
{

}
Class E : public G
{

}
Class A : public B
{

}
Class C : public B
{

}
Class D : public E
{

}
Class F : public E
{

}


Program:





#include <iostream,h>



#include<conio.h>







class one// base class






 public:


a()




{
cout << "This is a 1 class" << endl;


 }


};

class two: public one // first sub class 


{


};





class three: public one // second sub class


{






};






void main() // main function


{   










  one obj1; // creating object of sub class will


  two obj2;// invoke the constructor of base class


obj1.a();


obj2.b();


getch();

}


Output:

   This is a 1 class
This is a 1 class

5.Hybrid inheritance: 

it is made by combining more than one inheritance. 

Class F

{

}
Class B : public F
{

}
Class E : public F
{

}
 Class G : public E
{

}
Class A : public B
{

}
 Class C : public B
{

}


 List out visibility Of inherited members in various categories Of inheritance. 

Protected: 
  • This access modifier plays a key role in inheritance. 
  • Protected members of the class can be accessed within the class and from derived class but cannot be accessed from any other class or program. 
  • It works like public for derived class and private for other programs 



Programs: Do It by your own




You may also like

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts