You are on page 1of 28

8.

Inheritance
(a).single inheritance
Aim:
To write a c++ program to demonstrate single inheritance function.

Algorithm:
● step 1: Start the program
● step 2: create the base class A with a default constructor
Class A
{
Public:
A()
}

● step 3: create the child class B with a default constructor


Class B
{
Public:
B()
}
● step 4: call the base class from the child class like
Class B : Class A
● step 5: create the object for the child class to perform the operation
● step 6: end.
Program:
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"1"<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"2";
}
};
int main()
{
B b;
return 0;
}
Output:
1
2
Result:
Thus the program for single inheritance is successfully executed.
(b).Multilevel inheritance
Aim:
To write a c++ progam to demonstrate Multilevel inheritance function.

Algorithm:
● Step 1: Start the program
● Step 2: create the base class A with a default constructor
Class A
{
Public:
A()
}
● Step 3: create the class B with a default constructor
Class B
{
Public:
B()
}
● Step 4: create the class C with a default constructor
Class C
{
Public:
C()
}
● Step 5: call the base class B from the child class C
Class C : public B
● Step 6: call the base class A from the child class B
Class B : public A
● Step 7: create the object for the class C to perform the operation
● step 8: end.
Program:
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"1"<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"2"<<endl;
}
};
class C:public B
{
Public:
C()
{
cout<<"3";
}
};
int main()
{
C c;
return 0;
}
Output:
1
2
3
Result:
Thus the program for multilevel inheritance is successfully executed.
(c).Multiple inheritance
Aim:
To write a c++ progam to demonstrate Multiple inheritance function.

Algorithm:
● Step 1: Start the program
● Step 2: create the base class A with a default constructor
Class A
{
Public:
A()
}
● Step 3: create the class B with a default constructor
Class B
{
Public:
B()
}
● Step 4: create the class C with a default constructor
Class C
{
Public:
C()
}
● Step 5: call the base class B from the child class C
Class C : public B
● Step 6: call the base class A from the child class C
Class C : public A
● Step 7: create the object for the class C to perform the operation
● step 8: end.
Program:
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"1"<<endl;
}
};
class B
{
public:
B()
{
cout<<"2"<<endl;
}
};
class C:public B,public A
{
public:
C()
{
cout<<"3";
}
};
int main()
{
C c;
return 0;
}

Output:
2
1
3
Result:
Thus the program for multilevel inheritance is successfully executed.
(d).Hierarchical inheritance
Aim:
To write a c++ program to demonstrate hierarchical inheritance.

Algorithm:
● Step 1: Start the program
● Step 2: create the base class A with a default constructor
Class A
{
Public:
A()
}
● Step 3: create the class B with a default constructor
Class B
{
Public:
B()
}
● Step 4: create the class C with a default constructor
Class C
{
Public:
C()
}
● Step 5: create the class D with a default constructor
Class D
{
Public:
D()
}
● Step 6: call the base class A from the child class B
Class B : public A
● Step 7: call the base class A from the child class C
Class C : public A
● Step 8: call the base class A from the child class D
Class D : public A
● step 9: create the object for the class B, class C and class D to perform the
operation
● Step 10: end.
Program:
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"1"<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"2"<<endl;
}
};
class C:public A
{
public:
C()
{
cout<<"3"<<endl;
}
};
class D:public A
{
public:
D()
{
cout<<"4";
}
};

int main()
{
B b;
C c;
D d;
return 0;
}
Output:
1
2
1
3
1
4
Result:
Thus the program for multilevel inheritance is successfully executed.
(e).Hibrid inheritance
Aim:
To write a c++ program to demonstrate hibrid inheritance.

Algorithm:
● Step 1: Start the program
● Step 2: create the base class A with a default constructor
Class A
{
Public:
A()
}
● Step 3: create the class B with a default constructor
Class B
{
Public:
B()
}
● Step 4: create the class C with a default constructor
Class C
{
Public:
C()
}
● Step 5: create the class C with a default constructor
Class C
{
Public:
C()
}

● Step 6: call the base class A from the child class B


Class B : public A
● Step 7: call the base class B and class C from the child class D
Class D : public B,public C
● step 8: create the object for the class B, class C and class D to perform the
operation
● Step 9: end.
Program:
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"1"<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"2"<<endl;
}
};
class C
{
public:
C()
{
cout<<"3"<<endl;
}
};
class D:public B,public C
{
public:
D()
{
cout<<"4";
}
};

int main()
{
D d;
return 0;
}
Output:
1
2
3
4
Result:
Thus the program for multilevel inheritance is successfully executed.

You might also like