Search Your Topic

config
Friday, 16 June 2017 07:09

C++ Inheritance

Written by
Rate this item
(0 votes)

In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.

In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.


Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.

C++ Single Level Inheritance Example: Inheriting Fields

When one class inherits another class, it is known as single level inheritance. Let's see the example of single level inheritance which inherits the fields only.

 
  1. #include <iostream>  
  2. using namespace std;  
  3.  class Account {  
  4.    public:  
  5.    float salary = 60000;   
  6.  };  
  7.    class Programmer: public Account {  
  8.    public:  
  9.    float bonus = 5000;    
  10.    };       
  11. int main(void) {  
  12.      Programmer p1;  
  13.      cout<<"Salary: "<<p1.salary<<endl;    
  14.      cout<<"Bonus: "<<p1.bonus<<endl;    
  15.     return 0;  
  16. }  

Output:

Salary: 60000
Bonus: 5000

C++ Single Level Inheritance Example: Inheriting Methods

Let's see another example of inheritance in C++ which inherits methods only.

 
  1. #include <iostream>  
  2. using namespace std;  
  3.  class Animal {  
  4.    public:  
  5.  void eat() {   
  6.     cout<<"Eating..."<<endl;   
  7.  }    
  8.    };  
  9.    class Dog: public Animal    
  10.    {    
  11.        public:  
  12.      void bark(){  
  13.     cout<<"Barking...";   
  14.      }    
  15.    };   
  16. int main(void) {  
  17.     Dog d1;  
  18.     d1.eat();  
  19.     d1.bark();  
  20.     return 0;  
  21. }  

Output:

Eating...
Barking...

C++ Multi Level Inheritance Example

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

Let's see the example of multi level inheritance in C++.

 
  1. #include <iostream>  
  2. using namespace std;  
  3.  class Animal {  
  4.    public:  
  5.  void eat() {   
  6.     cout<<"Eating..."<<endl;   
  7.  }    
  8.    };  
  9.    class Dog: public Animal   
  10.    {    
  11.        public:  
  12.      void bark(){  
  13.     cout<<"Barking..."<<endl;   
  14.      }    
  15.    };   
  16.    class BabyDog: public Dog   
  17.    {    
  18.        public:  
  19.      void weep() {  
  20.     cout<<"Weeping...";   
  21.      }    
  22.    };   
  23. int main(void) {  
  24.     BabyDog d1;  
  25.     d1.eat();  
  26.     d1.bark();  
  27.      d1.weep();  
  28.      return 0;  
  29. }  

Output:

Eating...
Barking?
Weeping?
Read 744 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.