Difference between struct and class (struct vs class in C++)

Here In this article, we will see the difference between the struct and class (struct vs class in C++). We will also learn when to use the class and struct in your code.

At the beginning of the career, many developers get confused by these two keywords class and struct, and they not able to decide when they should use a struct or a class.

I have seen mostly developers getting confused which comes from the C language background. I have also started my career in the C language, so it is also hard for me to differentiate between C struct and C++ struct. Like other beginners, I also ask my self “should I use a struct or a class?”.

So let’s start by clearing up the situation, by stating the actual difference between struct and class (struct vs class in C++). First of all, I want to state that in terms of language, struct, and class has only differences related to its member visibility. Also like class struct can have constructors, methods (static, non-static, or virtual), it supports access specifiers (public, private, and protected), and other properties like inheritance .etc.

By default member variables and methods of the struct is public while in a class they are private. Let’s see example codes to understand the statement,

#include <iostream> 
using namespace std;

class Data
{
    int m_data; // By default
    //m_data is private
};


int main()
{
    Data testObj;

    // compiler error because
    //m_data is private
    testObj.m_data = 20;

    return 0;
}

Output:
class vs struct

You can see that the m_data is a class member variable and we have not mentioned the access specifier in class.

So when we are trying to access m_data  from the object in the main function we are getting compile-time errors. 

But opposite that, if we do not use any access specifier, then struct members are public.

Consider the below example where I am able to access struct member variable without any issue.

#include <iostream> 
using namespace std;


struct Data
{
    int m_data; // By default
    //m_data is public
};


int main()
{
    Data testObj;

    testObj.m_data = 20;

    cout << testObj.m_data <<endl;

    return 0;
}

Output: 20

A C++ struct and class have one more difference in terms of inheritance,  when deriving a struct, the default access-specifier is public. But when deriving a class, the default access specifier is private. It means struct will inherit publicly while the class will privately.

#include <iostream>
using namespace std;


class Base
{
    public:
    int m_data; // By default
    //m_data is private
};


//derived class
class Dervied : Base // same thing as "class Dervied : private Base"
{

};

int main()
{
    Dervied testObj;

    // compiler error because
    //inheritance is private
    testObj.m_data = 20;

    cout << testObj.m_data <<endl;

    return 0;
}

Output:

struct vs class

You can see when I am trying to access the base class member getting error. It is because I have not used the public access specifier at the time of the child class declaration.

But if besides the class if we used the struct we will not get any error. Consider the below example.

#include <iostream>
using namespace std;


struct Base
{
    int m_data; //public by-default
};


//derived class
struct Derived : Base // same thing as "struct Derived : public Base"
{

};

int main()
{
    Derived testObj;

    testObj.m_data = 20;

    cout << testObj.m_data <<endl;

    return 0;
}

Output: 20

Comparison chart for struct and class (struct vs class)

Let us see a comparison chart for struct and class for quick information.

StructureClass
By default member variables of the struct is public.By default member variables of the class is private.
By default member methods of the struct is public.By default member methods of the class is private.
When deriving a struct, the default access specifier is public.When deriving a class, default access specifiers are private.

Summary:

Basically, if we left the accessibility, then there is no such difference between struct and class at the language level. struct and class and only differ in terms of the default visibility of their members.

  • struct members and base classes/structs are public by default.
  • class members and base classes/structs are private by default.

Now comes to another point that when we should use struct or class in the C++ code. I will cover this topic in another article, but you should remember a simple convention that if there is at least one private member then go for class. You should remember these two points, which helps you to decide where you should class or struct but it is totally up to you.

  • Use struct for plain-old-data structures without any class-like features (like member function, private data members,…etc)
  • Use class when you make use of features such as private or protected members, non-default constructors, and operators, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *