What if you could change how things work without rewriting everything from scratch?
Why Function overriding in C++? - Purpose & Use Cases
Imagine you have a base class with a method, and you want a child class to do something a little different with that method. Without function overriding, you'd have to write separate methods with different names or copy-paste code, which gets messy fast.
Manually creating new methods for each variation means repeating code and confusing names. It's easy to make mistakes, hard to maintain, and your program becomes bulky and unclear.
Function overriding lets a child class provide its own version of a method already defined in the parent class. This keeps code clean, organized, and lets you change behavior easily without rewriting everything.
class Animal { void sound() { /* generic sound */ } } class Dog { void dogSound() { /* bark */ } }
class Animal { public: virtual void sound() { /* generic sound */ } }; class Dog : public Animal { public: void sound() override { /* bark */ } };
It enables flexible and clear code where child classes can customize behavior while sharing a common interface.
Think of a drawing app where a base Shape class has a draw() method, but each shape like Circle or Square draws itself differently by overriding draw().
Function overriding lets child classes change parent methods.
It avoids code duplication and confusion.
It makes programs easier to extend and maintain.
Practice
Solution
Step 1: Understand function overriding concept
Function overriding means a child class changes the behavior of a function defined in its parent class.Step 2: Identify correct description
A child class provides its own version of a parent class's virtual function. correctly describes overriding as providing a new version of a parent's virtual function in the child class.Final Answer:
A child class provides its own version of a parent class's virtual function. -> Option AQuick Check:
Function overriding = child changes parent's virtual function [OK]
- Confusing overriding with overloading
- Thinking overriding happens without virtual keyword
- Believing overriding applies to non-virtual functions
Solution
Step 1: Recall override syntax in child class
In C++11+, the child class function uses the same signature and adds the 'override' keyword after the function declaration.Step 2: Identify correct syntax
virtual void func() override; shows the correct syntax: 'virtual void func() override;'. The 'virtual' keyword can be used in the child class but is optional; however, it is valid and common to include it.Final Answer:
virtual void func() override; -> Option DQuick Check:
Child function uses virtual and override keywords correctly [OK]
- Adding 'virtual' keyword after function name
- Placing 'override' before function name
- Omitting 'override' keyword entirely
class Base {
public:
virtual void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived"; }
};
int main() {
Base* b = new Derived();
b->show();
delete b;
return 0;
}Solution
Step 1: Understand virtual function behavior
The pointer 'b' is of type Base* but points to a Derived object. Because 'show' is virtual, the Derived version is called.Step 2: Trace the function call
Calling b->show() invokes Derived::show(), printing "Derived".Final Answer:
Derived -> Option AQuick Check:
Virtual function calls child's version [OK]
- Expecting base class output
- Forgetting to make base function virtual
- Confusing pointer type with object type
class Parent {
public:
void display() { std::cout << "Parent"; }
};
class Child : public Parent {
public:
void display() override { std::cout << "Child"; }
};Solution
Step 1: Check virtual keyword in parent
For overriding to work, the base class function must be declared virtual.Step 2: Analyze override keyword usage
Child uses 'override' but Parent's display() is not virtual, causing a compilation error.Final Answer:
Parent's display() must be declared virtual to override. -> Option BQuick Check:
Base function must be virtual for override [OK]
- Ignoring virtual keyword in base class
- Thinking override works without virtual
- Assuming override keyword is optional
obj->print();? class A {
public:
virtual void print() { std::cout << "A"; }
};
class B : public A {
public:
void print() override { std::cout << "B"; }
};
class C : public B {
public:
void print() override { std::cout << "C"; }
};
int main() {
A* obj = new C();
obj->print();
delete obj;
return 0;
}Solution
Step 1: Understand pointer and object types
Pointer 'obj' is of type A*, but it points to an object of class C.Step 2: Follow virtual function overriding chain
Since print() is virtual and overridden in B and C, the most derived override (C::print) is called.Final Answer:
C -> Option CQuick Check:
Virtual call resolves to most derived override [OK]
- Expecting base class output
- Confusing pointer type with object type
- Ignoring virtual function behavior
Ready to prove it?
Pass the test and this pattern moves unknown
