0
0
C++programming~10 mins

Function overriding 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 base class function that can be overridden.

C++
class Base {
public:
    virtual void [1]() {
        std::cout << "Base function" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Arun
Bshow
Cprint
Ddisplay
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different function name that does not match the derived class function.
2fill in blank
medium

Complete the code to override the base class function in the derived class.

C++
class Derived : public Base {
public:
    void [1]() override {
        std::cout << "Derived function" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Aprint
Bdisplay
Cshow
Drun
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different function name or missing the override keyword.
3fill in blank
hard

Fix the error in the base class function declaration to allow overriding.

C++
class Base {
public:
    virtual void [1]() {
        std::cout << "Base function" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Arun
Bshow
Cdisplay
Dprint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Changing the function name so it does not match the derived class.
4fill in blank
hard

Fill both blanks to complete the derived class overriding the base class function.

C++
class Derived : public Base {
public:
    [1] [2]() override {
        std::cout << "Derived override" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Avoid
Bint
Cdisplay
Dshow
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different return type or function name.
5fill in blank
hard

Fill all three blanks to call the overridden function through a base class pointer.

C++
Base* ptr = new Derived();
ptr->[1]();
delete [2];
[3] = nullptr;
Drag options to blanks, or click blank then click option'
Aptr
Bdisplay
DbasePtr
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using wrong pointer names or function names.