-1

I have the following code in c++:

typedef struct
{
   int a;
   int b;
   float c;
} Data;

class DataInfo : public Data
{
   // some code here
};

my question is can class inherit struct in C++? how this happens?

user2131316
  • 3,111
  • 12
  • 39
  • 53
  • the same things? how come? – user2131316 Aug 26 '13 at 13:08
  • except all elements in a class are private by default – Alexis Aug 26 '13 at 13:09
  • @user2131316 The only difference between a `struct` and a `class` is that the members of a `class` is `private` by default whereas the members of a `struct` are `public` by default. Same thing goes for inherritance – olevegard Aug 26 '13 at 13:09
  • @user2131316: history. To make C's `struct`s classes they are just treated the same except that everything in a `struct` is `public` by default while in a class everything is `private` by default. – Dietmar Kühl Aug 26 '13 at 13:10
  • then this means they are not the same – user2131316 Aug 26 '13 at 13:10
  • @user2131316 It's a tiny difference, and if you state access modifier explicitly, it's totally insignificant. – olevegard Aug 26 '13 at 13:11
  • @user2131316 They are. Both the `class` and `struct` keywords are used to declare and define **classes**. The definitions look different, but the entities they define behave in indistiguishable manner. – R. Martinho Fernandes Aug 26 '13 at 13:11
  • @user2131316 they are essentially the same. You can write exactly the same types using either. You can forward declare something as a `struct` and then define it as a `class`. Or vice versa. – juanchopanza Aug 26 '13 at 13:12
  • 4
    Please don't use the nasty `typedef struct {} Name` construct. It's ugly and unnecesarry! – John Dibling Aug 26 '13 at 13:13
  • @JohnDibling, I do not understand why it is unnecessary – user2131316 Aug 26 '13 at 13:25
  • 1
    It's unnecesarry because it gets you exactly *nothing*. Just do `struct Name {};` instead. – John Dibling Aug 26 '13 at 13:42

1 Answers1

9

struct is included in C++ to provide complitability with C. It has same functionality as class, but members in struct are public by default. So you can inherit class from struct and do the same in reverse.

Nazar554
  • 4,105
  • 3
  • 27
  • 38