MOCKSTACKS
EN
Questions And Answers

More Tutorials








C++ Function Overriding

Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. It allows for a specific type of polymorphism (subtyping). The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.

C++ Function Overriding

C++ Example Of Function Overriding

If you look at below Example you will see we have function getColorAsMessage() exist in 3 classes: Vehicle, Car, Truck.

getColorAsMessage() in Vehicle Class (parent class) it will print the message: My color is: ...

getColorAsMessage() in Car Class (child class) and it will print the message: The Car color is: ... it will override the output of parent class as it's inherited from Vehicle class.

getColorAsMessage() in Truck Class (child class) and it will print the message: The Truck color is: ... it will override the output of parent class as it's inherited from Vehicle class.

Look at the comments in the code.

#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 {     

 public:             
    string getColorAsMessage() {  // Method Overriding
      return "The Car color is: " + color; // Original Method in Vehicle class print My color is:...
  }
};

class Truck : public Vehicle {    

 public:             
    string getColorAsMessage() { // Method Overriding
      return "The Truck color is: " + color;  // Original Method in Vehicle class print My color is:...
  }
};


int main() {

  // Car section
  Car newCar;  
  newCar.color = "Red";
  newCar.make = "BMW";
  newCar.model = "X5";
  cout << newCar.getColorAsMessage() << "\n"; 
  cout << newCar.make << "\n"; 
  
  // Truck section
  Truck newTruck;  
  newTruck.color = "White";
  newTruck.make = "Chevy";
  newTruck.model = "Silverado";
  cout << newTruck.getColorAsMessage() << "\n"; 
  cout << newTruck.make << "\n"; 

  return 0;
}

Output

The Car color is: Red
BMW
The Truck color is: White
Chevy

Conclusion

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



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.