0
0
C++programming~10 mins

Why polymorphism is needed in C++ - Test Your Understanding

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

Complete the code to declare a base class pointer.

C++
Base* [1];
Drag options to blanks, or click blank then click option'
Aobject
Bptr
Cbase
Dpointer
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a variable name that is a reserved keyword.
Forgetting the asterisk (*) for pointer declaration.
2fill in blank
medium

Complete the code to assign a derived class object to a base class pointer.

C++
Derived d;
Base* ptr = [1];
Drag options to blanks, or click blank then click option'
A*d
Bptr
Cd
D&d
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Assigning the object directly without using &.
Using the pointer variable name on the right side.
3fill in blank
hard

Fix the error in the virtual function declaration to enable polymorphism.

C++
class Base {
public:
    virtual void show() [1]
};
Drag options to blanks, or click blank then click option'
A()
B;
C{}
Dvoid
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Ending the declaration with a semicolon only, without providing a function body.
Using parentheses instead of braces.
4fill in blank
hard

Fill both blanks to call the correct overridden function using polymorphism.

C++
Base* ptr = new Derived();
ptr->[1]();
delete [2];
Drag options to blanks, or click blank then click option'
Ashow
Bptr
Cdelete
DDerived
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Calling a non-existent function name.
Deleting the class name instead of the pointer.
5fill in blank
hard

Fill all three blanks to implement polymorphism with virtual functions and base pointer.

C++
class Base {
public:
    virtual void [1]() [2]
};

class Derived : public Base {
public:
    void [3]() override {
        std::cout << "Derived show" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Ashow
B{}
C;
Ddisplay
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different function names in base and derived classes.
Ending the base class function declaration with a semicolon instead of braces.