MOCKSTACKS
EN
Questions And Answers

More Tutorials








C++ Inheritance

In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation. An inherited class is called a subclass of its parent class or super class. The term "inheritance" is loosely used for both class-based and prototype-based programming.

In C++ when creating a superclass we can inherit it using : when defining child class.

C++ Inheritance Example

First let's take below example a try to change it to use inheritance.

#include <iostream>
#include <string>
using namespace std;

class Car {        
 public:              
    string color;        
    string make;         
    string model;
 public:             
    string getColorAsMessage() { 
      return "My color is: " + color;
    }
};

class Truck {        
 public:              
    string color;        
    string make;         
    string model;
};


int main() {
  Car newCar;  

  newCar.color = "Red";
  newCar.make = "BMW";
  newCar.model = "X5";

  cout << newCar.getColorAsMessage() << "\n"; 
  cout << newCar.make << "\n"; 
  
  
  Truck newTruck;  

  newTruck.color = "White";
  newTruck.make = "Chevy";
  newTruck.model = "Silverado";

  cout << newCar.color << "\n"; 
  cout << newCar.make << "\n"; 
  return 0;
}


In the above example you can see how we have same attributes in both classes: Car, Truck. so we decided to create a parent class called Vehicle and we moved all attributes and methods to Vehicle class and we inherited it using keyword: : public Vehicle

Usage of inheritance in C++ for above example

#include <iostream>
#include <string>
using namespace std;

class Vehicle {
 public:              
    string color;        
    string make;         
    string model;
 public:             
    string getColorAsMessage() { 
      return "My color is: " + color;
    }
};

class Car : public Vehicle {      // here we are inheriting class Vehicle  

};

class Truck : public Vehicle {    // here we are inheriting class Vehicle     

};


int main() {
  Car newCar;  

  newCar.color = "Red";
  newCar.make = "BMW";
  newCar.model = "X5";

  cout << newCar.getColorAsMessage() << "\n"; 
  cout << newCar.make << "\n"; 
  
  
  Truck newTruck;  

  newTruck.color = "White";
  newTruck.make = "Chevy";
  newTruck.model = "Silverado";

  cout << newTruck.getColorAsMessage() << "\n"; 
  cout << newTruck.make << "\n"; 
  return 0;
}

Output

My color is: Red
BMW
My color is: White
Chevy

Conclusion

In this page (written and validated by ) you learned about C++ Inheritance . What's Next? If you are interested in completing Cpp tutorial, your next topic will be learning about: Cpp Function Overriding.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.