Back

14th November 2024

#Programming#C++

Differences between struct and class keywords in C++

Blog image

In C++ we have actually two ways to create something, that is understand as "class". There are two keywords, which enable us to do it: class and struct.

class Person { 
  // ...
};
struct Person { 
  // ...
};

Difference

There is only one difference between both. It's about default access modifiers.

struct MyStruct {
    int x;  // public by default

    private:
      int y;  // private
};

class MyClass {
    int x; // private by default
     
    public:
      int y;  // public
};

Why are there two such keywords in C++?

struct keyword must exist in C++ mainly for compatibility with C programming language. This keyword was used in C to group simple data and create slightly more complex structures with them. It did not support methods or other OOP mechanisms.

Addition of class keyword was intended to help programmers distinguish between two concepts: "simple structure" (struct) and "class in the sense of object-oriented programming" (class). Struct was intended to create simple groups of data and class was intended to be used for full OOP. However, in practice this distinction turned out to be unimportant, because struct in C++ supports all OOP mechanisms. As a result, we were left with two keywords that are almost functionally identical.

Which one is better to use?

Both structs and classes in C++ supports all OOP mechanisms and all access modifiers. They can actually be used interchangeably, but there are some conventions I have heard about, which I would like to reccommend:

Back to articles