struct vs class in C++
Maybe I should apologize for going on about C++ yet again, but, following on from the recent online lecture series that I conducted, I learned that there is a lot of interest in the topic. I also found the experience a very fruitful source of ideas, inspiration and queries – much of this will appear here.
During the lectures, I would periodically pose a question, which the attendees could answer via the chat. An interesting one was: how does a struct differ from a class in C++? …
I will start by defining a struct in C. I would see it as a customized, composite data type, which may be constructed from the existing built-in data types [int, char, etc.], bit fields [integers of specified bit size] and other structs. This example shows a simple example of a struct definition, along with the declaration of a variable of that type and access to one of the fields. A struct is a handy and flexible way to represent data. Similar facilities exist in most modern programming languages.
How does a C++ class differ from a C struct? There are a few differences. The key ones are:
- A class can also contain functions [called methods].
- The member variables and methods are hidden from the outside world, unless their declaration follows a public label.
- There can be a pair of special methods – the constructor and destructor – that are run automatically when an instance of the class [an object] is created and destroyed.
- Operators to work on the new data type can be defined using special methods [member functions].
- One class can be used as the basis for the definition of another [inheritance].
- Declaring a variable of the new type [an instance of the class; an object] requires just the name of the class – the keyword class is not required.
Most of these are illustrated in the example here.
But what about a struct in C++? The last example here gives a clue. 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.
Having imparted this information, I urge you not to exploit it too heavily. A key priority when you are writing code is to ensure that it is readable [=maintainable]. Somebody – it could be you – might need to look at this code in a year’s time and understand what it does. I have heard advice as follows: Assume that the person that will maintain your code is an armed psychopath, who has limited patience and knows your home address.
Comments
Leave a Reply
You must be logged in to post a comment.
Thanks for the info. That has been a confusing point for me.
” Assume that the person that will maintain your code is an armed psychopath, who has limited patience and knows your home address.” I have always contended that software should read like a book.
Frank: Glad it was helpful. Careful with the book analogy – I have come across some very unreadable books. 🙂
Thanks for clearing my concept in this regard. But I have a good resource on c++ in urdu which I would like to share with you people. The link is:
http://cplusplusinurdu.com
Thanks for sharing this information, even though currently i am not working in C++, this gave me good insight. Thanks a lot
There is one more difference: when deriving a struct, the default access-specifier is public. But when deriving a class, the default access specifier is private.
@Marton – Good point. You are quite correct – a small, but important, difference.
Very helpful. Thank you.
The point :
A class can also contain functions [called methods]. is incorrect. struct can also have member functions.
https://stackoverflow.com/questions/24196885/can-c-struct-have-member-functions
this website is helpful for the answer to the same question:
https://www.fluentcpp.com/2017/06/13/the-real-difference-between-struct-class/
Sorry @Harshit, you are incorrect. A C struct cannot contain a function. A C++ struct can. As I say at the end of my piece: “The only difference between a struct and class in C++ is the default accessibility of member variables and methods.”
Hello Colin Walls, i am Veysel you saying that “only difference between a struct and class in C++ is the default accessibility of member variables and methods” but is not important memory location for example struct variable in stack but class variable in heap (but class name in stack) ?
@Colin Walls
Correct from a strict technical point of view, but slightly incorrect. You can infact put a function in a C struct by referencing the function with a function pointer.
@El Roberto – That is true, but putting a pointer in a struct is very different from actually including a function, but I would admit that you can achieve similar results [just in a more roundabout way].
@veysel – there is no difference between a class and a stack with regard to storage in memory.
Thank you for the answering Colin Walls, i will follow your blogs
Well, thanks for clearing things up. Iwant to add that for inheritance if you don’t specify anything(default) then the struct will inherit publicly while the class will do private inheritance
@IBTIHEL – yes, the default visibility is applied there as well
C has strange namespaces (https://stackoverflow.com/questions/3793952/understanding-c-namespaces) so you often see
typedef struct StructTagName {
…
} StructTypeNameT;
C++ puts stucts (and classes) in the global namespace (ignoring namespaces andded with the ‘namespace’ keyword). No need to use a typedef or ‘struct’ before the StructTagName.