0
0
C++programming~10 mins

Interface-like behavior 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 in a C++ class.

C++
class Shape {
public:
    virtual void draw() [1];
};
Drag options to blanks, or click blank then click option'
A= 0
B{}
C;
D()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Forgetting the = 0 and just ending with a semicolon.
Adding an empty function body instead of making it pure virtual.
2fill in blank
medium

Complete the code to override the pure virtual function in the derived class.

C++
class Circle : public Shape {
public:
    void draw() [1] {
        // drawing code
    }
};
Drag options to blanks, or click blank then click option'
A() override
B()
C(int)
D(void)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Changing the function parameters.
Forgetting to add override keyword.
3fill in blank
hard

Fix the error in the code to prevent instantiation of the abstract class.

C++
Shape s[1];
Drag options to blanks, or click blank then click option'
A[]
B;
C{}
D()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to create an object of an abstract class.
Not using pointers or references for abstract types.
4fill in blank
hard

Fill both blanks to declare an interface-like class with a pure virtual destructor.

C++
class Interface {
public:
    virtual ~Interface() [1];
    virtual void execute() [2];
};
Drag options to blanks, or click blank then click option'
A= 0
B{}
C;
D()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Not declaring the destructor as pure virtual.
Forgetting to provide a destructor body if pure virtual.
5fill in blank
hard

Fill all three blanks to implement a derived class overriding interface methods correctly.

C++
class Impl : public Interface {
public:
    ~Impl() [1] override [2] {
        // destructor code
    }
    void execute() [3] override {
        // execution code
    }
};
Drag options to blanks, or click blank then click option'
A()
B{}
C;
D(void)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting function bodies.
Using semicolons instead of braces for function definitions.