C++ Struct Inheritance

In this tutorial we will discuss the concept of Inheritance in C++ Structures (also know as C++ Struct).

Just to clarify, only C++ Structs actually support Inheritance as C++ is an OOP language. C structs are more traditional, and do not support many features that C++ Structs. For a complete list of differences refer to this article.

Note: This tutorial focuses mostly on showcasing Inheritance within structs, rather the concept of Inheritance. For a more detailed look into Inheritance in C++, refer to our Inheritance with C++ tutorial.


Inheritance in C++ Structures

Let’s take a look at a simple example where we Inherit from one C++ Struct to the other. If you have done in inheritance in Classes before, you should know that is the exact same.

All you have to do is follow the following format:

struct Student : public Person

The above code, the Struct Student inherits from Parent using the public access modifier. The default access modifier is already public, we don’t actually have to mention this.

Example# 1

The below code features a short and interesting example on Inheritance. Notice how through the Initializer list, we initialized the variables that we inherited from the Parent. We have printed out the variables to show their currently stored values.

This is also inline with how Structs are usually used. Despite having the ability to hold functions and utilize OOP concepts like Constructors, we use structs mainly to just group Data. This is to create some distinction between the uses of a Class and a Structure in C++.

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    char gender;
};

struct Student : Person {
    int rollNo;
    string department;
};

int main() {
    Student s1 = {"Bob", 15, 'M', 146, "IT"};

    cout << s1.name << endl;
    cout << s1.age << endl;
    cout << s1.gender << endl;
    cout << s1.rollNo << endl;
    cout << s1.department << endl;
}
Bob
15
M
146
IT

Also remember, that we can access each variable inside the struct like this easily, as they are all public by default, unlike Classes.


Example# 2

Now let’s throw a function into the mix. In the below code, notice how we called the printPersonDetails() on the Student object, even though the printPersonDetails() function was part of the Person Class. This is all thanks to inheritance!

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    char gender;

    void printPersonDetails() {
        cout << name << ", " << age << ", " << gender << endl;
    }
};

struct Student : Person {
    int rollNo;
    string department;

    void printStudentDetails() {
        cout << rollNo << ", " << department << endl;
    }
};

int main() {
    Student s1 = {"Bob", 15, 'M', 146, "IT"};

    s1.printPersonDetails();
    s1.printStudentDetails();
}
Bob, 15, M
146, IT

This marks the end of the C++ Struct Inheritance Tutorial. Any suggestions or contributions are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments