Difference Between Structure and Class



In C++, both structures (struct) and classes (class) are user-defined data types, where they both give access to group different data elements (variables) and functions together. However, they still possess a few differences between them. In this article, we will see and go through its differences.

Structure (struct)

The struct is a user-defined data type, which allows the grouping of variables of different data types, with the members being public by default. This is commonly used to represent simple data structures, where encapsulation is not necessary. A struct can contain data members and member functions, but its primary use is to group data together.

Syntax

The syntax for struct is given below:

struct StructName {
    dataType member1;
    dataType member2;
};

Key Characteristics of a Structure in C++

The key characteristics of a structure(struct) in C++ are mentioned below:

  • The 'struct' keyword is used to define a structure in C++.
  • Every member in the structure is provided with a unique memory location.
  • When the value of one data member is changed, it doesn't affect other data members in the structure.
  • It helps initialize multiple members at once.
  • The total size of the structure is equivalent to the sum of the sizes of every data member.
  • It is used to store various data types.
  • It takes memory for every member which is present within the structure.
  • A member can be retrieved at any time.
  • It supports flexible arrays.
  • Its instance can be created without a keyword.
  • The values allocated to structures are stored in stack or heap memory depending on whether it has been allocated automatic or dynamic memory.

Classes (class)

In C++, a class is also a user-defined data type, which allows the grouping of variables (data members) and functions (member functions) under a single name. It defines a blueprint for creating objects and can encapsulate data, providing access control through access specifiers (public, private, protected). It supports encapsulation, inheritance, and polymorphism.

Syntax

The syntax for creating a class in C++ is given below:

class ClassName {
    public: 
    // Data members and member functions
};

Key Characteristics of a Class in C++

The key characteristics of a class in C++ are mentioned below:

  • It is defined using the 'class' keyword.

  • The data members of a class are stored inside each object.

  • It gets memory allocated only when an object of that class is created.

  • The objects are allocated to the stack or heap, depending on the object creation.

  • They can have constructors and destructors.

  • It can use inheritance to inherit properties from the base class.

  • The 'protected' access modifier can be used with the data members defined inside the class.

Difference Between Structure and Class

The following comparison table lists the differences between struct and class in C++.

Features struct class
Default Access Modifier The structure (struct) members are public by default. The class members are private by default.
Default Inheritance Type In structure (struct), the inheritance is public by default. In class, the inheritance is private by default.
Definition The structure is defined using the struct keyword. A class is defined using the class keyword
Syntax
struct StructName {
    dataType member1;
    dataType member2;
};
                    
class ClassName {
public: 
    // Data members and 
    member functions
}; 
                    
Use The structure is generally used for data holding or grouping. The classes are used in encapsulation and abstraction for data/information hiding.

Examples of Demonstrating Difference Between Structure and Class

Here are some examples differentiating between struct and class in C++:

Example 1

In this example, we have used a struct and a class to print the values of variables x, y, and z. The value of x and y is printed while z will give error as class is by default set to private and we can not directly access the private member.

#include <iostream>
using namespace std;

struct MyStruct
{
   int x = 10; // public by default
};

class MyClass
{
   int z = 20; // private by default

public:
   int y = 30; // public member
};

int main()
{
   MyStruct s;
   MyClass c;

   cout << "Struct x: " << s.x << endl;   //prints 10
   //cout << "Class x: " << c.z << endl;    //error 
   cout << "Class y : " << c.y << endl;   //prints 30
   return 0;
}

The output of the above code is as follows:

Struct x: 10
z will show error
Class y : 30

Example 2

In this example, we have demonstrated inheritance using struct and class. The struct prints the inherited message from its parent class as it is public by default.

#include <iostream>
using namespace std;

struct StructParent
{
   void message()
   {
      cout << "Hello from StructParent!" << endl;
   }
};

struct StructChild : StructParent
{
   // Inherits publicly by default
};

class ClassParent
{
public:
   void message()
   {
      cout << "Hello from ClassParent!" << endl;
   }
};

class ClassChild : ClassParent
{
   // Inherits privately by default
public:
   using ClassParent::message;
};

int main()
{
   StructChild s;
   s.message();

   ClassChild c;
   c.message();

   return 0;
}

The output of the above code is as follows:

Hello from StructParent!
Hello from ClassParent!
Updated on: 2025-05-15T16:13:38+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements