tags:

views:

144

answers:

3

Do structures support inheritance? I think it's stupid question, but I have not much idea about it.

What is the meaning of writing code like this:

struct A {
   void f() { cout << "Class A" << endl; }
};

struct B: A {
   void f() { cout << "Class B" << endl; }
};

In structures also private section will come, don't they give encapsulation? What is the major difference between structures and classes?

+6  A: 

Structures are classes with default visibility public. Everything else is equal.

ebo
+10  A: 

Yes structures support all features that classes do. The differences are:

  • structure inheritance is public by default
  • structure members are public by default
anon
We can't use struct keyword in templates?
yesraaj
@yesraaj: that's not so much a difference between structs and classes, it's a separate syntactic use of the `class` keyword which the `struct` keyword doesn't share. More properly, we probably shouldn't even be talking about "structures" in C++: `struct A {};` defines a class.
Steve Jessop
Is it possible to inherit Struct's from Class's (and reverse) or this yields undef behavior?
sinec
@sinec: yes, it's possible. Structs and Classes don't just behave similarly, *they are the exact same thing*. It's just that there are two different keywords which can be to define a class, and which keyword you use determines the default accessibility.
Steve Jessop
@Steve Jessop Thanks!
sinec
@Steve Jessop Thanks
yesraaj
Hi all..Thanks for ur response... if structure is providing encapsulation then why dont we use struct always.. i know class have more advantages like virtual func etc... but will it matter any thing else apart from it?
Shadow
@Solitaire: I *do* always use `struct` to define my classes, and I'm sure others do too. On the whole, it results in more consistent, clear, and concise code.
Roger Pate
@Solitaire Classes DON'T have those advantages - please re-read my answer. You could use struct always if you wanted, but it is normally reserved for Plain Old Data (POD) i.e. use in the same way that it would be used in C. This is only a naming convention, however.
anon
@Roger Really? I've never seen that done before - do your colleagues do the same?
anon
"class have more advantages like virtual func etc" - no. structs and classes are *the exact same thing*. Structs can have virtual functions. It's just that C++ programmers tend to maintain a convention that if something is mostly like a C struct, they'll use the struct keyword to define it, whereas if it's complex they'll use class. The definition of "mostly" and "complex" is not universally agreed: some people use struct strictly for POD classes, others use it in other cases where public access doesn't really matter (like locally-defined functors). Others ignore any such convention.
Steve Jessop
@Neil: Some do, some don't. It's an internal detail and can easily be flipped at any time with no repercussions (as you know), so there's no sense starting a holy war over syntax. (If it's intended that something be POD, then that's documented specifically and clearly.)
Roger Pate
@Roger: struct results in more concise code if (and only if) you put the public members first in your class definitions. If you put the private members first, then it's less concise. Which illustrates about how important I think the distinction is, and how much effect I think it has on clarity ;-)
Steve Jessop
@Steve: For large classes you still have bases which are public much more often than not (at least in code I see) and the benefit of being consistent, but yes, the benefit of being concise is relatively less there than it is for small classes (can be a big clarity benefit for lots of small TMP and traits classes).
Roger Pate
@Neil: `structs` are not reserved for POD use. `structs` are to maintain compatibility with the C language. As others have stated, anything that can be done with a `struct` can be done with a `class`. The difference between the two is the default accessibility settings of functions and data members. If I'm wrong, please guide me to the sections in the language spec.
Thomas Matthews
@Thomas 1) PODs are C. 2) I stated that. 3) Not only that - see my answer.
anon
+3  A: 

In C++ only difference between a structure and a class is that for structure the method/member variable visibility is public by default and for class it is private by default. Other than that there is no difference.

Naveen