Method overriding occurs when a derived class provides its own version of a function that is already defined in its base class. The function in the derived class has the same name and signature as in the base class.
Click to reveal answer
beginner
Why do we use method overriding?
We use method overriding to change or extend the behavior of a base class method in a derived class. It allows polymorphism, so the program can decide at runtime which method version to call.
Click to reveal answer
beginner
What keyword is used in C++ to indicate a method can be overridden?
The keyword <code>virtual</code> is used in the base class method declaration to allow overriding in derived classes.
Click to reveal answer
intermediate
What happens if a base class method is not declared <code>virtual</code> but a derived class defines a method with the same name?
If the base class method is not virtual, the derived class method hides the base method but does not override it. Calls through base class pointers will use the base class method, not the derived one.
Click to reveal answer
intermediate
Explain the role of the override specifier in C++.
The <code>override</code> specifier is used in the derived class method declaration to indicate that the method is intended to override a base class virtual method. It helps the compiler catch errors if the method does not actually override anything.
Click to reveal answer
Which keyword must be used in the base class to allow method overriding?
Avirtual
Boverride
Cstatic
Dfinal
✗ Incorrect
The virtual keyword in the base class method declaration allows derived classes to override that method.
What does the override keyword do in a derived class method?
APrevents the method from being overridden
BDeclares a new unrelated method
CMakes the method static
DIndicates the method overrides a base class virtual method
✗ Incorrect
The override keyword tells the compiler that the method is meant to override a base class virtual method.
If a base class method is not virtual, what happens when a derived class defines a method with the same name?
AThe derived method hides the base method
BThe derived method overrides the base method
CCompilation error
DThe base method is deleted
✗ Incorrect
Without virtual, the derived method hides the base method but does not override it.
Which of the following is true about method overriding?
AMethod signatures must be different
BMethod signatures must be the same
COnly static methods can be overridden
DOverriding is only possible with private methods
✗ Incorrect
To override a method, the derived class method must have the same name and signature as the base class method.
What is the benefit of method overriding?
APrevents inheritance
BIncreases program size
CAllows runtime polymorphism
DMakes methods static
✗ Incorrect
Method overriding enables runtime polymorphism, allowing the program to choose the correct method version during execution.
Explain method overriding in C++ and why the virtual keyword is important.
Think about how derived classes change base class behavior.
You got /3 concepts.
Describe the difference between method overriding and method hiding in C++.
Consider what happens when base class methods are called through pointers.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of method overriding in C++?
easy
A. To create a new method with a different name in the child class
B. To allow a child class to provide a specific implementation of a method already defined in its parent class
C. To prevent a method from being used in the child class
D. To copy all methods from the parent class to the child class without changes
Solution
Step 1: Understand method overriding concept
Method overriding allows a child class to change how a method works that it inherits from its parent.
Step 2: Identify the correct purpose
The child class provides its own version of the method, customizing behavior while keeping the same method name.
Final Answer:
To allow a child class to provide a specific implementation of a method already defined in its parent class -> Option B
Hint: Overriding means child changes parent's method behavior [OK]
Common Mistakes:
Confusing overriding with method overloading
Thinking overriding creates new method names
Believing overriding copies methods without changes
2. Which of the following is the correct way to declare a method for overriding in C++?
easy
A. override void display();
B. void display() override;
C. void virtual display();
D. virtual void display();
Solution
Step 1: Recall syntax for virtual methods
In C++, the parent class method must be marked with virtual to allow overriding.
Step 2: Check each option
virtual void display(); correctly uses virtual void display();. void display() override; uses override incorrectly in declaration, it is used in child method definition. Options C and D have wrong syntax.
Final Answer:
virtual void display(); -> Option D
Quick Check:
Parent method uses virtual keyword [OK]
Hint: Parent method must be virtual for overriding [OK]
Common Mistakes:
Placing override keyword in parent method declaration
Using wrong order of keywords
Omitting virtual keyword in parent
3. What will be the output of the following C++ code?
class Base {
public:
virtual void show() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived" << std::endl; }
};
int main() {
Base* ptr = new Derived();
ptr->show();
delete ptr;
return 0;
}
medium
A. Derived
B. Runtime error
C. Compilation error
D. Base
Solution
Step 1: Understand virtual function behavior
The pointer is of type Base* but points to Derived object. Because show() is virtual, the Derived version is called.
Step 2: Trace the call ptr->show()
At runtime, the program calls Derived::show(), printing "Derived".
Final Answer:
Derived -> Option A
Quick Check:
Virtual method calls child's version [OK]
Hint: Virtual calls use actual object method, not pointer type [OK]
Common Mistakes:
Expecting Base output due to pointer type
Confusing override keyword effect
Ignoring virtual keyword
4. Identify the error in the following code snippet related to method overriding:
class Parent {
public:
void greet() { std::cout << "Hello from Parent" << std::endl; }
};
class Child : public Parent {
public:
void greet() override { std::cout << "Hello from Child" << std::endl; }
};
medium
A. Parent class method greet() should be marked virtual
B. Child class method greet() should be marked virtual
C. override keyword is not allowed in C++
D. No error, code is correct
Solution
Step 1: Check parent method declaration
For overriding to work properly, the parent method must be declared virtual. Here, it is not.
Step 2: Analyze override keyword usage
The child method uses override, which requires the parent method to be virtual. Since parent method is not virtual, this causes a compilation error.
Final Answer:
Parent class method greet() should be marked virtual -> Option A
Quick Check:
Parent method must be virtual for override [OK]
Hint: Parent method must be virtual if child uses override [OK]
Common Mistakes:
Thinking child method must be virtual
Ignoring need for virtual in parent
Believing override keyword is optional
5. Given the classes below, what will be the output when ptr->display(); is called?
class Animal {
public:
virtual void display() { std::cout << "Animal" << std::endl; }
};
class Dog : public Animal {
public:
void display() override { std::cout << "Dog" << std::endl; }
};
class Puppy : public Dog {
public:
void display() override { std::cout << "Puppy" << std::endl; }
};
int main() {
Animal* ptr = new Puppy();
ptr->display();
delete ptr;
return 0;
}
hard
A. Animal
B. Dog
C. Puppy
D. Compilation error due to multiple overrides
Solution
Step 1: Understand multi-level overriding
Each class overrides display() with its own version. The pointer is of type Animal* but points to a Puppy object.
Step 2: Determine which method is called
Because display() is virtual, the most derived override (Puppy::display) is called at runtime.
Final Answer:
Puppy -> Option C
Quick Check:
Virtual calls deepest override in inheritance chain [OK]
Hint: Virtual calls deepest override in inheritance chain [OK]
Common Mistakes:
Expecting Dog or Animal output due to pointer type
Thinking multiple overrides cause errors
Ignoring virtual keyword effect
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.