Inheritance is one of the feature provided by Object Oriented Programming. Inheritance is actually used for re-usability of existing class. Using inheritance it is possible to declare a class with required details and implement the same class. But after implementing this class if further requirements are there to extend the same class with additional new features, then a new class is written with new requirements and this new class is connected with previously defined class and this concept of extending the old class with new features is called as inheritance.
In inheritance old class is called as base class and new class is called as derived class.
There are five different types of inheritance. They are
1) Single/Simple inheritance
2) Multiple inheritance
3) Multi level inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
General syntax for deriving a new class from old class is as follows
Derived class/new class : access specifier base class
In our program we are using multiple inheritance and hence I will say something about this inheritance. Here multiple word is used for base classes. It means in multiple inheritance there may be more than two base classes and each are separated by using comma.
e.g. class abc : public xyz, public mnp
here abc is the derived class and xyz and mnp are base classes. in syntax after derived class : (colon) is used as a separator between derived class and base class.
Following is the program to show the multiple inheritance.
Please include iostream.h and conio.h header files before declaration of class test1.
//PROGRAM TO IMPLEMENT MULTIPLE INHERITANCE.
void get(int m1)
void input(int m2)
class result:public test1, public test2
cout<<"\nMarks in first test are "< cout<<"\nMarks in second test are "< total=t1+t2;
cout<<"\nTotal of two test marks are "< avg=float(total)/2;
cout<<"\nAverage of two test marks are "< }
cout<<"\nEnter the marks(out of 25) in first test\n";
cout<<"\nEnter the marks(out of 25) in second test\n";
class result r;
While writing programs using inheritance, normally object of derived class is created. As derived class can have access to its own members(variables and functions) as well as members of base class/classes.
In above program both base class have protected members. When base class having protected members gets inherited in public mode then all protected members of base class becomes protected members of derived class and ready for further inheritance. Protected accesses specifier give access to base class members from derived class. If base class members are private then they can not be accessed in any way by derived class object.
Here in above program object of derived class(result is the derived class) is created. Using this derived class object program get access to base class members as well as its own members.
Above program has a function named as display() in derived class. In this function one statement is used which is
This is called as casting of a variable. Here casting means to change the type of total variable which is integer basically to float. But this is a temporary change during this statement only.