C++ OOP: Inheritance Quiz
40 in-depth questions covering C++ inheritance fundamentals including base and derived classes, protected members, method overriding, final and override keywords, and inheritance best practices — with 16 code examples to master object-oriented inheritance patterns.
Question 1
What is inheritance in C++?
Question 2
What is the syntax for inheritance in C++?
class Base {
public:
void method() {}
};
class Derived : public Base {
// Inherits method() from Base
};
int main() {
Derived d;
d.method(); // Can call inherited method
return 0;
}Question 3
What is the difference between public, protected, and private inheritance?
class Base {
public:
int pub;
protected:
int prot;
private:
int priv;
};
class PubDerived : public Base {
// pub is public, prot is protected, priv is inaccessible
};
class ProtDerived : protected Base {
// pub is protected, prot is protected, priv is inaccessible
};
class PrivDerived : private Base {
// pub is private, prot is private, priv is inaccessible
};Question 4
What are protected members in inheritance?
class Base {
protected:
int protectedData;
void protectedMethod() {}
public:
void publicMethod() {
protectedData = 42; // OK
protectedMethod(); // OK
}
};
class Derived : public Base {
public:
void derivedMethod() {
protectedData = 24; // OK - accessible in derived class
protectedMethod(); // OK - accessible in derived class
}
};
int main() {
Derived d;
// d.protectedData = 10; // Error - not accessible outside hierarchy
d.derivedMethod(); // OK
return 0;
}Question 5
What is method overriding in inheritance?
class Base {
public:
virtual void method() {
std::cout << "Base method" << std::endl;
}
};
class Derived : public Base {
public:
void method() override { // Override keyword is optional but recommended
std::cout << "Derived method" << std::endl;
}
};
int main() {
Base* b = new Derived();
b->method(); // Calls Derived::method() due to virtual dispatch
delete b;
return 0;
}Question 6
What is the difference between overriding and hiding in inheritance?
class Base {
public:
void method(int x) {
std::cout << "Base: " << x << std::endl;
}
virtual void virtMethod() {
std::cout << "Base virtual" << std::endl;
}
};
class Derived : public Base {
public:
void method() { // Hides Base::method(int)
std::cout << "Derived" << std::endl;
}
void virtMethod() override { // Overrides Base::virtMethod()
std::cout << "Derived virtual" << std::endl;
}
};
int main() {
Derived d;
d.method(); // Calls Derived::method()
// d.method(5); // Error - Base::method(int) is hidden
d.virtMethod(); // Calls Derived::virtMethod()
return 0;
}Question 7
What is the override keyword in C++?
class Base {
public:
virtual void method() {}
virtual void method(int x) {}
};
class Derived : public Base {
public:
void method() override { // Correct override
// Implementation
}
// void method(int x) override { // Error: no matching base method
// }
void method(float f) override { // Error: signature mismatch
}
};Question 8
What is the final keyword in inheritance?
class Base final { // Cannot be inherited from
public:
virtual void method() {}
};
// class Derived : public Base {}; // Error: cannot inherit from final class
class NonFinal {
public:
virtual void method() {}
virtual void finalMethod() final {} // Cannot be overridden
};
class Derived : public NonFinal {
public:
void method() override {} // OK
// void finalMethod() override {} // Error: cannot override final method
};Question 9
What is the order of constructor and destructor calls in inheritance?
class Base {
public:
Base() { std::cout << "Base constructor" << std::endl; }
~Base() { std::cout << "Base destructor" << std::endl; }
};
class Derived : public Base {
public:
Derived() { std::cout << "Derived constructor" << std::endl; }
~Derived() { std::cout << "Derived destructor" << std::endl; }
};
int main() {
Derived d; // Output: Base constructor, Derived constructor
return 0; // Output: Derived destructor, Base destructor
}Question 10
What is the base class initializer syntax in derived constructors?
class Base {
protected:
int value;
public:
Base(int v) : value(v) {}
};
class Derived : public Base {
private:
std::string name;
public:
Derived(int v, const std::string& n)
: Base(v), name(n) { // Initialize base class first
}
};
int main() {
Derived d(42, "test");
return 0;
}Question 11
What is a virtual destructor and why is it important?
class Base {
public:
Base() { std::cout << "Base created" << std::endl; }
virtual ~Base() { std::cout << "Base destroyed" << std::endl; } // Virtual!
virtual void method() = 0;
};
class Derived : public Base {
public:
Derived() { std::cout << "Derived created" << std::endl; }
~Derived() { std::cout << "Derived destroyed" << std::endl; }
void method() override {}
};
int main() {
Base* b = new Derived(); // Polymorphic creation
delete b; // Correctly calls ~Derived() then ~Base()
return 0;
}Question 12
What is the difference between composition and inheritance?
// Inheritance (is-a relationship)
class Engine {
public:
void start() {}
};
class Car : public Engine { // Car is-an Engine? No!
};
// Composition (has-a relationship) - Better design
class Engine {
public:
void start() {}
};
class Car {
private:
Engine engine; // Car has-an Engine
public:
void start() { engine.start(); }
};Question 13
What is the diamond problem in multiple inheritance?
class A {
public:
void method() { std::cout << "A" << std::endl; }
};
class B : public A {};
class C : public A {};
class D : public B, public C { // Diamond inheritance
// D inherits from two copies of A!
// Which method() should D use?
};
int main() {
D d;
// d.method(); // Ambiguous - which A::method()?
return 0;
}Question 14
What is virtual inheritance and how does it solve the diamond problem?
class A {
public:
void method() { std::cout << "A" << std::endl; }
};
class B : virtual public A {}; // Virtual inheritance
class C : virtual public A {}; // Virtual inheritance
class D : public B, public C { // Diamond with virtual inheritance
public:
void callMethod() { method(); } // No ambiguity - only one A
};
int main() {
D d;
d.callMethod(); // OK - calls A::method()
return 0;
}Question 15
What is the difference between using-declaration and inheritance access specifiers?
class Base {
private:
void privateMethod() {}
protected:
void protectedMethod() {}
public:
void publicMethod() {}
};
class Derived : private Base { // Private inheritance
public:
using Base::protectedMethod; // Make protectedMethod public
using Base::publicMethod; // Make publicMethod public
// Cannot use privateMethod - still private
};
int main() {
Derived d;
d.publicMethod(); // OK - made public via using
d.protectedMethod(); // OK - made public via using
return 0;
}Question 16
What is the Liskov Substitution Principle in inheritance?
Question 17
What is the fragile base class problem?
Question 18
What is the difference between interface inheritance and implementation inheritance?
// Interface inheritance (pure virtual)
class IShape {
public:
virtual double area() const = 0; // Interface only
virtual ~IShape() = default;
};
// Implementation inheritance
class Shape {
public:
virtual double area() const { return 0.0; } // Default implementation
void draw() const { /* common drawing code */ }
};
class Circle : public IShape { // Interface inheritance
public:
double area() const override { return 3.14 * r * r; }
private:
double r;
};
class ColoredShape : public Shape { // Implementation inheritance
public:
double area() const override { /* custom area */ }
};Question 19
What is the difference between covariant return types and regular overriding?
class Base {
public:
virtual Base* clone() const { return new Base(*this); }
};
class Derived : public Base {
public:
// Covariant return type - more specific than Base*
Derived* clone() const override { return new Derived(*this); }
};
int main() {
Derived d;
Derived* d2 = d.clone(); // Can use Derived* directly
Base* b = d.clone(); // Still works
return 0;
}Question 20
What is the difference between inheritance and templates for code reuse?
Question 21
What is the difference between public inheritance and private inheritance?
Question 22
What is the difference between virtual and non-virtual inheritance?
Question 23
What is the difference between inheritance and delegation?
// Inheritance
class Stack : public std::vector<int> { // Stack is-a vector
public:
void push(int x) { push_back(x); }
int pop() { int x = back(); pop_back(); return x; }
};
// Delegation (composition)
class Stack {
private:
std::vector<int> data; // Stack has-a vector
public:
void push(int x) { data.push_back(x); }
int pop() { int x = data.back(); data.pop_back(); return x; }
};Question 24
What is the difference between method hiding and method overriding?
Question 25
What is the difference between abstract classes and concrete classes in inheritance?
// Abstract class (cannot be instantiated)
class Shape {
public:
virtual double area() const = 0; // Pure virtual
virtual ~Shape() = default;
void draw() const { /* common implementation */ }
};
// Concrete class (can be instantiated)
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override { return 3.14159 * radius * radius; }
};
int main() {
// Shape s; // Error: abstract class
Circle c(5.0); // OK: concrete class
return 0;
}Question 26
What is the difference between inheritance depth and inheritance breadth?
Question 27
What is the difference between virtual method calls and non-virtual method calls in inheritance?
Question 28
What is the difference between inheritance and polymorphism?
Question 29
What is the difference between base class pointers and derived class pointers in inheritance?
class Base {
public:
virtual void method() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
void method() override { std::cout << "Derived" << std::endl; }
};
int main() {
Derived d;
Base* bp = &d; // Base pointer to derived object - OK
// Derived* dp = bp; // Error: cannot convert without cast
Derived* dp = static_cast<Derived*>(bp); // OK with cast
bp->method(); // Calls Derived::method() (polymorphism)
dp->method(); // Calls Derived::method()
return 0;
}Question 30
What is the difference between inheritance and subtyping?
Question 31
What is the difference between inheritance coupling and composition coupling?
Question 32
What is the difference between virtual base classes and regular base classes?
Question 33
What is the difference between inheritance and mixins?
Question 34
What is the difference between inheritance refactoring and composition refactoring?
Question 35
What is the difference between inheritance documentation and composition documentation?
Question 36
What is the difference between inheritance testing and composition testing?
Question 37
What is the difference between inheritance maintenance and composition maintenance?
Question 38
What is the difference between inheritance performance and composition performance?
Question 39
What is the difference between inheritance evolution and composition evolution?
Question 40
What are the fundamental principles for effective inheritance design in C++?
