[C++] Introduction to Class Inheritance | How to Create New Classes by Reusing Existing Ones

目次

Introduction

“Inheritance” is one of the three pillars of Object-Oriented Programming. It is a powerful mechanism that allows you to define a new class by inheriting the functions (member variables and member functions) of an existing class.

For example, if you have a basic class called “Vehicle,” you can inherit from it to create “Car” and “Airplane” classes. Both “Car” and “Airplane” share the basic function of “moving forward” from “Vehicle,” but each can add its own unique functions (e.g., “Car” can reverse, “Airplane” can fly).

This avoids code duplication and dramatically improves program reusability and extensibility. In this article, I will explain the basic usage of class inheritance in C++.

Sample Code for Class Inheritance

This code defines a Person class that handles basic information, and then inherits from it to create a Manager class that holds more specialized information.

Complete Code

#include <iostream>
#include <string>

using namespace std;

// --- 1. Definition of Parent Class (Base Class) ---
class Person {
public:
    string name;
    int age;

    void introduce() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << " years old" << endl;
    }
};

// --- 2. Definition of Child Class (Derived Class) ---
// Inherit public members of Person class
class Manager : public Person {
public:
    // Member unique to Manager class
    string department;
};

int main() {
    // --- 3. Create and use an object of the Child Class ---
    Manager managerA;
    
    // Set members inherited from Parent Class (Person)
    managerA.name = "Taro Suzuki";
    managerA.age = 45;
    
    // Set member unique to Child Class (Manager)
    managerA.department = "Development Dept";
    
    // Call member function inherited from Parent Class
    managerA.introduce();
    
    // Access member unique to Child Class
    cout << "Position: Manager (" << managerA.department << ")" << endl;
    
    return 0;
}

Execution Result

Name: Taro Suzuki
Age: 45 years old
Position: Manager (Development Dept)

Code Explanation

1. Parent Class and Child Class

  • Parent Class (Person): This is the source class for inheritance. It is also called a “Base Class” or “Super Class.”
  • Child Class (Manager): This is the new class created by taking over the functions of the parent class. It is also called a “Derived Class” or “Sub Class.”

2. class Manager : public Person

This is the syntax for inheritance.

  • class Manager: The name of the new child class to be defined.
  • : : Specify the parent class to inherit from after the colon.
  • public Person: This means inheriting the Person class publicly. When performing public inheritance, public members of the parent class are inherited as public members in the child class. This allows access like managerA.name from within the main function.

3. Using Inherited Members

In the definition of the Manager class, members like name, age, and introduce() are not written at all. However, because it inherits from the Person class, the object of the Manager class (managerA) can use these member variables and functions freely as if they were its own members.

Summary

In this article, I explained the basics of class “Inheritance” in C++.

  • Inheritance is a mechanism to create a new class by reusing the features of an existing class.
  • It is defined using the syntax: class ChildClassName : public ParentClassName.
  • The child class can use the public members of the parent class as its own.

Inheritance is a central feature for enhancing code reusability in object-oriented programming. By grouping common functions in a parent class and assigning specific functions to child classes, you can design programs that are efficient and easy to extend.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次