0
0
C++programming~20 mins

Interface-like behavior in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of virtual function call through base pointer
What is the output of this C++ code?
C++
#include <iostream>

class IShape {
public:
    virtual void draw() const = 0;
    virtual ~IShape() = default;
};

class Circle : public IShape {
public:
    void draw() const override {
        std::cout << "Circle" << std::endl;
    }
};

int main() {
    IShape* shape = new Circle();
    shape->draw();
    delete shape;
    return 0;
}
ACircle
BIShape
CCompilation error due to abstract class instantiation
DRuntime error due to pure virtual function call
Attempts:
2 left
πŸ’‘ Hint
Think about how virtual functions work when called through a base class pointer.
❓ Predict Output
intermediate
2:00remaining
Output of multiple inheritance with interface classes
What will this program print?
C++
#include <iostream>

class IReadable {
public:
    virtual void read() const = 0;
    virtual ~IReadable() = default;
};

class IWritable {
public:
    virtual void write() const = 0;
    virtual ~IWritable() = default;
};

class File : public IReadable, public IWritable {
public:
    void read() const override {
        std::cout << "Reading file" << std::endl;
    }
    void write() const override {
        std::cout << "Writing file" << std::endl;
    }
};

int main() {
    File f;
    IReadable* r = &f;
    IWritable* w = &f;
    r->read();
    w->write();
    return 0;
}
ACompilation error due to ambiguous base classes
BReading file\nWriting file
CWriting file\nReading file
DRuntime error due to multiple inheritance
Attempts:
2 left
πŸ’‘ Hint
Each interface has a pure virtual function. The class File implements both.
❓ Predict Output
advanced
2:00remaining
Output of interface pointer to derived object with overridden method
What is the output of this code?
C++
#include <iostream>

class IAnimal {
public:
    virtual void speak() const = 0;
    virtual ~IAnimal() = default;
};

class Dog : public IAnimal {
public:
    void speak() const override {
        std::cout << "Woof" << std::endl;
    }
};

class Cat : public IAnimal {
public:
    void speak() const override {
        std::cout << "Meow" << std::endl;
    }
};

void makeSpeak(const IAnimal* animal) {
    animal->speak();
}

int main() {
    Dog dog;
    Cat cat;
    makeSpeak(&dog);
    makeSpeak(&cat);
    return 0;
}
ARuntime error due to calling pure virtual function
BMeow\nWoof
CCompilation error due to missing virtual destructor
DWoof\nMeow
Attempts:
2 left
πŸ’‘ Hint
The function makeSpeak calls speak() on the interface pointer.
❓ Predict Output
advanced
2:00remaining
Output of interface with default method and override
What will this program print?
C++
#include <iostream>

class IPrinter {
public:
    virtual void print() const {
        std::cout << "Default print" << std::endl;
    }
    virtual ~IPrinter() = default;
};

class CustomPrinter : public IPrinter {
public:
    void print() const override {
        std::cout << "Custom print" << std::endl;
    }
};

int main() {
    IPrinter* p1 = new IPrinter();
    IPrinter* p2 = new CustomPrinter();
    p1->print();
    p2->print();
    delete p1;
    delete p2;
    return 0;
}
ADefault print\nCustom print
BCustom print\nDefault print
CCompilation error: cannot instantiate abstract class IPrinter
DRuntime error due to calling virtual function on deleted object
Attempts:
2 left
πŸ’‘ Hint
IPrinter has a default implementation of print(), so it can be instantiated.
❓ Predict Output
expert
3:00remaining
Output of diamond inheritance with virtual inheritance
What is the output of this program?
C++
#include <iostream>

class IDevice {
public:
    virtual void info() const = 0;
    virtual ~IDevice() = default;
};

class Scanner : virtual public IDevice {
public:
    void info() const override {
        std::cout << "Scanner" << std::endl;
    }
};

class Printer : virtual public IDevice {
public:
    void info() const override {
        std::cout << "Printer" << std::endl;
    }
};

class AllInOne : public Scanner, public Printer {
public:
    void info() const override {
        std::cout << "AllInOne" << std::endl;
    }
};

int main() {
    AllInOne aio;
    IDevice* device = &aio;
    device->info();
    return 0;
}
APrinter
BScanner
CAllInOne
DCompilation error due to ambiguous inheritance
Attempts:
2 left
πŸ’‘ Hint
Virtual inheritance avoids duplicate IDevice base. The AllInOne class overrides info().