238

Can a struct be inherited in C++?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

6 Answers6

352

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

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 2
    This 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
156

Yes. The inheritance is public by default.

Syntax (example):

struct A { };
struct B : A { };
struct C : B { };
SuPra
  • 8,488
  • 4
  • 37
  • 30
56

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.

b4hand
  • 9,550
  • 4
  • 44
  • 49
Chad Gorshing
  • 2,998
  • 32
  • 30
  • 7
    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. – camh Jun 11 '09 at 07:00
  • 11
    But 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
  • 2
    If 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
35

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Neha Agrawal
  • 411
  • 4
  • 9
30

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • Among? Are there any others? – Aaron Franke Oct 04 '22 at 18:14
  • @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
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
Rohan park
  • 61
  • 1
  • 4