Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, is a struct also a class? [duplicate]

Possible Duplicate:
C/C++ Struct vs Class

I know the technical differences between a struct and a class; and of course that question has been asked before.

Object-oriented programming relates objects and classes. In C++ taxonomy, is a struct also a class?

like image 421
user2023370 Avatar asked Apr 11 '11 10:04

user2023370


People also ask

Is struct same as class in C?

The only difference between a struct and class in C++ is the default accessibility of member variables and methods. In a struct they are public; in a class they are private.

Is a struct the same as a class?

Classes and Structs (C++)The two constructs are identical in C++ except that in structs the default accessibility is public, whereas in classes the default is private. Classes and structs are the constructs whereby you define your own types.

Can struct be replaced by class?

C structs are merely aggregates of raw data in the RAM. C++ struct is (almost) the same as C++ class. So yes, if you want to convert a class into a struct, you only need to change class for struct .

Can a struct have a class?

Yes, you can. The pointer to the class member variable is stored on the stack with the rest of the struct's values, and the class instance's data is stored on the heap. Structs can also contain class definitions as members (inner classes). Just curious, why did you cross out stack?


4 Answers

Yes, it's a full-blown class - struct keyword is a syntactic sugar that makes all members publicly accessible by default, while they are private by default in a class.

like image 117
sharptooth Avatar answered Sep 21 '22 15:09

sharptooth


Yes. The ONLY difference is that by default in a class everything is private, and in a struct, by default everything is public. The difference, in that sense is purely syntactical.

like image 23
satnhak Avatar answered Sep 21 '22 15:09

satnhak


Taxonomically, yes. Other than their different default access specifiers, they are exactly the same in C++.

  • All members and attributes of a struct are public by default.

  • All members and attributes of a class are private by default.

like image 24
Johnsyweb Avatar answered Sep 18 '22 15:09

Johnsyweb


  1. The member's of a struct are public by default
  2. The default inheritance for a class is private while for a struct it is public
like image 21
Sriram Subramanian Avatar answered Sep 17 '22 15:09

Sriram Subramanian