Difference between Structures and Classes in C++

In this article we will attempt to explain the difference between a Structure and a Class. This is a pretty interesting debate, and throughout this article you are about to learn exactly what Structures are capable of, and just how similar to Classes they are.

Understanding Structures

There are a few common misconceptions about Structures, as most people don’t use them much in favor of Classes. People typically use structs for holding a bunch of variables, without knowing what else they can be used for. So before we continue any further, let’s quickly review some of the things that structs have in common with Classes.

1. They can hold Functions. It may come as a surprise to some that structures can also have functions as members within them, used in the same way as Classes.

2. They can inherit from other structs!

3. They can have constructors and destructors too!

4. Access Modifiers are supported by Structures as well!

At this point, you might be wondering whether there even is a difference between Classes and Structures in C++. That’s just how similar they are. In the next section we will take a look at the few differences that they do actually have.

Note: There are significant differences in C structs and C++ structs. As C++ is an OOP language, C++ struct has several OOP properties that are not present in C structs, such as having functions as members, constructors and destructors, and Inheritance.

Difference between Class and Struct

There are only two minor differences between the workings of Classes and Structs, that are listed below.

Default Access Specifier: In C++, Structures by default have all member variables and functions declared as public. For those who don’t know, this means they can be accessed by using the dot operator by functions outside of the Struct.

On the other hand, in Classes all member variables and functions are by default private, which means they can only be accessed by functions within the Class itself (other member functions).

Inheritance: Another minor difference is that when Inheritance occurs in structs, it is by default public Inheritance. On the other hand, with Classes, the default Inheritance is private.

To learn more about private and public inheritance, check out our tutorial on Inheritance in Classes.

Evaluating the Differences between Classes and Structures

So we’ve taken a look at the differences between the two. Now the confusion is, as to where structs should be used, and where classes should be used.

If you noticed, none of the two differences mentioned earlier, actually have any effect on what either of them are capable of. All you need to do is slap a private access modifier on a struct, and it suddenly becomes the same as a Class.

Luckily, there are some guidelines that we can follow then deciding whether to use Structs or Classes for a given scenario.

  1. If you want to be backward compatible with the C language, then you will have to use Structs, but without the whole OOP part of it. A structure without the OOP features like Constructors and Functions, is referred to as a “P.O.D”, which stands for Plain Old Datatype.

    Defining a Struct in this way will ensure it’s compatible with C, which may come in handy while interfacing with C libraries.

2. Secondly, as good practice, use structs when you want objects with just a few variables (basically a POD). On the other hand, use Classes when you want to make use of Constructors, Destructors, Inheritance etc. This helps keep some distinction between their uses.

This marks the end of the “Differences between Structures and Classes in C++” article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Notify of
Inline Feedbacks
View all comments