0% found this document useful (0 votes)
51 views10 pages

Forms of Inheritance

C++ supports five types of inheritance: 1. Single inheritance allows a derived class to inherit from one base class. 2. Multiple inheritance allows a derived class to inherit from more than one base class. 3. Hierarchical inheritance allows multiple derived classes to inherit from a single base class. 4. Multilevel inheritance allows a derived class to inherit from another derived class. 5. Hybrid (virtual) inheritance combines hierarchical and multilevel inheritance, allowing a class to inherit from multiple base classes that are themselves derived from another common base class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views10 pages

Forms of Inheritance

C++ supports five types of inheritance: 1. Single inheritance allows a derived class to inherit from one base class. 2. Multiple inheritance allows a derived class to inherit from more than one base class. 3. Hierarchical inheritance allows multiple derived classes to inherit from a single base class. 4. Multilevel inheritance allows a derived class to inherit from another derived class. 5. Hybrid (virtual) inheritance combines hierarchical and multilevel inheritance, allowing a class to inherit from multiple base classes that are themselves derived from another common base class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Different forms of inheritance

C++ offers five types of Inheritance. They are:


 Single Inheritance
 Multiple Inheritance
 Hierarchical Inheritance
 Multilevel Inheritance
 Hybrid Inheritance (also known as Virtual Inheritance)
1. Single Inheritance
In this type of inheritance one derived class inherits from only one base class. It is the
simplest form of Inheritance.

Syntax:
class A
{
…….
};
class B: access_specifier A
{
……
}

Example:
#include<iostream.h>
class employee
{
public:
int salary; OUTPUT:
Enter employee salary: 50000
}; Employee salary: 50000
class developer : public employee
{
employee e;
public:
void salary()
{
cout<<"Enter employee salary: ";
cin>>e.salary; // access base class data member
cout<<"Employee salary: "<<e.salary;
}
};
int main()
{
developer obj;
obj.salary();
return 0;
}
2. Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two
base classes.

Syntax:
class A
{
……….
};
class B
{
………
};
class C: access_specifier A, access_specifier B
{
……..
};
Example:
#include <iostream>
using namespace std;
class A
{
public:
int x;
void getx()
{
cout << "enter value of x: "; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "enter value of y: "; cin >> y;
}
};
class C : public A, public B //C is derived from class A and class B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
OUTPUT:
enter value of x: 5
enter value of y: 4
Sum = 9
int main()
{
C obj1; //object of derived class C
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
}
3. Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.

Syntax:
class A // base class
{
..............
};
class B : access_specifier A // derived class from A
{
...........
};
class C : access_specifier A // derived class from A
{
...........
};
class D : access_specifier A // derived class from A
{
...........
};
Example:
// hierarchial inheritance.cpp
#include <iostream>
using namespace std;
class A //single base class
{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class C : public A //C is also derived from class base
{
public:
void sum() OUTPUT:
Enter value of x and y:
{
2
cout << "\nSum= " << x + y; 3
} Product= 6
Enter value of x and y:
};
2
int main() 3
{ Sum= 5
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
4. Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits
from some other class. The Super class for one, is sub class for the other.

Syntax:
class A // base class
{
...........
};
class B : acess_specifier A // derived class
{
...........
};
class C : access_specifier B // derived from derived class B
{
...........
};
Example:
#include <iostream>
using namespace std;
class A1 //single base class
{
public:
int x;
void getdata()
{
cout << "Enter value of x= ";
cin >> x;
}
};
class A2 : public A1 // derived class from base class A1
{
public:
int y; Output:
Enter value of x= 2
void readdata()
Enter value of y= 3
{ Enter value of z= 3
cout << "\nEnter value of y= "; Product= 18

cin >> y;
}
};
class A3 : public A2 // derived from class A2
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= ";
cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};
int main()
{
A3 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}
5. Hybrid (Virtual) Inheritance
Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.

Syntax:
class A
{
.........
};
class B : public A
{
..........
};
class C : public A
{
...........
};
class D : public B, public C
{
...........
};
Example:
// hybrid inheritance.cpp
#include <iostream>
using namespace std;
class A
{
public:
int x;
};
class B : public A
{
public:
B() //constructor to initialize x in base class A
{
x = 10;
} OUTPUT:
sum=5
};
class C
{
public:
int y;
C() //constructor to initialize y
{
y = 4;
}
};
class D : public B, public C //D is derived from class B and class C
{
public:
void sum()
{
cout << "Sum= " << x + y;
}
};
int main()
{
D obj1; //object of derived class D
obj1.sum();
return 0;
}

You might also like