Types of Inheritance in C++
1.single Inheritance
2. Multiple Inheritance
3. Multilevel
Inheritance
4. Hierarchical
Inheritance
5.
Hybrid (Virtual) Inheritance
1. 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.
Fig:Single Inheritance In C++
Syntax:
class
sub_class: accessmode base_class
{
//body
of subclass
};
Program:
#include<iostream.h>
#include<conio.h>
class
base
{
public:
base()
{
cout<<”base
class created”<<endl;
}
};
class
derived: public base
{
public:
derived()
{
cout<<”derived
class created”<<endl;
}
};
void
main()
{
base
a1;
a1.display();
getch();
}
Output:
Base
class created
2.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.
Fig: Multiple Inheritance |
Syntax:
class
sub_class: accessmode base_class1, accessmode baseclass2,….
{
//body
of subclass
};
Program
#include<iostream.h>
#include<conio.h>
class
base
{
public:
base()
{
cout<<”base
class created”<<endl;
}
};
class
super
{
public:
super()
{
cout<<”
super class created”<<endl;
}
};
class
derived: public base,public super
{
};
void
main()
{
derived
a1;
getch();
}
Output:
Base
class created
Super
class created
3. Multilevel Inheritance:
In
this type of inheritance, a derived class is created from another derived
class.
Fig: Multilevel Inheritance In C++
Syntax:
class baseclass1
{
//body
of base
};
class baseclass2:accessmode baseclass1
{
//body
of base
};
class derivedclass:accessmode baseclass2
{
//body
of derived
};
Program:
#include<iostream.h>
#include<conio.h>
class
base
{
public:
base()
{
cout<<”base
class created”<<endl;
}
};
class
super: public base
{
public:
super()
{
cout<<”
super class created”<<endl;
}
};
class
derived: public super
{
public:
derived()
{
cout<<”derived
class created”;
}
};
void
main()
{
derived
a1;
getch();
}
Output:
Base
class created
Super
class created
Derived
class created
4. 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.
Fig: Hierarchical Inheritance In C++ |
Syntax:
class baseclass
{
//body
of base
};
class derivedclass:accessmode baseclass
{
//body
of derived
};
class derivedclass:accessmode baseclass
{
//body
of derived
};
class derivedclass:accessmode baseclass
{
//body
of derived
};
Program:
#include<iostream.h>
#include<conio.h>
class
base
{
public:
base()
{
cout<<”base
class created”<<endl;
}
};
class
super: public base
{
};
class
derived: public base
{
};
void
main()
{
super
a1;
derived
a2;
getch();
}
Output:
Base
class created
Base
class created
5. Hybrid (Virtual) Inheritance:
Hybrid
Inheritance is implemented by combining more than one type of
inheritance.
Fig: Hybrid Inheritance In C++
Fig: Hybrid Inheritance In C++
Syntax:
class
baseclass
{
//body
of base
};
class
baseclass
{
//body
of base
};
class
derived: accessmode baseclass
{
//body
of derived
};
class
derived: accessmode baseclass,
accessmode baseclass
{
//body
of derived
};
Program:
#include<iostream.h>
#include<conio.h>
class
base
{
public:
base()
{
cout<<”base
class created”<<endl;
}
};
class
super
{
public:
super()
{
Cout<<”super
class created”<<endl;
}
};
class
derived: public base
{
};
class
sub: public base, public super
{
};
void
main()
{
sub
a1;
getch();
}
Output:
Base
class created
Super
class craeted
No comments:
Post a Comment