C++: Inheritance Basics

Last updated: 2026-08-26

Imagine you wrote Student and Teacher classes, and found they both have name and age, but students have student IDs and teachers have employee IDs.

Can you reuse the Person class code? Inheritance does exactly that — creating a "parent-child" relationship between classes.


1. What Is Inheritance?

(1) 1.1 Inheritance in Everyday Life

Real-Life Example Program Equivalent
A child inherits traits from parents (eye color, height, etc.) A derived class inherits members from its base class
A child can have their own traits (like special talents) A derived class can add new members

The essence of inheritance: Creating new classes by extending existing ones.

(2) 1.2 Why Do We Need Inheritance?

Without inheritance (duplicate code):

▶ Example 2: Code Example (Difficulty ⭐)

CPP
#include <iostream>
#include <string>

// Student class
class Student {
 std::string name;
 int age;
 std::string studentId;
};

// Teacher class
class Teacher {
 std::string name; // ❌ Duplicated
 int age; // ❌ Duplicated
 std::string teacherId;
};
▶ Try it Yourself

Output:

TEXT 📖 Display only
(program output)

With inheritance (code reuse):

CPP
#include <iostream>
#include <string>

// Base class (parent class)
class Person {
public:
 std::string name;
 int age;
};

// Derived class (child class)
class Student : public Person {
public:
 std::string studentId; // New member
};

class Teacher : public Person {
public:
 std::string teacherId; // New member
};


2. Basic Syntax of Inheritance

(1) 2.1 Declaring a Derived Class

Syntax:

TEXT 📖 Display only
class DerivedClassName : inheritanceMode BaseClassName {
 // new members
};

Example:

CPP
#include <iostream>
#include <string>

class Person {
public:
 std::string name;
 int age;
};

// Student inherits Person
class Student : public Person {
public:
 std::string studentId;
};

int main() {
 Student s1;
 s1.name = "Alice"; // ✅ Inherited name
 s1.age = 20; // ✅ Inherited age
 s1.studentId = "2024001"; // Own member
 
 std::cout << "Name: " << s1.name << std::endl;
 std::cout << "Age: " << s1.age << std::endl;
 std::cout << "Student ID: " << s1.studentId << std::endl;
 
 return 0;
}


3. Inheritance Modes (Access Control)

(1) 3.1 Three Inheritance Modes

Base Class Member \ Inheritance Mode public Inheritance protected Inheritance private Inheritance
public members public (unchanged) protected private
protected members protected protected private
private members Not accessible Not accessible Not accessible

💡 Key Point: The most commonly used inheritance mode is public inheritance.

CPP
#include <iostream>
#include <string>

class Person {
private:
 std::string id; // Private member: not accessible by derived class
 
protected:
 std::string address; // Protected member: accessible by derived class
 
public:
 std::string name; // Public member: accessible by everyone
 
 void introduce() {
 std::cout << "My name is " << name << std::endl;
 }
};

class Student : public Person {
public:
 void setAddress(const std::string& addr) {
 address = addr; // ✅ Can access protected member
 // id = "123"; // ❌ Cannot access private member
 }
};

int main() {
 Student s1;
 s1.name = "Alice"; // ✅ Accessible
 // s1.address = "Wuhan"; // ❌ Not accessible (protected)
 
 return 0;
}


4. Constructors and Inheritance

(1) 4.1 Derived Class Constructor Call Order

Rule: The base class constructor is called first, then the derived class constructor.

CPP
#include <iostream>
#include <string>

class Person {
public:
 Person() {
 std::cout << "Person constructor" << std::endl;
 }
};

class Student : public Person {
public:
 Student() {
 std::cout << "Student constructor" << std::endl;
 }
};

int main() {
 Student s1;
 return 0;
}

💡 Key Point: The destructor call order is reversed — the derived class destructor is called first, then the base class destructor.



5. Practice: Simple Student Management System (Inheritance Version)

▶ Example 1: Implementation Using Inheritance (Difficulty ⭐⭐)

CPP
#include <iostream>
#include <string>

// Base class
class Person {
protected:
 std::string name;
 int age;
 
public:
 Person(const std::string& n, int a) : name(n), age(a) {}
 
 void introduce() {
 std::cout << "Name: " << name << ",Age: " << age << std::endl;
 }
};

// Derived class: Student
class Student : public Person {
private:
 std::string studentId;
 double score;
 
public:
 Student(const std::string& n, int a, const std::string& id, double s)
 : Person(n, a), studentId(id), score(s) {}
 
 void introduce() {
 Person::introduce(); // Call base class introduce
 std::cout << "Student ID: " << studentId << ",Score: " << score << std::endl;
 }
};

int main() {
 Student s1("Alice", 20, "2024001", 92.5);
 s1.introduce();
 
 return 0;
}
▶ Try it Yourself

Output:

TEXT 📖 Display only
Name: Alice,Age: 20
Student ID: 2024001,Score: 92.5

❓ FAQ

Q What are the inheritance modes? Which one should I use?
A > - public inheritance: most commonly used, preserves the base class's interface > - protected inheritance: rarely used > - private inheritance: rarely used

▶ Example 3: Derived Class Constructor (Difficulty ⭐)

CPP
#include <iostream>
#include <string>

class Animal {
protected:
    std::string name;
public:
    Animal(std::string n) : name(n) {}
    void speak() { std::cout << name << "makes a sound" << std::endl; }
};

class Dog : public Animal {
public:
    Dog(std::string n) : Animal(n) {}
    void speak() { std::cout << name << "barks" << std::endl; }
};

int main() {
    Dog dog("Wangcai");
    dog.speak();

    return 0;
}
▶ Try it Yourself

Output:

TEXT 📖 Display only
Wangcaibarks
💡 Tip: Derived class constructors need to call the base class constructor to initialize base class members. Derived classes can override base class member functions.


📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Define an Animal base class with a name member. Have Dog and Cat classes inherit from it.

  2. Intermediate (Difficulty ⭐⭐): Extend the above program by adding a void speak() virtual function to Animal (you'll learn about virtual functions later), and have Dog and Cat override it.

  3. Challenge (Difficulty ⭐⭐⭐): Design a library management system class hierarchy:

  4. Person (name, age)

  5. Student inherits Person (student ID, number of books borrowed)

  6. Teacher inherits Person (employee ID, number of books borrowed)

  7. Implement borrow and return functionality.

  8. Inheritance: derived classes have all members of the base class

  1. Derived class constructors call base class constructors
  2. is-a relationship: a derived class is a kind of base class

6. 🚀 Next Steps

Now that you've learned inheritance basics, let's move on to Polymorphism (Lesson 32) — allowing base class pointers to call derived class functions for "one interface, multiple implementations"!

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏