C++ Inheritance & Polymorphism: All Types, Virtual Functions, vtable, override, final & DSA (2025)
⚡ Quick Answer: Inheritance & Polymorphism in C++
- Inheritance:
class Derived : public Base— derived class gets all public/protected members - 5 types: single, multiple, multilevel, hierarchical, hybrid
- virtual: enables runtime dispatch via vtable — always call the correct derived version
- pure virtual:
= 0— forces derived class to implement; makes class abstract - override: compile-time check that you're actually overriding a virtual function
- virtual destructor: mandatory in any polymorphic base class
- diamond problem: solved with
virtual publicinheritance
Inheritance and polymorphism are the two pillars of C++ OOP — they let you model real-world
hierarchies, write reusable algorithms that work on any derived type, and build extensible
systems without modifying existing code. This guide covers everything from the 5 inheritance
types through vtable internals, the diamond problem, dynamic_cast, and five
complete DSA examples.
5 Inheritance Types
Single → Hybrid
Access Specifiers
public/protected/private
Virtual Functions
vtable explained
Pure Virtual & Abstract
Interfaces in C++
override & final
C++11 safety keywords
Diamond Problem
Virtual inheritance
dynamic_cast
Safe runtime cast
DSA Examples
Graph, Tree, Iterator
5 Inheritance Types With Examples
class Dog : public AnimalMost common — use for is-a.
class Amphibian : public Land, public WaterUse carefully — can cause diamond.
class C : public B where B : public AEach level specializes further.
class Dog : public Animalclass Cat : public AnimalOften causes diamond problem.
Needs virtual inheritance.
#include <iostream>
#include <string>
using namespace std;
// ── Single Inheritance ─────────────────────────────────────────
class Animal {
protected:
string name;
public:
explicit Animal(string n) : name(move(n)) {}
void breathe() const { cout << name << " breathes\n"; }
};
class Dog : public Animal { // Single inheritance
public:
explicit Dog(string n) : Animal(move(n)) {}
void bark() const { cout << name << " barks!\n"; }
};
// ── Multilevel Inheritance ─────────────────────────────────────
class GuideDog : public Dog { // Dog → Animal → GuideDog
public:
explicit GuideDog(string n) : Dog(move(n)) {}
void guide() const { cout << name << " guides owner\n"; }
};
// ── Hierarchical Inheritance ───────────────────────────────────
class Cat : public Animal { // Cat and Dog both inherit Animal
public:
explicit Cat(string n) : Animal(move(n)) {}
void meow() const { cout << name << " meows!\n"; }
};
int main() {
GuideDog gd("Buddy");
gd.breathe(); // from Animal
gd.bark(); // from Dog
gd.guide(); // own method
Dog d("Rex"); Cat c("Whiskers");
d.breathe(); c.breathe(); // both inherit breathe()
d.bark(); c.meow();
return 0;
}
Buddy breathes
Buddy barks!
Buddy guides owner
Rex breathes
Whiskers breathes
Rex barks!
Whiskers meows!Access Specifiers in Inheritance
| Base member | public inheritance | protected inheritance | private inheritance |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | inaccessible | inaccessible | inaccessible |
Virtual Functions & vtable Internals
vptr → → Circle vtable
[0]: Circle::draw
[1]: Circle::area → Circle::draw()
"Drawing circle"
vptr → → Square vtable
[0]: Square::draw
[1]: Square::area → Square::draw()
"Drawing square"
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Shape {
public:
// virtual → runtime dispatch via vtable
virtual void draw() const { cout << "Shape::draw\n"; }
virtual double area() const noexcept { return 0; }
virtual ~Shape() = default; // MUST be virtual
};
class Circle : public Shape {
double r;
public:
explicit Circle(double radius) : r(radius) {}
void draw() const override { cout << "Circle(r="<<r<<")\n"; }
double area() const noexcept override{ return 3.14159*r*r; }
};
class Square : public Shape {
double s;
public:
explicit Square(double side) : s(side) {}
void draw() const override { cout << "Square(s="<<s<<")\n"; }
double area() const noexcept override{ return s*s; }
};
// Works for ANY Shape — current or future derived types
void printInfo(const Shape& s) {
s.draw(); // virtual → calls correct derived version
cout << " area=" << s.area() << "\n";
}
int main() {
// Store heterogeneous shapes — unique_ptr for RAII
vector<unique_ptr<Shape>> shapes;
shapes.emplace_back(make_unique<Circle>(5.0));
shapes.emplace_back(make_unique<Square>(4.0));
shapes.emplace_back(make_unique<Circle>(3.0));
for (const auto& s : shapes) printInfo(*s);
// Without virtual: WRONG — always calls Shape::draw
Shape* p = new Circle(2.0);
p->draw(); // WITH virtual: Circle::draw ✓
delete p; // virtual dtor: ~Circle then ~Shape ✓
return 0;
}
Circle(r=5)
area=78.5398
Square(s=4)
area=16
Circle(r=3)
area=28.2743
Circle(r=2)Pure Virtual Functions & Abstract Classes
#include <iostream>
#include <memory>
using namespace std;
// Abstract class — cannot be instantiated directly
class Serializable {
public:
virtual string serialize() const = 0; // pure virtual — MUST override
virtual void deserialize(const string&) = 0;
virtual ~Serializable() = default;
// Non-pure virtual: has default, can override
virtual string format() const { return "json"; }
};
// Concrete class — implements all pure virtuals → instantiable
class UserRecord : public Serializable {
string name; int id;
public:
UserRecord(string n, int i) : name(move(n)), id(i) {}
string serialize() const override {
return "{\"id\":" + to_string(id) + ",\"name\":\"" + name + "\"}";
}
void deserialize(const string& data) override {
cout << "Deserializing: " << data << "\n";
}
};
// ⚠ This would be a COMPILE ERROR:
// Serializable s; // Cannot instantiate abstract class
void save(const Serializable& obj) {
cout << obj.format() << ": " << obj.serialize() << "\n";
}
int main() {
UserRecord u("Alice", 42);
save(u); // json: {"id":42,"name":"Alice"}
u.deserialize("{\"id\":43}");
return 0;
}
json: {"id":42,"name":"Alice"}
Deserializing: {"id":43}override, final & Virtual Destructors
#include <iostream>
using namespace std;
class Base {
public:
virtual void process() { cout << "Base::process\n"; }
virtual void compute(int x) { cout << "Base::compute("<<x<<")\n"; }
virtual ~Base() { cout << "~Base\n"; } // MUST be virtual
};
class Derived : public Base {
public:
// override: compile error if Base::process doesn't exist/is not virtual
void process() override { cout << "Derived::process\n"; }
// ⚠ Without override, this silently creates a NEW function (not override)
// void compute(double x) { } ← different signature — not an override!
void compute(int x) override { cout << "Derived::compute("<<x<<")\n"; }
// final: GrandChild cannot override this
virtual void locked() final { cout << "Derived::locked\n"; }
~Derived() override { cout << "~Derived\n"; }
};
// final class — cannot be subclassed
class LeafNode final : public Derived { };
// class Bad : public LeafNode { }; ← COMPILE ERROR
int main() {
Base* p = new Derived();
p->process(); // Derived::process ← virtual dispatch
p->compute(42); // Derived::compute ← virtual dispatch
delete p; // ~Derived then ~Base ← virtual destructor
return 0;
}
Derived::process
Derived::compute(42)
~Derived
~Baseoverride on every function that overrides a virtual. (3) final on classes and functions you explicitly don't want further overridden. These three together eliminate the most common inheritance bugs.
Diamond Problem & Virtual Inheritance
#include <iostream>
using namespace std;
// ── Without virtual inheritance — PROBLEM ─────────────────────
class A { public: int x = 10; };
class B : public A {};
class C : public A {};
class D : public B, public C {
// D has TWO copies of A::x — ambiguous!
// D::x is a compile error — use B::x or C::x
};
// ── With virtual inheritance — SOLUTION ───────────────────────
class AV { public: int val = 42; void show() { cout << "val="<<val<<"\n"; } };
class BV : virtual public AV {}; // virtual inheritance
class CV : virtual public AV {}; // virtual inheritance
class DV : public BV, public CV {
public:
// DV must directly initialize AV (the shared base)
DV() : AV(), BV(), CV() {}
};
int main() {
// Without virtual
D d;
cout << d.B::x << " " << d.C::x << endl; // 10 10 — two separate copies
d.B::x = 99;
cout << d.B::x << " " << d.C::x << endl; // 99 10 — B and C are independent
// With virtual inheritance — ONE shared AV
DV dv;
dv.show(); // no ambiguity — only one AV
dv.val = 100;
dv.show(); // val=100 — single copy ✓
return 0;
}
10 10
99 10
val=42
val=100dynamic_cast & Object Slicing
#include <iostream>
#include <memory>
using namespace std;
class Animal { public: virtual ~Animal()=default; virtual void speak()=0; };
class Dog : public Animal {
public:
void speak() override { cout << "Woof!\n"; }
void fetch() const { cout << "Fetching!\n"; } // Dog-specific
};
class Cat : public Animal { public: void speak() override { cout << "Meow!\n"; } };
int main() {
// ── dynamic_cast — safe runtime downcast ─────────────────
unique_ptr<Animal> a = make_unique<Dog>();
a->speak(); // Woof! — virtual dispatch
// Downcast to Dog* — safe (returns nullptr if not Dog)
Dog* d = dynamic_cast<Dog*>(a.get());
if (d) d->fetch(); // Fetching! — Dog-specific method
// Downcast to Cat* — returns nullptr (not a Cat)
Cat* c = dynamic_cast<Cat*>(a.get());
if (!c) cout << "Not a Cat\n";
// ── Object slicing — DANGER with pass-by-value ────────────
Dog dog;
Animal sliced = dog; // SLICED — only Animal part copied!
// sliced.speak() — would call Animal::speak, not Dog::speak
// (speak is pure virtual, so this actually won't compile)
// Fix: always use pointers or references for polymorphism
Animal& ref = dog;
ref.speak(); // Woof! — reference, no slicing
return 0;
}
Woof!
Fetching!
Not a Cat
Woof!DSA Examples: Graph, Tree, Strategy
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Graph { // Abstract graph interface
protected:
int V;
vector<vector<int>> adj;
public:
explicit Graph(int v) : V(v), adj(v) {}
virtual void addEdge(int u, int v) = 0; // pure virtual
virtual void printGraph() const = 0;
virtual ~Graph() = default;
int vertices() const noexcept { return V; }
};
class Directed : public Graph {
public:
using Graph::Graph;
void addEdge(int u, int v) override { adj[u].push_back(v); }
void printGraph() const override {
cout << "Directed:\n";
for (int i=0; i<V; i++) {
cout << " " << i << " → ";
for (int nb : adj[i]) cout << nb << " ";
cout << endl;
}
}
};
class Undirected : public Graph {
public:
using Graph::Graph;
void addEdge(int u, int v) override {
adj[u].push_back(v); adj[v].push_back(u);
}
void printGraph() const override {
cout << "Undirected:\n";
for (int i=0; i<V; i++) {
cout << " " << i << " — ";
for (int nb : adj[i]) cout << nb << " ";
cout << endl;
}
}
};
// Works with any Graph — current or future
void buildAndPrint(Graph& g) {
g.addEdge(0,1); g.addEdge(1,2); g.addEdge(0,2);
g.printGraph();
}
int main() {
Directed dg(3); buildAndPrint(dg);
Undirected ug(3); buildAndPrint(ug);
}
Directed:
0 → 1 2
1 → 2
2 →
Undirected:
0 — 1 2
1 — 0 2
2 — 1 0 #include <iostream>
#include <memory>
using namespace std;
struct Node { int val; unique_ptr<Node> left, right; explicit Node(int v):val(v){} };
class BinaryTree {
protected:
unique_ptr<Node> root;
void inorder_(const Node* n) const {
if (!n) return;
inorder_(n->left.get());
cout << n->val << " ";
inorder_(n->right.get());
}
public:
virtual void insert(int) = 0; // pure virtual — BST vs Random tree differ
void inorder() const { inorder_(root.get()); cout << endl; }
virtual ~BinaryTree() = default;
};
class BST : public BinaryTree {
void ins_(Node*& n, int v) {
if (!n) { n = new Node(v); return; }
v < n->val ? ins_((Node*&)n->left, v) : ins_((Node*&)n->right, v);
}
public:
void insert(int v) override { ins_((Node*&)root, v); }
};
int main() {
BST bst;
for (int x : {5,2,7,1,3}) bst.insert(x);
bst.inorder(); // 1 2 3 5 7
BinaryTree* t = &bst; // polymorphic use
t->insert(6);
t->inorder(); // 1 2 3 5 6 7
}
1 2 3 5 7
1 2 3 5 6 7 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class SortStrategy {
public:
virtual void sort(vector<int>& v) = 0;
virtual string name() const = 0;
virtual ~SortStrategy() = default;
};
class QuickSort : public SortStrategy {
public:
void sort(vector<int>& v) override { sort(v.begin(), v.end()); }
string name() const override { return "QuickSort"; }
};
class DescSort : public SortStrategy {
public:
void sort(vector<int>& v) override { sort(v.rbegin(), v.rend()); }
string name() const override { return "DescSort"; }
};
void run(SortStrategy& s, vector<int> v) {
s.sort(v);
cout << s.name() << ": ";
for (int x : v) cout << x << " ";
cout << endl;
}
int main() {
vector<int> data = {5,3,8,1,9};
QuickSort qs; run(qs, data);
DescSort ds; run(ds, data);
}
QuickSort: 1 3 5 8 9
DescSort: 9 8 5 3 1 Best Practices & Common Mistakes
✅ Virtual destructor in every polymorphic base
Any class with a virtual function needs virtual ~Base() = default;. Without it, deleting through a base pointer leaks resources.
✅ Always use override
Catches typos, signature mismatches, and overriding non-virtual functions — all silent bugs without override.
✅ Public inheritance for is-a only
If the derived class isn't truly a subtype of the base (Liskov Substitution Principle), use composition or private inheritance instead.
✅ Use pure virtual for interfaces
Force derived classes to implement required behavior. Documents the contract clearly and prevents incomplete implementations.
✅ Prefer unique_ptr for polymorphic collections
vector<unique_ptr<Base>> stores heterogeneous objects safely with automatic cleanup — better than raw pointer arrays.
✅ Mark non-extensible classes final
Signals design intent, prevents accidental subclassing, and enables compiler optimizations (devirtualization).
❌ Never call virtual in constructor/destructor
During construction, the vtable is not fully set up — virtual calls resolve to the base version. Use template method pattern if you need this.
❌ Avoid deep hierarchies (>3 levels)
Deep hierarchies are fragile — a base change breaks all descendants. Favor composition or flat hierarchies with multiple interfaces.
FAQ / Interview Questions
Base* p = new Derived(); p->method() — without virtual, always calls Base::method even though p points to a Derived object.class B : virtual public A and class C : virtual public A. This ensures D contains only one copy of A's data. D's constructor must directly initialize A. Virtual inheritance adds a small overhead (extra pointer) — use it only when you genuinely have the diamond structure.Base* p = new Derived(); delete p; — only ~Base() runs; ~Derived() is skipped, leaking any resources the derived class owns. With virtual destructor: ~Derived() runs first, then ~Base(). Rule: any class with virtual functions (polymorphic) must have a virtual destructor. Write virtual ~Base() = default;. This is one of the most common C++ bugs when forgotten.Animal a = dog; — a is now a pure Animal. Prevention: always use pointers or references for polymorphism: Animal& a = dog; or Animal* a = &dog;. Function parameters: void f(Animal a) slices; void f(Animal& a) or void f(Animal* a) does not. The C++ Core Guidelines recommend marking polymorphic base classes with a deleted copy constructor to catch accidental slicing.dynamic_cast: safe runtime downcast in a polymorphic hierarchy (requires virtual functions). Returns nullptr for pointers, throws std::bad_cast for references on failure. Use when you genuinely need to call derived-specific methods not in the base. static_cast: compile-time cast, no runtime check. For upcasting (derived to base — always safe), or when you are certain of the type (performance-critical code). Never use static_cast for downcasting in a polymorphic hierarchy — if you're wrong, it's undefined behavior. Frequent use of dynamic_cast often signals a design problem — prefer pure virtual interface design.Master C++ OOP — from inheritance to design patterns
Structured lessons, 200+ exercises, completion certificate. Join 50,000+ students on CoodeVerse.
Related C++ Topics on CoodeVerse
Frequently asked questions
What is inheritance in C++?
Inheritance is an OOP mechanism where a class (derived/child class) automatically acquires the data members and member functions of another class (base/parent class). This promotes code reuse — you write common behavior once in the base class and reuse it in all derived classes. Syntax: class Derived : public Base { ... }; The derived class can add new members, override inherited methods, and access protected members of the base. Inheritance models the 'is-a' relationship: a Dog is-a Animal, a Square is-a Shape.
What are the 5 types of inheritance in C++?
(1) Single: one derived class inherits from one base class — class Dog : public Animal. (2) Multiple: one derived class inherits from two or more base classes — class Amphibian : public Land, public Water. (3) Multilevel: chain of inheritance — class grand child : public Child where Child : public Parent. (4) Hierarchical: multiple derived classes inherit from the same base — class Dog : public Animal and class Cat : public Animal. (5) Hybrid: combination of multiple and multilevel, often involving the diamond problem.
What are access specifiers in C++ inheritance?
The access mode (public, protected, private) controls how base class members appear in the derived class. Public inheritance: public→public, protected→protected, private→inaccessible. Protected inheritance: public→protected, protected→protected. Private inheritance: public→private, protected→private. Most common: public inheritance for 'is-a' (Dog is-a Animal). Private inheritance for 'implemented-in-terms-of' (Stack implemented using vector without exposing vector's interface). Protected inheritance is rare.
What is polymorphism in C++ and what are its types?
Polymorphism means 'many forms' — the same interface can have different implementations dep ending on the object. Two types in C++: (1) Compile-time polymorphism (static dispatch): function overloading, operator overloading, templates. The correct version is chosen by the compiler at compile time. (2) Run-time polymorphism (dynamic dispatch): virtual functions with inheritance. The correct version is chosen at runtime based on the actual object type, via the virtual function table (vtable). Run-time polymorphism requires a base class pointer or reference to a derived object.
What is a virtual function in C++ and how does it work?
A virtual function is a member function declared with virtual in the base class that can be overridden in derived classes. When called through a base class pointer or reference, the correct derived class version runs — this is runtime polymorphism. Mechanism: each class with virtual functions has a vtable (virtual function table) — an array of function pointers. Each object has a hidden vptr (virtual pointer) pointing to its class's vtable. Calling a virtual function follows vptr → vtable → correct function. Without virtual: the base class version always runs regardless of the actual object type (static dispatch).
What is a pure virtual function and what is an abstract class in C++?
A pure virtual function is declared with = 0: virtual void draw() = 0; It has no implementation in the base class. A class with at least one pure virtual function is an abstract class — it cannot be instantiated (new abstract class() is a compile error). Derived classes MUST override all pure virtual functions to become concrete (instantiable). Use abstract classes to define interfaces: a Shape abstract class with pure virtual area() and draw() forces every concrete shape (Circle, Square) to implement them. Without implementation, derived classes are also abstract.
What is the override keyword in C++ and why should I use it?
override (C++11) tells the compiler that a function is intended to override a virtual function in a base class. Without override: void draw() — compiles silently even if you accidentally misspell the name or get the signature wrong (creating a new function instead of overriding). With override: void draw() override — compile error if no matching virtual function exists in a base class. Always use override on every function intended to override a virtual. It catches: typos in name, wrong parameter types, overriding a non-virtual function.
What is the final keyword in C++?
final (C++11) prevents further overriding or inheritance. On a virtual function: void draw() final — derived classes cannot override this function. On a class: class Circle final — no class can inherit from Circle. Use final when: a class is not designed for inheritance (enforces design intent, enables compiler optimizations), or a virtual function's override chain should stop. Example: class Square : public Shape { void area() final; } — Square::area() cannot be overridden in classes that inherit from Square.
What is a virtual destructor in C++ and when is it required?
A virtual destructor ensures the correct destructor is called when deleting a derived object through a base class pointer. Without virtual: Base* p = new Derived(); delete p; — only ~Base() runs; ~Derived() is skipped → resource leak. With virtual ~Base(): ~Derived() runs first (correct), then ~Base(). Rule: if a class has any virtual function (i.e., it's used polymorphically), its destructor MUST be virtual. Write: virtual ~Base() = default; in any polymorphic base class. Without it, deleting through a base pointer is undefined behavior.
What is the diamond problem in C++ and how is it solved?
The diamond problem occurs in multiple inheritance when two base classes share a common ancestor, causing ambiguity and data duplication. Example: class A { int x; }; class B : public A {}; class C : public A {}; class D : public B, public C {}; — D has two copies of A::x, and A:: references are ambiguous. Solution: virtual inheritance — class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {}; — D has only ONE copy of A's data, and the diamond is resolved. D's constructor must directly initialize A: D() : A(), B(), C() {}
What is dynamic_cast in C++ and when should I use it?
dynamic_cast safely converts a base class pointer/reference to a derived class pointer/reference at runtime. It works only with polymorphic classes (at least one virtual function). For pointers: auto* d = dynamic_cast<Derived*>(base ptr); — returns nullptr if the cast fails (object is not actually Derived). For references: dynamic_cast<Derived&>(ref) — throws std::bad_cast if the cast fails. When to use: when you need to call derived-class-specific methods not in the base class, or to check object type at runtime. Prefer designing with pure interfaces to avoid needing dynamic_cast — it often signals a design problem.
What is the difference between virtual and non-virtual function calls in C++?
Non-virtual call: always calls the function defined in the static (declared) type of the pointer/reference — compile-time dispatch, zero overhead. Virtual call: follows vptr to the vtable, looks up the correct function for the actual runtime type — runtime dispatch, ~1-3 ns overhead per call. Example: Base* p = new Derived(); p->non virtual() — calls Base::non virtual, ignores runtime type. p->virtual_fn() — calls Derived::virtual_fn, correct. Rule: without virtual, you always get the base class behavior regardless of the actual object. This is why forgetting virtual is a common serious bug.
What is the vtable in C++ and how does it enable runtime polymorphism?
The vtable (virtual function table) is a static array of function pointers, one per class with virtual functions. Each class has its own vtable. Each object has a hidden vptr (first 8 bytes of the object on 64-bit) pointing to its class's vtable. When you call a virtual function: (1) Follow the object's vptr. (2) Look up the function pointer at the correct offset in the vtable. (3) Call that function. This is why runtime polymorphism works: Derived overrides draw() → Derived's vtable has Derived::draw. Base* p = new Derived() — p's vptr points to Derived's vtable — p->draw() calls Derived::draw.
What is the difference between function overriding and function overloading in C++?
Function overloading: multiple functions with the same name but different parameter lists in the same scope — resolved at compile time (no inheritance required). Function overriding: a derived class redefines a virtual function from the base class with the same name and signature — resolved at runtime. Key differences: overloading needs different parameters; overriding needs same signature with virtual. Overloading is compile-time polymorphism; overriding is runtime polymorphism. Using override keyword confirms you're overriding — if signature doesn't match any virtual, it's a compile error.
What is object slicing in C++ and how do I prevent it?
Object slicing occurs when a derived class object is copied or assigned to a base class object (by value). The derived-specific data members are 'sliced off' — only the base portion is copied, losing polymorphic behavior. Animal a = dog; — a is a pure Animal, not a Dog. Solution: always use pointers or references for polymorphism: Animal* a = &dog; or Animal& a = dog; — no slicing. In function parameters: void process(Animal a) — slices. void process(Animal& a) or void process(Animal* a) — no slicing. Never pass polymorphic objects by value to a function expecting the base type.
When should I use inheritance vs composition in C++?
Use inheritance (is-a): when the derived class genuinely is a specialized version of the base class (Dog is-a Animal), and you need runtime polymorphism (treating heterogeneous objects uniformly). Use composition (has-a): when a class uses another class but is not a type of it (Car has-a Engine — not Car is-a Engine). Composition is more flexible: you can change the composed type at runtime and avoid tight coupling. Rule of thumb: if you find your self using inheritance primarily for code reuse without a true is-a relationship, switch to composition. Deep inheritance hierarchies (>3 levels) are usually a design smell.
How do C++ virtual functions differ from Java interfaces?
C++ virtual functions: optional override — a derived class can override or not. Abstract class with pure virtuals is closest to Java interface. Multiple inheritance allowed. Virtual dispatch via vtable (same mechanism). noexcept specifier on virtual functions. Java interface: all methods are implicitly abstract (until Java 8 default methods). Implements is explicit. No multiple class inheritance but multiple interface implementation. Virtual dispatch via interface table. Key difference: C++ gives you fine-grained control — you decide which functions are virtual, which are final, which are pure virtual. Java's design forces an interface-based approach. C++ templates (CRTP) can achieve zero-cost compile-time polymorphism with no vtable — not possible in Java.
What is constructor and destructor order in C++ inheritance?
Construction order: base class constructors run first (in inheritance order), then member constructors, then derived class constructor body. For class D : public B where B : public A — A's constructor runs, then B's, then D's. Destruction order: exact reverse. D's destructor runs, then B's, then A's. For multiple inheritance class D : public B, public C — construction: B then C then D. Destruction: D then C then B. This is why base class destructors must be virtual for polymorphic use — without virtual, delete on a base pointer only calls the base destructor, skipping all derived destructors.
How is inheritance used in DSA with C++?
Common DSA uses: (1) Abstract data type interfaces — Graph base class with pure virtual addEdge(), traverse(); directed graph and undirected graph override them. (2) Tree hierarchy — binary tree → BST → avl tree: each adds specialization without repeating base code. (3) Iterator pattern — Iterator base class with pure virtual next(), has next(); linkedlist iterator, tree iterator override. (4) Algorithm strategy — Sorter base with virtual sort(); quick sorter, merge sorter override. (5) Node types — graph node base; WeightedNode, ColoredNode extend. Runtime polymorphism lets you operate on base class pointers and get the correct behavior for each concrete type.
What are best practices for C++ inheritance and polymorphism?
(1) Virtual destructor in every polymorphic base class. (2) Always use override on overriding functions. (3) Mark leaf classes final if not designed for subclassing. (4) Prefer public inheritance for is-a; composition for has-a. (5) Avoid inheritance deeper than 3 levels. (6) Use pure virtual for interfaces — force derived classes to implement. (7) Never call virtual functions in constructors/destructors (dynamic dispatch not working yet). (8) Use dynamic_cast only when necessary — prefer interface design. (9) Avoid multiple inheritance of classes with data; use multiple interface inheritance. (10) Mark non-virtual base functions with non-virtual clearly.