C++ · Module 20 · Lesson 21 min read

OOP Interview Questions

Must-know answers

Q: What are the four pillars of OOP? Encapsulation, abstraction, inheritance, polymorphism.

Q: Overloading vs overriding?

OverloadingOverriding
Same name, different parametersSame signature in a derived class
Same scopeBase ↔ derived
Compile-time (static) polymorphismRuntime (dynamic) polymorphism, needs virtual

Q: What is a virtual function? How is it implemented? A function resolved at runtime by the object's actual type; usually via a vtable per class and a vptr per object.

Q: Why do we need a virtual destructor? Deleting a Derived through a Base pointer without one is undefined behaviour; the Derived destructor wouldn't run.

Q: Abstract class vs interface? Abstract: at least one pure virtual function, may have data/implemented functions. Interface (by convention): only pure virtual functions + virtual destructor.

Q: Can constructors be virtual? No. Destructors? Yes (and should be for polymorphic bases).

Q: What is object slicing? Copying a Derived into a Base by value drops the derived part.

Q: Diamond problem? Two copies of a common base via two paths; fixed by virtual inheritance.

Q: Composition vs inheritance? has-a vs is-a; prefer composition for code reuse.

Q: What is this? A pointer to the current object inside member functions.

Q: Shallow vs deep copy? Copying pointers vs copying what they point to.

Q: Rule of Three/Five/Zero? See Unit 10.

Q: Access specifiers & inheritance modes? public / protected / private (Unit 9.1).

Q: friend? Grants a function/class access to private members; not inherited, not transitive.

Q: Static vs dynamic binding? Compile-time choice (non-virtual, overloads, templates) vs runtime choice (virtual).

Example

C++
#include <iostream>
#include <memory>
#include <string>
#include <vector>

// Overloading: same name, different parameters (compile-time)
int area(int side) { return side * side; }
double area(double r) { return 3.14 * r * r; }

// Overriding + abstract class + virtual destructor (runtime)
class Shape {
public:
    virtual ~Shape() { std::cout << "  ~Shape\n"; }
    virtual double area() const = 0;                  // pure virtual: Shape is abstract
    virtual std::string name() const { return "shape"; }
};
class Square : public Shape {
public:
    explicit Square(double s) : s_(s) {}
    ~Square() override { std::cout << "  ~Square\n"; }
    double area() const override { return s_ * s_; }
    std::string name() const override { return "square"; }
private:
    double s_;
};

// Diamond with virtual inheritance
struct Person { std::string id = "P-1"; };
struct Student : virtual Person {};
struct Employee : virtual Person {};
struct TeachingAssistant : Student, Employee {};

// Shallow vs deep copy
struct Shallow { int* p; };
struct Deep {
    std::unique_ptr<int> p;
    explicit Deep(int v) : p(std::make_unique<int>(v)) {}
    Deep(const Deep& o) : p(std::make_unique<int>(*o.p)) {}
};

int main() {
    std::cout << "overloading: area(3) = " << area(3) << ", area(1.0) = " << area(1.0) << '\n';

    std::cout << "overriding via base pointer:\n";
    std::unique_ptr<Shape> s = std::make_unique<Square>(4);
    std::cout << "  " << s->name() << " area " << s->area() << '\n';
    std::cout << "virtual destructor runs both:\n";
    s.reset();

    TeachingAssistant ta;
    ta.id = "TA-7";
    std::cout << "diamond: one shared Person? " << std::boolalpha
              << (&static_cast<Student&>(ta).id == &static_cast<Employee&>(ta).id) << ", id " << ta.id << '\n';

    int value = 5;
    Shallow a{ &value }, b = a;
    *b.p = 99;
    std::cout << "shallow copy shares data: a sees " << *a.p << '\n';
    Deep c(5), d = c;
    *d.p = 99;
    std::cout << "deep copy is independent: c still " << *c.p << '\n';

    Square sq(2);
    Shape& ref = sq;
    std::cout << "static vs dynamic binding: " << ref.name() << " (dynamic), "
              << ref.Shape::name() << " (forced static)\n";
    return 0;
}

/* Output:
overloading: area(3) = 9, area(1.0) = 3.14
overriding via base pointer:
  square area 16
virtual destructor runs both:
  ~Square
  ~Shape
diamond: one shared Person? true, id TA-7
shallow copy shares data: a sees 99
deep copy is independent: c still 5
static vs dynamic binding: square (dynamic), shape (forced static)
  ~Square
  ~Shape
*/