Form Structure Example
Single One base → one derived AnimalDog
Multiple One derived ← two or more bases FlyingRobotFlyableRobot
Multilevel Chain: base → derived → further derived AnimalMammalDog
Hierarchical One base → multiple independent derived classes ShapeCircle, Square, Triangle
Hybrid Combination of two or more forms above Diamond-shaped hierarchies — can trigger the "diamond problem"
// Diamond problem (hybrid inheritance)
class A { public: int x; };
class B : public A {};
class C : public A {};
class D : public B, public C {};   // D has TWO copies of A::x -- ambiguous!

// Fixed with virtual inheritance:
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};   // now only ONE shared copy of A::x

Each form, with a runnable example

Single inheritance

class Animal { public: void eat() { cout << "eats\n"; } };
class Dog : public Animal { public: void bark() { cout << "barks\n"; } };
// Dog gets eat() + adds bark()

Multiple inheritance

class Flyable { public: void fly() { cout << "flies\n"; } };
class Robot { public: void compute() { cout << "computes\n"; } };
class FlyingRobot : public Flyable, public Robot {};   // inherits from BOTH
// FlyingRobot fr; fr.fly(); fr.compute();  -- both work

Multilevel inheritance

class Animal { public: void eat() { cout << "eats\n"; } };
class Mammal : public Animal { public: void walk() { cout << "walks\n"; } };
class Dog : public Mammal { public: void bark() { cout << "barks\n"; } };
// Dog has ALL THREE: eat(), walk(), bark() -- inherited through the whole chain

Hierarchical inheritance

class Shape { public: virtual double area() = 0; };
class Circle : public Shape { /* ... */ };
class Square : public Shape { /* ... */ };
// Circle and Square are independent siblings, both derived from Shape

Hybrid inheritance (and the diamond problem)

class A { public: int x; };
class B : public A {};
class C : public A {};
class D : public B, public C {};   // PROBLEM: D has TWO copies of A::x -- which one does D::x refer to?

// Fix: virtual inheritance
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};   // NOW: only ONE shared copy of A::x

Why the diamond problem happens

D inherits from both B and C, and both B and C separately inherit from A. Without virtual, the compiler creates two independent sub-objects of A inside D — one via the B path, one via the C path — so d.x is genuinely ambiguous. virtual inheritance tells the compiler to share a single A sub-object no matter how many paths lead to it.

Important MCQs

  1. One derived class inheriting from two or more base classes is called:
  2. A chain like Animal → Mammal → Dog is an example of:
  3. One base class with several independent derived classes (e.g., Shape → Circle, Square) is:
  4. The "diamond problem" arises specifically in: