C++ is the best cross-platform language that is ideal for creating high-performance languages. It is one of the world’s most popular programming languages.

C++ was developed as an extension to the C language by Bjarne Stroustrup. It was updated three times in 2011, 2014, and 2017 with C++11, C++14, and C++17. 

C++ is a part of modern embedded systems, graphical user interfaces, and operating systems. It is an object-oriented language with a clean program structure that lowers development costs and allows the codes to be reused. It is easy to switch between C++ and Java, making it a developer’s favorite.

To start with C++, all you need are a text editor and a compiler. You can use Notepad to write C++ code, and GCC compilers can be employed to translate the C++ code into understandable computer language. An Integrated Development Environment (IDE) can be used to edit and compile C++ codes. 

In this article, we will discuss C++ Struct and learn how to create the instance and access the variable of a structure. We will also dive into the points that distinguish a structure from a class.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

What Is C++ Struct?

Classes and structures hold the collection of data of multiple data types. The structure is the collection of variables of different data types under a single name only. For example, if you require some details about a customer to be stored in the data, you can create the different variables like name, contact number, email id, etc. These variables can be used to store all information separately.

The future scope of data extends to storing information for multiple customers. To fulfill the requirements, you need to create different variables for each customer’s details. For example, name1, contact number1, email id1, name2, contact number2, and email id2. This will create a long code string with no interrelation between the variables like name, contact number, email id, etc.

The best solution to this lengthy coding is collecting all related information under a single customer name like customer1. You can then use it for different customers. This collection of all related information under a single name customer1 yields a clean, efficient, and readable cost and is called structure. The “struct” keyword defines a structure type followed by an identifier.

C++ Struct Syntax

struct structure_name

{

  // member declarations

}

The member variables of different types are declared inside the curly brackets. Thus, C++ Struct is declared by preceding the struct keyword and is followed by the structure name.

Example:

Struct Student

{

  char name[20];

  int id;

  int age;

}

Here, students are the structure containing three variables: name, id, and age. It allocated no memory at the time of declaration of structure. The memory is allocated when the variable of a structure is created.

How to Create the Instance of Structure?

The structure variable can be defined as-

student s;

It is the structure variable of student type. When the structure variable is created, the memory will be allotted according to its size. The student structure here has one char variable and two integer variables. Thus, the total memory occupied will be:

One char variable: 1 byte

Two integer variables: 2*4bytes = 8 bytes

Thus, the total memory occupied by the variable in the above case is 9 bytes.

How to Access the Variable of Structure?

It is easy to access the variable of C++ struct by simply using the instance of the structure followed by the dot (.) operator and the field of the structure.

For example:

s.id= 4;

Here, you’re accessing the id field of the C++ Struct Student by using the dot (.) operator. It assigns the 4 values to the id field.

Become a Certified UI UX Expert in Just 5 Months!

With The Best-in-class UI UX ProgramExplore Program
Become a Certified UI UX Expert in Just 5 Months!

Examples of C++ Struct

1. Simple Structure Named Rectangle With Two Data Members: Width and Height

#include <iostream>    

using namespace std;    

 struct rectdemo      

{      

   int w, h;       

 };      

int main(void) {    

    struct rectdemo rec;    

    rec.w=8;    

    rec.h=5;    

   cout<<"Area of Rectangle is: "<<(rec.w * rec.h)<<endl;    

 return 0;    

}    

Output:

Cpp_Struct_1

2. Simple Structure Using Constructor to Initialize Data and Find Area of Rectangle

#include <iostream>    

using namespace std;    

 struct Rectangle    {      

   int width, height;      

  Rectangle(int w, int h)      

    {      

        width = w;      

        height = h;      

    }      

  void areaOfRectangle() {       

    cout<<"Area of Rectangle is: "<<(width*height); }      

 };      

int main(void) {    

    struct Rectangle rec=Rectangle(4,6);    

    rec.areaOfRectangle();    

   return 0;    

}    

Output:

Cpp_Struct_2.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Differences Between Structure and Class

The differences between the structure and class in C++ are:

Structure in C++

Class in C++

No C++ Struct member can have a null value.

Members can have null values.

The instance of the structure is known as the “Structure variable.”

The instance of the class is known as “Object of the class.”

Memory is allocated on a stack.

Memory is allocated on the heap.

It is considered a value type.

It is considered as a reference type.

The default access specifier will be public in C++ Struct in case it is not declared explicitly.

The default access specifier will be private in the C++ class in case it is not declared explicitly.

Its Syntax is:

struct structure_name

{

  // member declarations.

}

 

Its Syntax is:

class class_name

{

// body of the class.

}

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Wrapping Up

Struct is the lightweight version of data types’ collection and is similar to a C++ class holding different data types. It is easy to create an instance and access the variables in C++ structures. This article covers all about the C++ struct, how to define it, and use it in any program. 

If you are you planning to learn about the practical usability of the C++ struct, you can enroll in  Simplilearn’s Full Stack Web Development course or you can explore Simplilearn’s SkillUp Program that offers free courses on the basics of computer programming. Simplilearn is one of the leading online education providers. Visit Simplilearn now to know about the trending programming skills from the leading mentors of the industry and gain skills and professional expertise with multiple software development professional courses. 

In case you have any questions for us, leave them in the comments section of this article and our experts will get back to you at the earliest.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449