0
0
C++programming~10 mins

Why abstraction is required 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 an abstract class with a pure virtual function.

C++
class Shape {
public:
    virtual void draw() = [1];
};
Drag options to blanks, or click blank then click option'
A0
B1
Ctrue
Dvoid
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 1 instead of 0 for pure virtual function
Omitting the virtual keyword
Trying to define the function body here
2fill in blank
medium

Complete the code to create a pointer to the abstract class and assign it to a derived class object.

C++
Shape* shape = new [1]();
Drag options to blanks, or click blank then click option'
AShape
BObject
CRectangle
DCircle
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to instantiate the abstract class directly
Using a class that does not implement all pure virtual functions
3fill in blank
hard

Fix the error in the code by completing the missing keyword to enforce abstraction.

C++
[1] Shape {
public:
    virtual void display() = 0;
};
Drag options to blanks, or click blank then click option'
Aclass
BAbstract
CInterface
DConcrete
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'abstract' or 'interface' keywords which do not exist in C++
Omitting the 'class' keyword
4fill in blank
hard

Fill both blanks to define a derived class that implements the pure virtual function.

C++
class Circle : public Shape {
public:
    void [1]() override {
        std::cout << "Drawing Circle" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Apaint
Bdisplay
Cdraw
Dshow
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different function name than the base class
Omitting the override keyword
5fill in blank
hard

Fill all three blanks to create an abstract class and a derived class that implements its pure virtual function.

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

class [3] : public [1] {
public:
    void [2]() override {
        std::cout << "Implemented" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
AShape
Bdraw
CCircle
DObject
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Mismatching class or function names
Not implementing the pure virtual function in the derived class