Thursday, August 26, 2021

types of inheritance


Types of inheritance

1.       Single inheritance

2.       Multiple inheritance

3.       Heretical inheritance

4.       Multilevel inheritance

5.       Hybrid inheritance

Single inheritance:

it has single base class and single derived class it consists of at least 2 classes

example:

animal derived from leaving things

car derived from vehicle

program:

in this example a single base and child classes exist so it is single

 inheritence

#include<iostream>
using namespace std;
class animal
{
    public:
    int legs=4;
    int tail=1;
};
class dogpublic animal
{
    public:
    void disp()
    {
    cout<<"i provide security in day because i sleep in day"<<endl;
}
};
int main()
{
    dog d1;
    cout<<"legs are ="<<d1.legs<<endl;
    cout<<"tail ="<<d1.tail<<endl;
    d1.disp();
}

multiple inheritance:

in this type of inheritance, a single child class derived from two or more classes

example:

petrol derived from liquid and fuel

child class derived from mother and father

example program:

#include<iostream>

Using namespace std;

Int main ()

{

Class father

{

};

Class mother

{

};

Class child: public father, mother

{

};

error ambiguity or diamond problem

sample A: display ();

sample. b: display ();

hieratical inheritance:

in this type of inheritance multiple child classes inherits from a single parent class

example:

civil, mechanical and electrical derived from engineer class

multilevel inheritance:

in this type of inheritance, the derived class inherit from a class that inherited from another derived class

child father grandfather

hybrid inheritance:

hybrid inheritance is a method where one or more types of inheritance are combined together

No comments:

Post a Comment

How to apply for Canada visa?

  Applying for a Canada Visa: A Comprehensive Guide Explore the Beauty of Canada - Your Step-by-Step Visa Application Guide Introduction Can...