You are on page 1of 5

C++ Inheritance

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. {  
9.    public:  
10.    float bonus = 5000;    
11.    };       
12. int main(void) 
13. {  
14.      Programmer p1;  
15.      cout<<"Salary: "<<p1.salary<<endl;    
16.      cout<<"Bonus: "<<p1.bonus<<endl;    
17.     return 0;  
18. }  
Output:

Salary: 60000
Bonus: 5000

In the above example, Employee is the base class and Programmer is the derived class.

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. }  

C++ Aggregation (HAS-A Relationship)


In C++, aggregation is a process in which one class defines another class as any entity
reference. It is another way to reuse the class. It is a form of association that represents
HAS-A relationship.

C++ Aggregation Example


Let's see an example of aggregation where Employee class has the reference of Address
class as data member. In such way, it can reuse the members of Address class.

1. #include <iostream>  
2. using namespace std;  
3. class Address {  
4.     public:  
5.    string addressLine, city, state;    
6.      Address(string addressLine, string city, string state)    
7.     {    
8.         this->addressLine = addressLine;    
9.         this->city = city;    
10.         this->state = state;    
11.     }    
12. };  
13. class Employee    
14.     {    
15.         private:  
16.         Address* address;  //Employee HAS-A Address   
17.         public:  
18.         int id;    
19.         string name;    
20.         Employee(int id, string name, Address* address)    
21.        {    
22.            this->id = id;    
23.            this->name = name;    
24.            this->address = address;    
25.        }    
26.      void display()    
27.        {    
28.            cout<<id <<" "<<name<< " "<<     
29.              address->addressLine<< " "<< address->city<< " "<<address->state<<endl;    
30.        }    
31.    };   
32. int main(void) {  
33.     Address a1= Address("C-146, Sec-15","Noida","UP");    
34.     Employee e1 = Employee(101,"Nakul",&a1);    
35.             e1.display();   
36.    return 0;  
37. }  

You might also like