0

Possible Duplicate:
What are the differences between struct and class in C++

do structures support OOP as classes??

Community
  • 1
  • 1
Prashant
  • 61
  • 1
  • 1
  • 2

3 Answers3

6

in C++, there is almost no difference between struct and class. yes OOP is available with the keyword struct.

you can consider both class and struct the same except that:

  • by default visibility in struct is public, while it's private for class
  • by default inheritance in struct is public: while it's private for class.
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
4

There is no difference between struct and class in C++ except that struct members are public by default and class members are private by default. What ever else holds for class holds for struct as well in C++.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • you forgot about inheritance accessors. – UmmaGumma Feb 07 '11 at 10:09
  • @Ashot : Yes, but Stephane has mentioned it. I believe OP should understand anything by default is public in struct and private in class. That is what I meant. Thanks. – Mahesh Feb 07 '11 at 10:12
  • @Philipp. What do you mean by different ? – Mahesh Feb 07 '11 at 10:20
  • 1
    The same as the others said. I was too late, and since it's already been said a thousand times, I'll delete my comment. But mentioning the difference with respect to inheritance visibility is extremely important and often forgotten. – Philipp Feb 07 '11 at 10:23
0

in C++ and .NET you can have methods defined for a struct as well, in C++ a struct has all members public by default while a class has members private by default. A struct can implement an interface but cannot derive a base class.

structs are value types while classes are reference types.

In my opinion and point of view since a struct cannot derive another class, it does not fully support OOP, that's why we have classes. :)

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Not a .NET question (admittedly, was only revealed later on, therfore no downvote). Extra credit for not preaching the irrelevant and wrong "structs go to stack" though. –  Feb 07 '11 at 10:01