C++ is the best cross-platform language that is ideal for creating high-performance languages. It 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 one of the world’s most popular programming languages.

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 that makes it a developer’s favorite.

It is easy to start with C++. All you need are a text editor and a compiler. Notepad can be used 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. You will know all about the C++ Struct in this article.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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. In such 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 string of code with no interrelation between the different 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 that contains three variables namely 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. As you know that when the structure variable is created, the memory will be allocated, hence the memory according to its size will be allotted. 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 and 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.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Examples of C++ Struct

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

#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 the Constructor to Initialize Data and Method to Calculate the Rectangle’s Area

#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.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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

This article covers all about the C++ Struct, how to define it, and use it in any program. Struct is the lightweight version of data types’ collection. It is similar to a C++ class holding different data types. Struct is a value type compared to classes which fall under reference type like the classes. Overall, classes and structs are used to create an instance of classes in C++. It is easy to use, create an instance, and access the variables in C++ structures.

Are you planning to learn about the practical usability of the C++ Struct? It is easy to learn all details about structures in C++ from Simplilearn’s Full Stack Web Development course. It is an easy process to become a part of the skill up to learn the basics of computer programming for free. In case you’re looking for some certified software development professional courses, Simplilearn is one of the leading online education providers. Get to know about the trending programming skills from the leading mentors of the industry to gain skills and professional expertise.

Have any questions for us? Leave them in the comments section of this article and our experts will get back to you on the same, ASAP!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.