0
0
C++programming~5 mins

Method overriding in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Avirtual
Boverride
Cstatic
Dfinal
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
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
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
What is the benefit of method overriding?
APrevents inheritance
BIncreases program size
CAllows runtime polymorphism
DMakes methods static
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.