Struct vs Class in C++

Struct vs Class in C++ with Examples:

In this article, I am going to discuss Struct vs Class in C++ with Examples. Please read our previous article where we discussed This Pointer in C++ with Examples.

What is Structure in C++?

The structure is a user-defined data type in C++ that combines logically related data items of different data types like float, char, int, etc., together. All the struct elements are stored at contiguous memory locations. The structure type variable helps us to store more than one data item of varying data types under one name.

Syntax of Class in C++:
class class-name
{
      // Data Members
      // Member Functions
};

In the above syntax, the class name is the name that is assigned to the class. The data members of the class are nothing but variable variables. The Member Functions are nothing but class functions.

What is Class in C++?

A class is a blueprint or a set of instructions to build a specific type of object. It is a basic concept of Object-Oriented Programming which revolves around real-life entities. Class is also a user-defined data type that can have different kinds of data members and member functions inside its body.

Syntax of Structure in C++
struct struct_name
{
// Struct Members
}

In the above syntax, the struct keyword is used to define a structure. The struct_name is the name of the structure. The struct members are added within curly braces.

Struct vs Class in C++:

Let us look at the difference between Structure and Class in C++. Let us understand this with an example. Please have a look at the below code.

#include <iostream>
using namespace std;
struct Demo
{
    int x;
    int y;

    void Display ()
    {
        cout << x << " " << y << endl;
    }
};
int main ()
{
    Demo d;
    d.x = 10;
    d.y = 20;
    d.Display ();
}

Here we have created a structure called Demo. Inside Demo, we have declared two integer type variables x and y. Then we created a function called Display which will print the values of x and y. Next inside the main function, we have created an object d of structure Demo. Then we have assigned some values to x and y and finally called the Display function. So, 10 and 20 will be displayed on the screen.

Difference between Structure and Class in C++:

Now, let us see the difference between structure and class in C++. In our example, we have data members as well as member functions. The structure is present in C language also but in C language you can have only data members inside a structure. You can’t have functions inside a structure in C Language. So, the difference between a structure of C language and C++ is that in C++, the structure can have data members as well as functions. This is the first point.

The next thing is the structure looks more like a class. In C++, the structure is nothing but like a class. So instead of writing class, we can even use structure in C++. Then what is the difference between structure and class?

In our example, inside the main function, we have an object of structure Demo and we have accessed everything from that object. Now instead of structure, let us make it as a class.

#include <iostream>
using namespace std;
class Demo
{
    int x;
    int y;

    void Display ()
    {
        cout << x << " " << y << endl;
    }
};
int main ()
{
    Demo d;
    d.x = 10;
    d.y = 20;
    d.Display ();
}
Output:

Difference between Structure and Class in C++ with Examples

As you can see in the above image, when we call the data members i.e. x and y inside the main function then we will get an error of accessing private members. So why data members and member function is private now? Because inside a class by default, everything is private and we cannot access these data members and functions. And in structure, we can access all the things. They were accessible so it means all members of a structure by default public only.

Note: The difference between structure and class in C++ is that they both are the same except in structure, by default, everything is public whereas in a class by default everything is private. So, if you want to make anything public then you have to write public otherwise everything is private in a class.

Main differences between the structure and class in C++:
  1. The most important difference between them is security. A Structure is not secure and cannot hide its implementation details from the end-user as everything in a structure is public while a class is secure and can hide its programming and designing details because of accessibility (private. protected and public).
  2. By default, all the members of a structure are public whereas all members of a class are private in C++.
  3. The structure in C++ will automatically initialize its members whereas constructors are used to initializing the class members and destructors are used to destroy the object.
  4. When a structure is implemented, it will allocate memory on the stack whereas, in class, the memory is allocated on the heap.
  5. We cannot initialize variables in a structure during the declaration but it is possible with class in C++.
  6. The structure is a value type in C++ whereas a class is a reference type.
  7. The structure is declared using the struct keyword while a class is declared using the class keyword.
  8. The structure is a user-defined data type that combines logically related data items of different data types like char, float, int, etc., together whereas class is a blueprint or a set of instructions to build a specific type of object.
Similarities Between Structure and Class in C++
  1. Both class and structure can declare any of their members as private.
  2. In C++, both class and structure support inheritance.
  3. Both class and structure are syntactically identical in C++.
When to use Class and Structure in C++?

If you have a large memory footprint or like to use a concept like inheritance, then you can choose a class. On the other hand, the structure can be used when you have a small memory field or footprint that is needed to initialize default values. Both classes and structs can have a mixture of public, protected, and private members that can use inheritance and can have member functions. So, I would recommend you to

  1. use struct for plain-old-data structures without any class-like features;
  2. use class when you make use of features such as private or protected members, non-default constructors, operators, etc.

In the next article, I am going to discuss Operator Overloading in C++ with Examples. Here, in this article, I try to explain Struct vs Class in C++ with Examples and I hope you enjoy this Struct vs Class in C++ with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.