0
0
C++programming~10 mins

Pure virtual functions in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A= 0
B= 1
C{}
D;
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.
2fill in blank
medium

Complete 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'
Aprint
Bshow
Cdisplay
Drun
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a function name different from the one asked.
Adding a function body instead of '= 0;'.
3fill in blank
hard

Fix 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'
Adisplay
Brun
Cprint
Dshow
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.
4fill in blank
hard

Fill 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'
Aint
Bvoid
Ccalculate
Ddisplay
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Swapping return type and function name.
Using wrong function names.
5fill in blank
hard

Fill 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'
Abool
Bprocess
Cint
Dvoid
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Mixing up return type and parameter type.
Using wrong function names.