Last Updated: January 3, 2026
30 quizzes
In public inheritance, how are public and protected members of the base class treated in the derived class interface?
Which scenario is the most appropriate use of single inheritance?
Given class Base and class Derived : private Base, how can client code access Base's public members through Derived?
In C++, when constructing a derived object, in which order are constructors called?
What is the main reason to use virtual inheritance?
In multiple inheritance, how do you resolve ambiguity when two base classes define a method with the same name?
Which access specifier for inheritance best models 'is-a and can be used wherever base is expected'?
Which statement about constructors in inheritance is correct?
In a diamond hierarchy without virtual inheritance, class D : public B, public C, both derived from A, what does a D object contain?
When should you prefer composition over inheritance in C++?
Call a base class constructor with a parameter from a derived class constructor
class Derived : public Base {public: Derived(int x) : (x) {}};Click an option to fill the blank:
Declare a derived class using protected inheritance
class Derived : Base { };Click an option to fill the blank:
Use virtual inheritance to avoid duplicate A subobjects
class B : public A {};Click an option to fill the blank:
What is the output?
1#include <iostream>
2
3class Animal {
4public:
5 void speak() const { std::cout << "Animal"; }
6};
7
8class Dog : public Animal {
9public:
10 void speak() const { std::cout << "Dog"; }
11};
12
13int main() {
14 Dog d;
15 Animal &ref = d;
16 ref.speak();
17}
18What is the output regarding constructor calls?
1#include <iostream>
2
3class Base {
4public:
5 Base() { std::cout << "B"; }
6};
7
8class Derived : public Base {
9public:
10 Derived() { std::cout << "D"; }
11};
12
13int main() {
14 Derived d;
15}
16What does this program print?
1#include <iostream>
2
3class Base {
4public:
5 virtual void f() { std::cout << "B"; }
6 virtual ~Base() = default;
7};
8
9class Derived : public Base {
10public:
11 void f() override { std::cout << "D"; }
12};
13
14void call(Base b) {
15 b.f();
16}
17
18int main() {
19 Derived d;
20 call(d);
21}
22What is the output with multiple inheritance?
1#include <iostream>
2
3class Logger {
4public:
5 void log() const { std::cout << "L"; }
6};
7
8class Notifier {
9public:
10 void notify() const { std::cout << "N"; }
11};
12
13class Service : public Logger, public Notifier {
14};
15
16int main() {
17 Service s;
18 s.log();
19 s.notify();
20}
21What is the output demonstrating virtual inheritance construction order?
1#include <iostream>
2
3class A {
4public:
5 A() { std::cout << "A"; }
6};
7
8class B : virtual public A {
9public:
10 B() { std::cout << "B"; }
11};
12
13class C : virtual public A {
14public:
15 C() { std::cout << "C"; }
16};
17
18class D : public B, public C {
19public:
20 D() { std::cout << "D"; }
21};
22
23int main() {
24 D d;
25}
26Find the bug related to object lifetime in this inheritance example
Click on the line(s) that contain the bug.
#include <iostream> class Base {public: Base() { std::cout << "Base"; } ~Base() { std::cout << "~Base"; }}; class Derived : public Base {public: ~Derived() { std::cout << "~Derived"; }}; void destroy(Base* p) { delete p;} int main() { Derived* d = new Derived(); destroy(d);} Find the bug related to constructor initialization in multiple inheritance
Click on the line(s) that contain the bug.
#include <string> class Named {public: Named(const std::string& n) : name(n) {}private: std::string name;}; class Sized {public: Sized(int s) : size(s) {}private: int size;}; class Widget : public Named, public Sized {public: Widget() { // attempt to default-construct bases }}; Click the line where ambiguity arises due to multiple inheritance
Click on the line to select.
#include <iostream> class A {public: void print() const { std::cout << "A"; }}; class B : public A {};class C : public A {}; class D : public B, public C {}; int main() { D d; d.print();} Click the line that uses virtual inheritance to solve the diamond problem
Click on the line to select.
class A {public: int value;}; class B : virtual public A {};class C : virtual public A {}; class D : public B, public C {public: void set(int v) { value = v; }}; Complete the class definition to use public inheritance and override a virtual function
#include <iostream>class Base {public: virtual void run() const { std::cout << "Base"; }};class Derived : Base {public: void run() const { std::cout << "Derived"; }};Click an option to fill blank 1:
Use a constructor initializer list to call the base class constructor
#include <string>class User {public: User(const std::string& name) : name_(name) {}private: std::string name_;};class Admin : public User {public: Admin(const std::string& name, int level) : (name), level_() {}private: int level_;};Click an option to fill blank 1:
Complete the declarations to model virtual inheritance in a diamond hierarchy
class Base {public: int id;};class Left : public Base {};class Right : public Base {};class Final : public Left, public Right {public: void set(int v) { id = v; }};Click an option to fill blank 1:
Control access to base members via private inheritance and a public wrapper
class Logger {public: void log(const char* msg) const;};class Service : Logger {public: void info(const char* msg) const { (msg); }};Click an option to fill blank 1:
Match the inheritance-related terms with their descriptions
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the access specifiers in inheritance with resulting visibility of base public members in the derived class
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the steps when constructing an object with virtual inheritance: class D : public B, public C; class B : virtual public A; class C : virtual public A;
Drag and drop to reorder, or use the arrows.
Order these actions when designing an inheritance hierarchy for a new feature
Drag and drop to reorder, or use the arrows.