Inheritance strongly supports the concept of reusability i.e. newly created class reuses the properties of already existing ones. Access specifier decides the way in which the base class member will be inherited by the derived class. There are many ways to achieve inheritance single, multiple, Hierarchical, Multilevel, Hybrid. Whereas our main topic of discussion is the difference between single and multiple inheritance. In single inheritance, we do have only one base class which is inherited by only one derived class. In multiple inheritance, we have more than two base classes which are combinely inherited by only one derived class.
Content: Single Vs Multiple Inheritance
Comparison Chart
BASIS FOR COMPARISON | SINGLE INHERITANCE | MULTIPLE INHERITANCE |
---|---|---|
Basic | Derived class inherits a single base class. | Derived class inherits two or more than two base class. |
Implementation | Class derived_class : access_specifier base class | Class derived _class: access_specifier base_class1, access_specifier base_class2, .... |
Access | Derived class access the features of single base class | Derived class access the combined features of inherited base classes |
Visibility | Public, Private, Protected | Public, Private, Protected |
Run time | Require small amount of run time over head | Require additional runtime overhead as compared to single inheritance |
Definition of Single Inheritance
In Single inheritance, there is a single base class and a single derived class. Derived class inherits the base class either publicly, protectedly and privately. The members of the base class can be accessed by derived class according to the access specifier specified during inheriting the base class.
Let’s have a real life example we have two classes, an “account class” and a” saving account class” . Here “saving account class” can inherit the feature of “account class”, so “account class” class will become base/super/parent class for saving account class, and “saving account class” will be a derived class.
Here “account class’s” attributes are acc_no (private) and balance (public),and member functions are initialize(), get_accno() which are public. Now, “account class” get inherited to “saving account class” publicly so, all public member of “account class” can be directly accessed by “saving account class”. It can access the private member of “account class” through public member functions of “account class” but can’t access them directly.
Implementation of Single Inheritance:
- # include<iostream>
- using name space std;
- class Account{
- int acc_no, ;
- public:
- float balance;
- void initialize(int x, int y){
- acc_no=x;
- balance=y;
- }
- int get_accno.(){
- return acc_no;
- }
- } ;
- class Saving_acc : public Account{
- float intrest_rate;
- public :
- //constructor of Saving_acc class
- Saving_acc(int c){
- interest_rate=c;
- }
- void display (){
- cout<<account no<< get_a(); // acc_no is private to Saving_acc
- cout<<"revised balance"<<balance*intrest_rate<<"\n";//balance is public to Saving_acc
- }
- };
- int main()
- {
- Saving_acc S(0.07); //constructor of Saving_acc initialized
- S.initialize(24494, 6000); // object of Saving_acc class called
- //the member function Account class
- S.display();
- return 0;
Definition of Multiple Inheritance
Multiple inheritance allows derived class to inherit combined features of more than one base class i.e. we do have single derived class and multiple base classes. Every base class has to be inherited by mentioning the separate access specifier for each of them. A derived class can access the members of base classes based on the access specifier by which the base class is inherited.
Lets make it easier with the example we have three classes i.e. Bike, Car, and Vehicle . Now, “Vehicle” can inherit the features of “Bike” as well as “Car”. So, “Vehicle” becomes derived class and “Bike” and “Car” becomes the base class. Now, “Bike” and “Car” are publicly inherited by “Vehicle” it can access all public member of “Bike” and “Car” but as we have an entity Model_no protected in “Car” so it private to Car but can be accessed by “Vehicle”.
Implementation of Multiple Inheritance:
- # include<iostream>
- using namespace std;
- class Bike{
- int engine_cost;
- ;
- public:
- void set_engine_cost(int x){
- engine_cost=x;
- }
- };
- class Car{
- protected:
- int Model_no;
- public:
- void set_Model_no(int p){
- Model_no=p;
- }
- };
- class Vehical: public Bike, public car{
- int no_of_wheels
- public:
- vehical(int w){
- no_of_wheels=w;
- cout<<"no of wheels"<<no_of_wheels;
- }
- void display_Vehical_info( ){
- cout<<"engine cost of bike"<< engine_cost<<"\n";// illegal because engine_cost is private
- //to Bikeand vehicle ca't access it directly.
- cout<<""Model no of car is"<< Model_no<<"\n // legal because Model_no is private
- //to Car but accessible by vehicle.
- }
- };
- int main(){
- Vehicle V(4);
- V.set_engine_cost(12000);
- V.set_Model_no(1021);
- V.display_Vehical_info( );
- }
Key Differences Between Single and Multiple Inheritance
- Single inheritance is one derived class having a single base class whereas, in multiple inheritance, has two or more than two base classes, but single derived class.
- Multiple inheritance is quite confusing as here a single derived class inherit two or more base class. If the base classes have an attribute or function with the same name then for derived class, it becomes difficult to identify that which base class’s attribute or function it is it has to derive.
- Overhead issues are lesser in case of single inheritance. But in case of multiple inheritance the object construction or destruction invokes the constructors and destructor of the parent class in the class hierarchy which increases the overhead.
- Single inheritance is more towards specialization. Whereas multiple inheritance is more towards generalization.
- As single inheritance has less overhead it has less runtime as compared to multiple inheritance.
Conclusion
Inheritance makes the programmer’s work easy as if one class is already formed its features can be adapted by other if required. Although the access specifier limits the access to members of a base class in a certain way, but it makes data more secure. Single inheritance is somehow more simpler and easy to implement than multiple inheritance. Inheritance reduces the size of object code, but also affects the run time of the program.
No comments:
Post a Comment
commnet here