257

Can a struct be inherited in C++?

0

6 Answers 6

368

Yes, struct is exactly like class except the default accessibility is public for struct (while it's private for class).

Sign up to request clarification or add additional context in comments.

1 Comment

This is even the case in C++98, which I was surprised to learn, since all of my C++ textbooks leave out struct inheritance.
164

Yes. The inheritance is public by default.

Syntax (example):

struct A { };
struct B : A { };
struct C : B { };

1 Comment

when using typedef structs: typedef struct B : A { ... } B
59

Other than what Alex and Evan have already stated, I would like to add that a C++ struct is not like a C struct.

In C++, a struct can have methods, inheritance, etc. just like a C++ class.

4 Comments

a C++ struct can be like a C struct. When it is, its called a POD - Plain Old Datatype. It is an important distinction, since for example, only POD structs can be part of unions.
But PODs can have methods, so are not "like" C structs in the sense which cgorshing is talking about.
If it's a POD, it doesn't have methods. Otherwise the name is meaningless.
39

In C++, a structure's inheritance is the same as a class except the following differences:

When deriving a struct from a class/struct, the default access-specifier for a base class/struct is public. And when deriving a class, the default access specifier is private.

For example, program 1 fails with a compilation error and program 2 works fine.

// Program 1
#include <stdio.h>

class Base {
    public:
        int x;
};

class Derived : Base { }; // Is equivalent to class Derived : private Base {}

int main()
{
    Derived d;
    d.x = 20; // Compiler error because inheritance is private
    getchar();
    return 0;
}

// Program 2
#include <stdio.h>

struct Base {
    public:
        int x;
};

struct Derived : Base { }; // Is equivalent to struct Derived : public Base {}

int main()
{
    Derived d;
    d.x = 20; // Works fine because inheritance is public
    getchar();
    return 0;
}

3 Comments

Can a class derive from a struct, and can a struct derive from a class?
What's the reason for default access becoming private, when it's public in the parent?
This doesn't answer whether the default base exposure is public when the base vs when the derived type is a struct. Experimentation shows the default is public when the derived type is a struct, regardless of whether the base is a class or struct.
30

Of course. In C++, structs and classes are nearly identical (things like defaulting to public instead of private are among the small differences).

2 Comments

Among? Are there any others?
@AaronFranke fair enough, IIRC, there are exactly two differences, which both can be described by my response: 1. defaulting to publicly inheriting, and 2. members default to public visibility. So in essence, these two are the same: struct T : Base { int x; }; and class T : public Base { public: int x; };
6

Yes, c++ struct is very similar to c++ class, except the fact that everything is publicly inherited, ( single / multilevel / hierarchical inheritance, but not hybrid and multiple inheritance ) here is a code for demonstration

#include<bits/stdc++.h>
using namespace std;

struct parent
{
    int data;
    parent() : data(3){};           // default constructor
    parent(int x) : data(x){};      // parameterized constructor
};
struct child : parent
{
    int a , b;
    child(): a(1) , b(2){};             // default constructor
    child(int x, int y) : a(x) , b(y){};// parameterized constructor
    child(int x, int y,int z)           // parameterized constructor
    {
        a = x;
        b = y;
        data = z;
    }
    child(const child &C)               // copy constructor
    {
        a = C.a;
        b = C.b;
        data = C.data;
    }
};
int main()
{
   child c1 ,
         c2(10 , 20),
         c3(10 , 20, 30),
         c4(c3);

    auto print = [](const child &c) { cout<<c.a<<"\t"<<c.b<<"\t"<<c.data<<endl; };

    print(c1);
    print(c2);
    print(c3);
    print(c4);
}
OUTPUT 
1       2       3
10      20      3
10      20      30
10      20      30

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.