← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 2 of 11

C++ Function Overriding

Function overriding is when a child class gives its own version of something the parent already does, like a child doing a chore in their own way. The child's version is used for the child.
Syntax
cpp
class Base {
public:
  virtual void method_name() { }
};

class Derived : public Base {
public:
  void method_name() {
    Base::method_name();  // optionally call the base version
  }
};

What is Function Overriding?

Function overriding happens when a derived class defines a method with the exact same name, return type, and parameter list as a method already defined in its base class — the derived version replaces the inherited one when called on a derived object.

Example: What is Function Overriding?

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Meow" << std::endl; }
};

// Program execution starts in main()
int main() {
	Cat c;
	c.speak();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Calling Base Class Overridden Method

Even after overriding a base method, you can still explicitly invoke the parent's original version from inside the derived class by qualifying the call with the base class name and the scope resolution operator, letting you extend rather than fully replace the inherited behavior.

Example: Calling Base Class Overridden Method

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override {
		Animal::speak();
		// Print to the console with cout
		std::cout << "Meow" << std::endl;
	}
};

// Program execution starts in main()
int main() {
	Cat c;
	c.speak();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Access Specifiers in Overriding

For overriding to work correctly, the base class's method must be visible to the derived class, which typically means it's declared public or protected — a private base method can't be overridden in any meaningful sense since the derived class can't even see it.

Example: Access Specifiers in Overriding

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Meow" << std::endl; }
};

// Program execution starts in main()
int main() {
	Cat c;
	c.speak();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overriding vs Overloading

Overloading and overriding are often confused but are different mechanisms: overloading keeps the same name but changes the parameter signature within the same class or scope, while overriding happens across a base/derived pair and requires an identical signature.

Example: Overriding vs Overloading

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Animal speaks" << std::endl; }
	void speak(int times) { for (int i = 0; i < times; i++) std::cout << "!"; std::cout << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Woof" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.speak();
	d.speak(3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overriding with Pointers

If you point a base class pointer at a derived object and call a method that was overridden but never declared virtual in the base class, the base class's version runs by default — this is 'static binding'.

Marking the base method virtual switches to dynamic binding, so the derived version runs instead.

Example: Overriding with Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	void speak() { std::cout << "Animal" << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() { std::cout << "Dog" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Animal *a = new Dog();
	a->speak();
	// Free the heap memory with delete
	delete a;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Changing the parameter list slightly, which creates a new function that hides the base one instead of overriding it.
  2. Forgetting virtual in the base class, so calls through a base pointer use the base version.
  3. Not using override, so a typo in the signature is silently accepted.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.