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++? …

1I 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:

  • 2A 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.

3But 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.

Post Author

Posted June 2nd, 2014, by

Post Tags

, ,

Post Comments


About The Colin Walls Blog

This blog is a discussion of embedded software matters - news, comment, technical issues and ideas, along with other passing thoughts about anything that happens to be on my mind. The Colin Walls Blog

@colin_walls tweets

Follow colin_walls


6 comments on this post | ↓ Add Your Own

Commented on 3 June 2014 at 15:00
By Frank Putnam-Jr

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.

Commented on 4 June 2014 at 08:57
By Colin Walls

Frank: Glad it was helpful. Careful with the book analogy – I have come across some very unreadable books. 🙂

Commented on 26 December 2014 at 14:04
By Awais kamran

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:

Commented on 25 June 2016 at 17:23
By Ravindra Kulkarni

Thanks for sharing this information, even though currently i am not working in C++, this gave me good insight. Thanks a lot

Commented on 13 December 2016 at 13:26
By Marton

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.

Commented on 15 December 2016 at 11:25
By Colin Walls

@Marton – Good point. You are quite correct – a small, but important, difference.

Add Your Comment