C++ — Inheritance
Basic inheritance
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
string name;
void eat() { cout << name << " is eating" << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << name << " is barking" << endl; }
};
int main() {
Dog dog;
dog.name = "Rex";
dog.eat(); // Inherited from Animal
dog.bark(); // Defined in Dog
return 0;
}
Access specifiers in inheritance
#include <iostream>
using namespace std;
class Base {
public:
int pub = 1;
protected:
int prot = 2;
private:
int priv = 3;
};
class Derived : public Base {
public:
void show() {
cout << pub << endl; // OK: public stays public
cout << prot << endl; // OK: protected stays protected
// cout << priv; // Error: private not accessible
}
};
int main() {
Derived d;
d.show();
cout << d.pub << endl; // OK
// cout << d.prot; // Error: protected
return 0;
}
| Base access | public inheritance | protected inheritance | private inheritance |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | Not accessible | Not accessible | Not accessible |
Constructor chaining
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "Base constructor" << endl; }
Base(int x) { cout << "Base(" << x << ")" << endl; }
};
class Derived : public Base {
public:
Derived() : Base() {
cout << "Derived constructor" << endl;
}
Derived(int x) : Base(x) {
cout << "Derived(" << x << ")" << endl;
}
};
int main() {
Derived d(42);
return 0;
}
// Output: Base(42) Derived(42)
Multi-level inheritance
#include <iostream>
using namespace std;
class LivingThing {
public:
void breathe() { cout << "Breathing" << endl; }
};
class Animal : public LivingThing {
public:
void move() { cout << "Moving" << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << "Barking" << endl; }
};
int main() {
Dog dog;
dog.breathe(); // From LivingThing
dog.move(); // From Animal
dog.bark(); // From Dog
return 0;
}
Multiple inheritance
#include <iostream>
using namespace std;
class Print {
public:
void print() { cout << "Printing..." << endl; }
};
class Scan {
public:
void scan() { cout << "Scanning..." << endl; }
};
class Printer : public Print, public Scan {
public:
void fax() { cout << "Faxing..." << endl; }
};
int main() {
Printer p;
p.print();
p.scan();
p.fax();
return 0;
}
The diamond problem
#include <iostream>
using namespace std;
class Person {
public:
string name;
};
// Virtual inheritance prevents duplicate base
class Student : virtual public Person {
public:
int studentId;
};
class Employee : virtual public Person {
public:
int employeeId;
};
class WorkingStudent : public Student, public Employee {
public:
void show() {
// Only one copy of Person
cout << "Name: " << name << endl;
cout << "Student ID: " << studentId << endl;
cout << "Employee ID: " << employeeId << endl;
}
};
int main() {
WorkingStudent ws;
ws.name = "Alice";
ws.studentId = 1001;
ws.employeeId = 2001;
ws.show();
return 0;
}
Override and final
#include <iostream>
using namespace std;
class Base {
public:
virtual void greet() { cout << "Hello from Base" << endl; }
virtual ~Base() = default;
};
class Derived : public Base {
public:
void greet() override { cout << "Hello from Derived" << endl; }
};
// Prevent further overriding
class FinalDerived final : public Base {
public:
void greet() override { cout << "Hello from FinalDerived" << endl; }
};
// class EvenMoreDerived : public FinalDerived { }; // Error: final class
int main() {
Derived d;
d.greet();
Base *ptr = &d;
ptr->greet(); // Polymorphic call
return 0;
}
Protected constructor
#include <iostream>
using namespace std;
class AbstractBase {
protected:
AbstractBase() { cout << "AbstractBase constructed" << endl; }
};
class Derived2 : public AbstractBase {
public:
Derived2() { cout << "Derived2 constructed" << endl; }
};
int main() {
// AbstractBase b; // Error: can't instantiate
Derived2 d; // OK: derived class can call protected constructor
return 0;
}
Mini Practice
Write C++ code that:
- Creates an
Animalbase class withDogandCatderived classes - Uses
overridekeyword on a virtual function - Demonstrates the diamond problem and virtual inheritance
- Shows constructor chaining order
Up Next
In the next lesson, you'll learn about Polymorphism — virtual functions and dynamic dispatch.
Related Topics
Frequently Asked Questions about Inheritance
What is Inheritance in C++?
Inheritance is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Inheritance?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Inheritance.
Why is Inheritance important in C++?
Inheritance is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.