Back to C++
2026-02-025 min read

C++ Function Overriding

Learn C++ Function Overriding step by step with clear examples and exercises.

Why This Matters

Welcome to this in-depth tutorial on C++ Function Overriding! We'll explore why it matters, its prerequisites, the core concept, a worked example, common mistakes, practice questions, and FAQs. By the end of this lesson, you'll have a solid understanding of function overriding in C++ and how to use it effectively.

Why This Matters

Function Overriding is an essential part of Object-Oriented Programming (OOP) in C++. It allows us to create subclasses that inherit properties from a parent class while providing their own implementation for specific methods. This feature enables polymorphism, which makes our code more flexible and easier to manage. In real-world scenarios, function overriding is crucial for creating modular code that can be reused in various contexts, such as in object-oriented design patterns or when writing libraries.

Prerequisites

Before diving into the core concept of function overriding, it's essential to have a good understanding of the following topics:

  1. C++ Basics (variables, data types, operators, control statements)
  2. Object-Oriented Programming (classes, objects, inheritance)
  3. Virtual Functions and Abstract Classes

Core Concept

Function Overriding occurs when a subclass provides its own implementation for a method that is already declared in the base class. The subclass's implementation overrides the one defined in the base class. To achieve this, we must adhere to specific rules:

  1. The method signature (name, return type, and parameters) in the subclass should be identical to the one in the base class.
  2. The access specifier for the method in the subclass can be more restrictive than the one in the base class but cannot be less restrictive.
  3. If a base class method is declared as virtual, the overriding method in the subclass should also be declared as virtual.

Here's an example of function overriding:

// Base class
class Shape {
public:
virtual void draw() {
cout << "Drawing a generic shape" << endl;
}
};

// Subclass Rectangle
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle" << endl;
}
};

In this example, the draw() method in the Shape class is declared as virtual. The Rectangle class inherits from Shape and provides its own implementation for the draw() method, which overrides the one defined in the base class.

Worked Example

Let's explore a more complex example that demonstrates function overriding with multiple inheritance and polymorphism:

// Base class with a virtual method
class Animal {
public:
virtual void makeSound() {
cout << "Animal makes a sound" << endl;
}
};

// Derived class Dog
class Dog : public Animal {
public:
void makeSound() {
cout << "Dog barks" << endl;
}
};

// Another base class with a non-virtual method
class Mammal {
public:
void breathe() {
cout << "Mammal breathes" << endl;
}
};

// Derived class GoldenRetriever, which inherits from both Animal and Mammal
class GoldenRetriever : public Animal, public Mammal {
public:
// Overriding the makeSound() method from Animal
void makeSound() {
cout << "Golden Retriever barks in a friendly manner" << endl;
}
};

int main() {
GoldenRetriever gr;
gr.makeSound(); // Output: "Golden Retriever barks in a friendly manner"
gr.breathe(); // Output: "Mammal breathes"
return 0;
}

In this example, the GoldenRetriever class inherits from both Animal and Mammal. It overrides the makeSound() method from the Animal base class while also inheriting the breathe() method from the Mammal base class.

Common Mistakes

  1. Forgetting to declare a method as virtual in the base class, making it impossible for subclasses to override it.
  2. Changing the return type or parameter list of a method in the subclass, which results in a new method instead of an override.
  3. Using a private access specifier for a method in the subclass that was public or protected in the base class, making it impossible to override the method.
  4. Failing to call the overridden method from the base class in the subclass constructor, which can lead to unexpected behavior.
  5. Incorrectly using override and final keywords, leading to compile errors or unintended consequences.

Practice Questions

  1. Write a C++ program that demonstrates function overriding with multiple inheritance and polymorphism similar to the example provided above but for a different set of animals (e.g., Cat, Lion).
  2. Given the following classes, implement a display() method in the Square class that overrides the one defined in the Shape class:
class Shape {
public:
virtual void display() {
cout << "Displaying a generic shape" << endl;
}
};

class Square : public Shape {
public:
// Implement the display() method for Square here
};

FAQ

Q: Why do we need to use virtual in the base class when declaring a method that will be overridden?

A: Using virtual ensures that the correct implementation of the method is called during runtime, even if the object's type is not known at compile-time.

Q: Can we override a static method in C++?

A: No, static methods cannot be overridden because they are bound to their class at compile-time and not to individual objects.

Q: What is the purpose of the override keyword in C++11?

A: The override keyword ensures that a method is correctly overriding an existing method from a base class, preventing accidental method hiding or incorrect method signatures.

Q: How can we call the overridden method from the base class in the subclass constructor?

A: We can use the base keyword to call the constructor of the base class and its methods, including overridden ones. For example:

class Base {
public:
Base() {
doSomething();
}
virtual void doSomething() {
cout << "Base does something" << endl;
}
};

class Derived : public Base {
public:
Derived() : Base() {
doSomething(); // Calls the overridden method from the base class
}
void doSomething() override {
cout << "Derived does something" << endl;
}
};
C++ Function Overriding | C++ | XQA Learn