Inheritance in c++

 Inheritance

  • It is a process in which one object acquires all the property and behaviour of its parent class automatically.
  • In such way, you can use, extend or modify the attributes and behaviour which are defined in other class.

Types of inheritance supported in C++

  1. Single level inheritance
  2. Multilevel inheritance
  3. Hierarchical inheritance
  4. Multiple inheritance
  5. Hybrid inheritance
syntax for deriving base class into derived class:
class derivedclassname:access_specifier baseClassName{
    //code
}

where,
deriveclassname- it is the name of the derived class.

access_specifier:- access specifier specifies whether the features are publicly, protected or privately inherited.

baseClassName:- it is name of base class which is going to inherit properties and behaviour of derived class.


1. Single level inheritance.

  • In single level inheritance only one derived class is inherited from one base class.
  • here A is base class and B is derived class.




#include<iostream>
using namespace std;
//Single Inheritance in C++ Programming
class A
{
   public:
    void displayA()
    {
        cout<<"Class A."<<endl;
    }
};
class B:public A
{
    public:
    void displayB()
    {
        cout<<"Class B."<<endl;
    }
};
int main()
{
    B o;
    o.displayA();
    o.displayB();
    return 0;
}
// output
// class A
// class B


2. Multilevel inheritance

when class inherits another class which is further inherited by another class it is known as multilevel inheritance. multilevel inheritance is transitive so the last derived class acquires all the members of its base classes for example the Class B inherits Class A and Class C inherits Class B so Class C will have all the properties and methods of class A,B  as well as of Class C.





#include<iostream>
using namespace std;
class A{
  public:
  void displayA(){
      cout<<"Class A"<<endl;
  }
};
class B:public A{
  public:
  void displayB(){
      cout<<"Class B"<<endl;
  }
};

class C:public B{
  public:
  void displayC(){
      cout<<"Class C"<<endl;
  }
};

int main(){
  C o;
  o.displayA()  ;
  o.displayB()  ;
  o.displayC()  ;

    return 0;
}


3.Hierarchical inheritance


It is defined as the process of deriving more than one class from a one base class.





#include<iostream>
using namespace std;
class A{
  public:
  void displayA(){
      cout<<"Class A"<<endl;
  }
};
class B:public A{
  public:
  void displayB(){
      cout<<"Class B"<<endl;
  }
};

class C:public A{
  public:
  void displayC(){
      cout<<"Class C"<<endl;
  }
};

int main(){
  C c;
  B b;
  cout<<"calling from B"<<endl;
  b.displayA()  ;
  b.displayB();
  cout<<"calling from C"<<endl;
  c.displayA()  ;
  c.displayC()  ;
  return 0;
}


4. Multiple inheritance

when a derived class inherits properties and behavioral from 2 different classes i.e from two base classes then it is called as multiple inheritance.







#include<iostream>
using namespace std;
class A{
  public:
  void displayA(){
      cout<<"Class A"<<endl;
  }
};
class B{
  public:
  void displayB(){
      cout<<"Class B"<<endl;
  }
};

class C:public A,public B{
  public:
  void displayC(){
      cout<<"Class C"<<endl;
  }
};

int main(){
  C c;
  c.displayA();
  c.displayB();
  c.displayC();
  return 0;
}


5.Hybrid inheritance

hybrid inheritance is a combination of any multiple inheritance and any other type of inheritance or you can say when 2 or more type of inheritance are existing in a program then it is hybrid inheritance.





#include<iostream>
using namespace std;
class father{
  public:
  void house(){
      cout<<" father's house"<<endl;
  }
};
class son:public father{
  public:
  void car(){
      cout<<"son's car"<<endl;
  }
};

class daughter:public father{
  public:
  void mobile(){
      cout<<"daughter mobile"<<endl;
  }
};

 class student:public daughter{
  public:
  void school(){
      cout<<"daughter goes school"<<endl;
  }
};
int main(){
son s;
daughter d;
student stud;

cout<<"Son can acces"<<endl;
s.car();
s.house();
cout<<"daugther can access"<<endl;
d.mobile();
d.house();
cout<<"student can access:"<<endl;
stud.house();
stud.school();
stud.mobile();
  return 0;
}


Post a Comment

Previous Post Next Post