Structures and Classes in C++

August 16, 2019

Blog

Structures and Classes in C++

A shift to C++ has been anticipated for years. There is a very healthy interest in the language and some development teams have adopted it, but the expected wholesale transition has not yet occurred.

Currently, most embedded programming is done in C. A shift to C++ has been anticipated for many years. There is a very healthy interest in the language and some development teams have adopted it, but the expected wholesale transition has not yet occurred. Many engineers are very wary of the language, as they feel that using it relinquishes some of the control that they are accustomed to with C. For me, this wariness manifests itself in frequent questions about C++ for embedded.

A common question is: what is the difference between a struct and a class in C++?

Firstly, what is a structure in C? It may be defined 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 structures. Here is a simple example of the definition, declaration and use of a structure:

A struct is a convenient, flexible and readable way to represent data. Similar facilities exist in most modern programming languages.

So, back to the question (almost). How does a class in C++ differ from a structure in C? The key differences are:

  • A class can also contain functions (these are generally 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 that 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.

Here is an illustration of a number of these features:

 

So, what about a structure in C++? The answer is they are exactly the same thing as a class, but for one, tiny, difference. In a class, the member variables and methods are, by default, private and may only be accessed by methods in the same class. You need to have a public: label to make them visible elsewhere. In a structure, the contents are publicly accessible by default and you need a private: label to hide them.

Having answered the original question, I urge you not to extensively exploit this newfound knowledge. A key priority when you are writing code is to ensure that it is readable (=maintainable). Somebody who is not so clear about this small feature of the language 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 who will maintain your code is an armed psychopath, who has limited patience and knows your home address.

Categories
Software & OS