0
0
C++programming~5 mins

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

Choose your learning style9 modes available
Recall & Review
beginner
What is function overriding in C++?
Function 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 parameters as the one in the base class.
Click to reveal answer
beginner
Why do we use function overriding?
We use function overriding to change or extend the behavior of a base class function in a derived class. It allows polymorphism, so the program can decide at runtime which function to call based on the object type.
Click to reveal answer
intermediate
What keyword is used in C++11 and later to explicitly indicate a function is overriding a base class function?
The keyword <code>override</code> is used after the function declaration in the derived class to indicate that the function is meant to override a base class function. This helps catch errors if the base function signature changes.
Click to reveal answer
intermediate
What happens if the base class function is not declared as <code>virtual</code> but the derived class overrides it?
If the base class function is not <code>virtual</code>, then the function call is resolved at compile time (static binding). This means the base class version will be called even if the object is of the derived class type, which is usually not what we want.
Click to reveal answer
beginner
Example: What will be the output of this code?<br><pre>#include &lt;iostream&gt;
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; }</pre>
The output will be <code>Derived</code>. Because <code>show()</code> is virtual in the base class and overridden in the derived class, the call <code>b->show()</code> uses the derived class version at runtime.
Click to reveal answer
What must be true for function overriding to work correctly in C++?
AThe base class function must be declared virtual.
BThe derived class function must have a different name.
CThe base class function must be private.
DThe derived class function must be static.
What does the override keyword do in C++?
AIt makes the function static.
BIt makes the function inline.
CIt prevents the function from being overridden.
DIt tells the compiler the function is intended to override a base class function.
If a base class function is not virtual, what kind of binding is used when calling it through a base class pointer?
ANo binding
BDynamic binding (runtime)
CStatic binding (compile-time)
DLate binding
Which of these is a correct signature for overriding a base class function void display() const;?
Avoid display() override;
Bvoid display() const override;
Cint display() const override;
Dvoid display(int) const override;
What will happen if you use override keyword but the base class function does not exist?
ACompiler error
BProgram runs normally
CWarning only
DUndefined behavior
Explain function overriding and why it is important in C++.
Think about how derived classes change base class behavior.
You got /4 concepts.
    Describe the difference between function overriding and function overloading.
    Focus on how the functions differ in name, parameters, and class relationships.
    You got /4 concepts.