Metadata

  • ๐Ÿ“… Date :: 12-06-2025
  • ๐Ÿท๏ธ Tags :: cpp

Notes

Detailed Notes on Structs in C++

A struct (short for structure) is a custom data type in C++ that allows you to group variables of different data types together under a single name. It is commonly used to organize complex data in a manageable way. Unlike arrays that can only store multiple values of the same data type, a struct can hold variables of different types (e.g., integers, floats, strings, booleans, etc.).

In this guide, weโ€™ll break down how to create and use structs in C++, explaining each step with examples to make it easy to understand.


What is a Struct?

A struct is a way to bundle related variables under one name. Each variable within the struct is known as a member. The members can be of different data types, making structs very versatile.

Creating a Struct

To define a struct, you need to:

  1. Use the struct keyword to indicate that you are defining a structure.
  2. Provide a name for the structure (the struct name). This is similar to a data type.
  3. Define the members of the struct inside curly braces {}. Members are the individual variables that make up the structure.
  4. End the struct definition with a semicolon ;.

Example: Defining a Student Struct

Suppose you want to group information about students. You could define a struct named student that contains the following members:

  • name: a string to store the studentโ€™s name.
  • gpa: a double to store the studentโ€™s GPA.
  • enrolled: a boolean to store the studentโ€™s enrollment status.
struct student {
    string name;        // Name of the student
    double gpa;         // GPA of the student
    bool enrolled;      // Enrollment status of the student
};
 

Here, student is a new data type, and you can use it to create variables (or instances) of student type.

Declaring and Initializing Struct Variables

Once youโ€™ve defined a struct, you can create variables of that struct type. The syntax for declaring a struct variable is similar to declaring a regular variable but uses the struct name as the data type.

For example, to create a student variable named student1:

student student1;
 

This creates a variable student1 of type student, and it has three members: name, gpa, and enrolled.

Accessing Struct Members

You can access and modify the members of a struct using the dot operator (.). This is similar to how you access elements of an array or object.

Example: Setting and Accessing Members

student student1;  // Declare a student variable
student1.name = "Spongebob";  // Set name
student1.gpa = 3.2;           // Set GPA
student1.enrolled = true;     // Set enrollment status
 
// Access and display members
cout << student1.name << endl;     // Outputs: Spongebob
cout << student1.gpa << endl;      // Outputs: 3.2
cout << student1.enrolled << endl; // Outputs: 1 (true)
 

In the example above:

  • student1.name accesses the name member.
  • student1.gpa accesses the GPA member.
  • student1.enrolled accesses the enrollment status (represented as a boolean).

For booleans, true is displayed as 1, and false is displayed as 0.

Creating Multiple Instances of Structs

You can create as many instances of the struct as needed, each with its own set of data.

Example: Creating More Students

student student2;
student2.name = "Patrick";
student2.gpa = 2.1;
student2.enrolled = true;
 
student student3;
student3.name = "Squidward";
student3.gpa = 1.5;
student3.enrolled = false;
 

Each of these student variables (student1, student2, student3) has its own unique set of members.

Displaying Struct Members

You can display the members of a struct using cout or any other method of output. Hereโ€™s how you could display the information for each student:

cout << "Student 1: " << student1.name << ", GPA: " << student1.gpa << ", Enrolled: " << student1.enrolled << endl;
cout << "Student 2: " << student2.name << ", GPA: " << student2.gpa << ", Enrolled: " << student2.enrolled << endl;
cout << "Student 3: " << student3.name << ", GPA: " << student3.gpa << ", Enrolled: " << student3.enrolled << endl;
 

This will output:

Student 1: Spongebob, GPA: 3.2, Enrolled: 1
Student 2: Patrick, GPA: 2.1, Enrolled: 1
Student 3: Squidward, GPA: 1.5, Enrolled: 0

Default Member Values

Struct members can have default values if you want. This eliminates the need to manually assign a value to each member when you create a new instance of the struct. Default values can be assigned at the time of declaration inside the struct.

Example: Default Values for Members

struct student {
    string name = "Unknown";
    double gpa = 0.0;
    bool enrolled = true;  // Default to enrolled
};
 

Now, when you create a new student variable, the default values will be assigned unless you explicitly change them.

student student1;  // Automatically has default values
 
cout << student1.name << endl;  // Outputs: Unknown
cout << student1.gpa << endl;   // Outputs: 0.0
cout << student1.enrolled << endl; // Outputs: 1 (true)
 

Conclusion

Structs are a powerful feature in C++ that help organize and manage complex data by grouping different types of variables under one structure. They allow you to work with multiple data types efficiently and can be easily expanded to store more information as needed. Using structs can improve the clarity and organization of your code, especially when dealing with complex entities like students, employees, or any other real-world objects.

Key takeaways:

  • Structs group related variables under one name.
  • Members are accessed using the dot operator (.).
  • Multiple instances of a struct can be created, each with its own set of data.
  • Default values for struct members can be set at the time of declaration.

Structs are an essential part of C++ and can be used to model real-world objects and entities in your programs.


References