0% found this document useful (0 votes)
41 views16 pages

K .J.Somaiya Polytechnic, Mumbai-77: Batch No: CO2

The document discusses various types of inheritance in C++ including single, multiple, hierarchical, multilevel, and hybrid inheritance. It provides code examples and outputs to demonstrate how to implement each type of inheritance through a program. The types of inheritance are defined and examples are given to show single inheritance with two classes A and B, multiple inheritance with classes M, N and P, hierarchical inheritance with classes Father, Son and Daughter, multilevel inheritance with classes Vehicle, FourWheeler and Car, and hybrid inheritance combining hierarchical and multiple inheritance with classes Arithmetic, Plus, Minus and Result.

Uploaded by

ilyas kukshiwala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views16 pages

K .J.Somaiya Polytechnic, Mumbai-77: Batch No: CO2

The document discusses various types of inheritance in C++ including single, multiple, hierarchical, multilevel, and hybrid inheritance. It provides code examples and outputs to demonstrate how to implement each type of inheritance through a program. The types of inheritance are defined and examples are given to show single inheritance with two classes A and B, multiple inheritance with classes M, N and P, hierarchical inheritance with classes Father, Son and Daughter, multilevel inheritance with classes Vehicle, FourWheeler and Car, and hybrid inheritance combining hierarchical and multiple inheritance with classes Arithmetic, Plus, Minus and Result.

Uploaded by

ilyas kukshiwala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

K .J.

Somaiya Polytechnic, Mumbai-77

Batch No: CO2

Enrollment No.: FCOG17131

Experiment No: 19

Experiment Name: Program on single, multilevel, multiple, hierarachical and


hybrid inheritenace .

Programming in C/ IISem /2017-2018


K .J.Somaiya Polytechnic, Mumbai-77

Experiment No.19

Experiment Name:Program on single,multilevel,multiple,hierarachical and hybrid inheritenace .

Objective: Student will able to implement various types of inheritance.

Theory:

Types of Inheritance
In C++, we have 5 different types of Inheritance. Namely,

1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)

1.Single Inheritance:
Single Inheritance: In single inheritance, a class is allowed to inherit from only one class.
i.e. one sub class is inherited by one base class only.

Programming in C/ IISem /2017-2018


2.Multiple Inheritance:
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit
from more than one classes. i.e one sub class is inherited from more than one base
classes.

3.Hierarchical Inheritance:

In this type of inheritance, more than one sub class is inherited from a single base class. i.e.
more than one derived class is created from a single base class.

4.Multilevel Inheritance:
In this type of inheritance, a derived class is created from another derived class.

Programming in C/ IISem /2017-2018


5.Hybrid Inheritance:
Hybrid Inheritance is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance:

Implementation and output :


Write a Program on single,multilevel,multiple,hierarachical and hybrid inheritenace.

1. Single Inheritance:
Input:

#include<iostream.h>

#include<conio.h>

class A

private:

Programming in C/ IISem /2017-2018


int a;

public:

void get_a()

cout<<"Enter a ";

cin>>a;

void put_a()

cout<<a;

};

class B:public A

int b;

public:

void get_b()

cout<<"Enter b ";

cin>>b;

void put_b()

cout<<b;

Programming in C/ IISem /2017-2018


};

int main()

clrscr();

A a1;

a1.get_a();

a1.put_a();

B b1;

b1.get_b();

b1.get_a();

b1.put_a();

b1.put_b();

getch();

return 0;

Output:

Programming in C/ IISem /2017-2018


2. Multiple Inheritance:
Input:

#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get_m(int x)
{m=x;}
};
class N
{
protected:
int n;
public:
void get_n(int y)
{n=y;}
};
class P:public M,public N
{
public:
void display()
{
cout<<"m ="<<m<<endl;
cout<<"n ="<<n<<endl;
cout<<"m * n ="<<m*n;
}
};
void main()
{
clrscr();
P p1;
p1.get_m(10);
p1.get_n(20);
p1.display();
getch();
}

Programming in C/ IISem /2017-2018


Output:

3. Hierarchical Inheritance:
Input:

#include<iostream.h>
#include<conio.h>
class father
{
int age;
char name[20];
public:
void get()
{
cout<<"Enter father's name:";
cin>>name;
cout<<"Enter father's age:";
cin>>age;
}
void show()
{
cout<<"\n Father's name is "<<name;
cout<<"\n Father's age is "<<age;
}
};
class son:public father
{
int age;

Programming in C/ IISem /2017-2018


char name[20];
public:
void get()
{
father::get();
cout<<"Enter son's name:";
cin>>name;
cout<<"Enter son's age:";
cin>>age;
}
void show()
{
father::show();
cout<<"\n Son's name is "<<name;
cout<<"\n Son's age is "<<age;
}
};
class daughter:public father
{
int age;
char name[20];
public:
void get()
{
father::get();
cout<<"Enter daughter's name:";
cin>>name;
cout<<"Enter daughter's age:";
cin>>age;
}
void show()
{
father::show();
cout<<"\n Daughter's name is "<<name;
cout<<"\n Daughter's age is "<<age;
}
};
int main()
{
clrscr();
son s1;
daughter d1;

Programming in C/ IISem /2017-2018


s1.get();
d1.get();
s1.show();
d1.show();
getch();
return 0;
}

Output:

4. Multilevel Inheritance:
Input:

#include<iostream.h>

#include<conio.h>

class vehicle

public:

vehicle()

cout<<"This is a vehicle"<<endl;

Programming in C/ IISem /2017-2018


};

class fourwheeler:public vehicle

public:

fourwheeler()

cout<<"Objects with 4 wheels are vehicles"<<endl;

};

class car:publicfourwheeler

public:

car()

cout<<"Car has 4 wheels"<<endl;

};

int main()

clrscr();

car obj;

getch();

return 0;

Output:

Programming in C/ IISem /2017-2018


5. Hybrid Inheritance:
Input:

#include<iostream.h>
#include<conio.h>
class arithmetic
{
protected:
int num1,num2;
public:
void getdata()
{
cout<<"For addition:";
cout<<"\n Enter the first number:";
cin>>num1;
cout<<"\n Enter the second number:";
cin>>num2;
}
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:

Programming in C/ IISem /2017-2018


int n1,n2,diff;
public:
void sub()
{
cout<<"\n For subtraction:";
cout<<"\n Enter the first number:";
cin>>n1;
cout<<"\n Enter the second number:";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public minus
{
public:
void display()
{
cout<<"\n Sum of "<<num1<<" and "<<num2<<" is "<<sum;
cout<<"\n Difference of "<<n1<<" and "<<n2<<" is "<<diff;
}
};
void main()
{
clrscr();
result z;
z.getdata();
z.add();
z.sub();
z.display();
getch();
}

Output:

Programming in C/ IISem /2017-2018


FLOWCHART:

Programming in C/ IISem /2017-2018


Conclusion – Thus, we learnt to implement various types of inheritance.

Programming in C/ IISem /2017-2018


Programming in C/ IISem /2017-2018

You might also like