0
0
C++programming~20 mins

Abstract classes in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Abstract Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of abstract class pointer calling derived method

What is the output of this C++ code?

C++
#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0;
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle" << endl;
    }
};

int main() {
    Shape* shape = new Circle();
    shape->draw();
    delete shape;
    return 0;
}
ARuntime error: pure virtual function call
BShape::draw called
CCompilation error: cannot instantiate abstract class
DDrawing Circle
Attempts:
2 left
πŸ’‘ Hint

Remember that calling a pure virtual function through a derived class pointer calls the derived override.

❓ Predict Output
intermediate
2:00remaining
Value of member after calling pure virtual function in constructor

What is the output of this C++ program?

C++
#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        print();
    }
    virtual void print() = 0;
};

class Derived : public Base {
public:
    int x = 5;
    void print() override {
        cout << x << endl;
    }
};

int main() {
    Derived d;
    return 0;
}
A5
BUndefined behavior or runtime error
CCompilation error: pure virtual function call in constructor
D0
Attempts:
2 left
πŸ’‘ Hint

Think about which class's constructor is running and what members are initialized at that time.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in abstract class usage

What error does this code produce?

C++
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() = 0;
};

class Dog : public Animal {
public:
    void sound() {
        cout << "Woof" << endl;
    }
};

int main() {
    Animal a;
    a.sound();
    return 0;
}
ACompilation error: cannot instantiate abstract class 'Animal'
BRuntime error: pure virtual function call
COutput: Woof
DCompilation error: missing override specifier
Attempts:
2 left
πŸ’‘ Hint

Can you create an object of an abstract class?

πŸ“ Syntax
advanced
2:00remaining
Which option correctly declares an abstract class?

Which of the following code snippets correctly declares an abstract class in C++?

Aclass A { virtual void f() {}; };
Bclass A { void f() = 0; };
Cclass A { virtual void f() = 0; };
Dclass A { virtual void f() = 1; };
Attempts:
2 left
πŸ’‘ Hint

Remember the syntax for pure virtual functions.

πŸš€ Application
expert
3:00remaining
Number of objects created and output

Consider the following code. How many objects are created and what is the output?

C++
#include <iostream>
using namespace std;

class Abstract {
public:
    Abstract() { cout << "Abstract created\n"; }
    virtual void action() = 0;
};

class Concrete : public Abstract {
public:
    Concrete() { cout << "Concrete created\n"; }
    void action() override { cout << "Action done\n"; }
};

int main() {
    Concrete c;
    Abstract& ref = c;
    ref.action();
    return 0;
}
A
1 object created; output:
Abstract created
Concrete created
Action done
BCompilation error: cannot instantiate Abstract
C
1 object created; output:
Concrete created
Action done
D
2 objects created; output:
Abstract created
Concrete created
Action done
Attempts:
2 left
πŸ’‘ Hint

Think about how many constructors run when creating a derived object.