Can a struct
be inherited in C++?
6 Answers
Yes, struct
is exactly like class
except the default accessibility is public
for struct
(while it's private
for class
).

- 854,459
- 170
- 1,222
- 1,395
-
2This is even the case in C++98, which I was surprised to learn, since all of my C++ textbooks leave out struct inheritance. – lil' wing Oct 15 '20 at 18:40
Yes. The inheritance is public by default.
Syntax (example):
struct A { };
struct B : A { };
struct C : B { };

- 8,488
- 4
- 37
- 30
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.

- 9,550
- 4
- 44
- 49

- 2,998
- 32
- 30
-
7a 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. – camh Jun 11 '09 at 07:00
-
11But PODs can have methods, so are not "like" C structs in the sense which cgorshing is talking about. – Steve Jessop Jun 11 '09 at 11:09
-
2If it's a POD, it doesn't have methods. Otherwise the name is meaningless. – RL-S Apr 01 '20 at 13:42
-
1[With these new rules a lot more types can be PODs now.](https://stackoverflow.com/a/7189821/10902172) @RL-S – yokus Aug 20 '22 at 20:12
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;
}

- 30,738
- 21
- 105
- 131

- 411
- 4
- 9
-
Can a class derive from a struct, and can a struct derive from a class? – Aaron Franke Oct 04 '22 at 18:13
-
What's the reason for default access becoming private, when it's public in the parent? – mavavilj Jan 02 '23 at 13:12
Of course. In C++, structs and classes are nearly identical (things like defaulting to public instead of private are among the small differences).

- 30,738
- 21
- 105
- 131

- 87,561
- 32
- 179
- 238
-
-
@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; };` – Evan Teran Oct 04 '22 at 20:40
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

- 61
- 1
- 4