Recall & Review
beginner
What is method overriding in C++?
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?
✗ 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?✗ 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?
✗ Incorrect
Without
virtual, the derived method hides the base method but does not override it.Which of the following is true about method overriding?
✗ 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?
✗ 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.