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)

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

struct StructName {
dataType member1;
dataType member2;
};

Key Characteristics of a Structure in C++

  • The ?struct' keyword is used to define a structure.
  • 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 structure.
  • It helps initialize multiple members at once.
  • Total size of the structure is equivalent to the sum of the size 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 a time.
  • It supports flexible arrays.
  • Its instance can be created without a keyword.
  • It doesn't support protected access modifier.
  • It doesn't support inheritance.
  • It doesn't have a constructor or destructor.
  • The values allocated to structures are stored in stack memory.

Classes (class)

In C++, 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 control through access specifiers (public, private, protected). It supports encapsulation, inheritance, and polymorphism.

Syntax

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

Key Characteristics of a Class in C++

  • It is defined using ?class' keyword.

  • When data is defined in a class, it is stored in memory as a reference.

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

  • The reference type (before creating an object) is allocated on heap memory.

  • They can have constructors and destructors.

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

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

Comparison Table

Here is the following Comparison table for Static Vs Class.

Feature

struct class
Default Access Modifier public (members are publicly accessible by default) Private (members are private by default)
Member Functions Can have member functions Can have member functions
Inheritance Default Access public by default private by default
Usage Convention Mainly for simple data containers Used for complex data and behaviors
Memory Layout Same as class, but the layout is determined by member types and padding Same as struct, but with more emphasis on controlling data access
Encapsulation Supports encapsulation, but members are public by default Fully supports encapsulation with private members and public methods
Updated on: 2024-12-02T00:29:12+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements