C++ Function Overriding
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Changing the parameter list slightly, which creates a new function that hides the base one instead of overriding it.
- Forgetting
virtualin the base class, so calls through a base pointer use the base version. - Not using
override, so a typo in the signature is silently accepted.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: