Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pure virtual function named display in the base class.
C++
class Base { public: virtual void display() [1]; };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Forgetting to add '= 0' makes the function not pure virtual.
Using '{}' instead of '= 0' defines a normal virtual function.
β Incorrect
A pure virtual function is declared by assigning 0 to the function declaration, like
virtual void display() = 0;.2fill in blank
mediumComplete the code to make Base an abstract class by adding a pure virtual function show.
C++
class Base { public: virtual void [1]() = 0; };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a function name different from the one asked.
Adding a function body instead of '= 0;'.
β Incorrect
The function name can be any valid identifier. Here,
show is used as the pure virtual function name.3fill in blank
hardFix the error in the derived class by correctly overriding the pure virtual function display.
C++
class Derived : public Base { public: void [1]() override { // implementation } };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a different function name than the base class pure virtual function.
Forgetting to override the pure virtual function causes the derived class to be abstract.
β Incorrect
The derived class must override the pure virtual function with the exact same name to provide implementation.
4fill in blank
hardFill both blanks to declare a pure virtual function calculate that returns an int.
C++
class Calculator { public: virtual [1] [2]() = 0; };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Swapping return type and function name.
Using wrong function names.
β Incorrect
The function return type is
int and the function name is calculate.5fill in blank
hardFill all three blanks to define a pure virtual function process that takes an int parameter and returns bool.
C++
class Processor { public: virtual [1] [2]([3] value) = 0; };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Mixing up return type and parameter type.
Using wrong function names.
β Incorrect
The function returns
bool, is named process, and takes an int parameter.