views:

2332

answers:

4

can struct be inherited in C++ ?

+20  A: 

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

Alex Martelli
+4  A: 

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

Evan Teran
Thanks alot ..........
So, why not accept one of these as your answer?
GMan
+4  A: 

Yes. The inheritance is public by default.

Syntax (example):

struct A { };
struct B : A { };
struct C : B { };
Suvesh Pratapa
+3  A: 

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

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

Chad Gorshing
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
But PODs can have methods, so are not "like" C structs in the sense which cgorshing is talking about.
Steve Jessop