structure in c and class in c++
what is difference between structure and class?
Collapse
X
-
Tags: None
-
Structures can not have functions in them.
Classes have the ADDED functionality of being able to have functions -
There is only one difference between a struct and a class: The default access for struct members is public whereas the default access for class members is private.
C++ comes from C and in C there is only one way to group data members together: a struct. Therefore, C++ has only structs.
A class is a term used in object technology to represent a group of data members managed by operations.
To make C++ look more object-like a "class" was added to the language. It is implemented as a struct with a private default access.
That means C++ structs can have constructors, destructors, etc.
Try it out. Write a class and get it working with a constructor, destructor and maybe another member function. Once it works, change the "class" keyword to "struct" and rebuild. You will see abolutely nothing happen. The code will work as before.Comment
Comment