C++ Function Overriding
In this page:
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 <iostream>
class Animal {
public:
virtual void speak() { std::cout << "Some sound" << std::endl; }
};
class Cat : public Animal {
public:
void speak() override { std::cout << "Meow" << std::endl; }
};
int main() {
Cat c;
c.speak();
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 <iostream>
class Animal {
public:
virtual void speak() { std::cout << "Some sound" << std::endl; }
};
class Cat : public Animal {
public:
void speak() override {
Animal::speak();
std::cout << "Meow" << std::endl;
}
};
int main() {
Cat c;
c.speak();
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 <iostream>
class Animal {
public:
virtual void speak() { std::cout << "Some sound" << std::endl; }
};
class Cat : public Animal {
public:
void speak() override { std::cout << "Meow" << std::endl; }
};
int main() {
Cat c;
c.speak();
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 <iostream>
class Animal {
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; }
};
class Dog : public Animal {
public:
void speak() override { std::cout << "Woof" << std::endl; }
};
int main() {
Dog d;
d.speak();
d.speak(3);
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 <iostream>
class Animal {
public:
void speak() { std::cout << "Animal" << std::endl; }
};
class Dog : public Animal {
public:
void speak() { std::cout << "Dog" << std::endl; }
};
int main() {
Animal *a = new Dog();
a->speak();
delete a;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: