Introduction to C++ | Struct

C++ Struct

A struct (short for structure) in C++ is a user-defined data type that allows you to combine different types of data together. It is used to group related variables of different types under a single name. Structs are typically used to represent a record or an object, such as a person, a point in a 2D space, or a car with multiple attributes.

What is a Struct?

A struct is a data structure that can hold multiple data items, possibly of different types, under a single unit. Each data item in a struct is called a member or field.

In C++, structs are very similar to classes, but there is a key difference: by default, the members of a struct are public, while the members of a class are private.

Syntax of Struct

The syntax for defining a struct is as follows:

Syntax of Struct


        struct StructName {
            type member1;
            type member2;
            // More members
        };
                    

Example: Simple Struct

Here’s a simple example of defining and using a struct:

Code Example: Simple Struct


        #include <iostream>
        using namespace std;
        
        // Define the struct
        struct Person {
            string name;
            int age;
        };
        
        int main() {
            // Create an instance of the struct
            Person p1;
            p1.name = "John";
            p1.age = 30;
        
            // Accessing and printing the members of the struct
            cout << "Name: " << p1.name << endl;
            cout << "Age: " << p1.age << endl;
        
            return 0;
        }
                    

Output:

Name: John
Age: 30

Accessing Struct Members

Struct members can be accessed using the dot operator (.) when you have an instance of the struct. Each member of a struct can be of any data type, including other structs, arrays, or pointers.

Example: Accessing Struct Members

Here’s an example showing how to access struct members:

Code Example: Accessing Struct Members


        #include <iostream>
        using namespace std;
        
        // Define the struct
        struct Point {
            int x;
            int y;
        };
        
        int main() {
            // Create an instance of the struct
            Point p1;
            p1.x = 10;
            p1.y = 20;
        
            // Accessing the members of the struct and printing them
            cout << "x = " << p1.x << endl;
            cout << "y = " << p1.y << endl;
        
            return 0;
        }
                    

Output:

x = 10
y = 20

Structures with Functions

In C++, you can also define functions within structs, just like in classes. However, in structs, the functions are public by default.

Example: Struct with Function

Here’s an example where a function is defined inside a struct:

Code Example: Struct with Function


        #include <iostream>
        using namespace std;
        
        struct Rectangle {
            int width;
            int height;
        
            // Function inside the struct
            int area() {
                return width * height;
            }
        };
        
        int main() {
            // Create an instance of Rectangle
            Rectangle r1;
            r1.width = 10;
            r1.height = 5;
        
            // Access the function inside the struct
            cout << "Area of Rectangle: " << r1.area() << endl;
        
            return 0;
        }
                    

Output:

Area of Rectangle: 50

Passing Structs to Functions

Structs can be passed to functions by value or by reference. When passed by value, a copy of the struct is made, and any changes made inside the function do not affect the original struct. When passed by reference, any changes made inside the function are reflected in the original struct.

Example: Passing Struct by Reference

Here’s an example of passing a struct by reference to a function:

Code Example: Passing Struct by Reference


        #include <iostream>
        using namespace std;
        
        struct Person {
            string name;
            int age;
        };
        
        // Function to modify struct by reference
        void updateAge(Person &p) {
            p.age = 35;
        }
        
        int main() {
            Person p1 = {"Alice", 30};
        
            cout << "Before update - Age: " << p1.age << endl;
        
            updateAge(p1);  // Passing by reference
        
            cout << "After update - Age: " << p1.age << endl;
        
            return 0;
        }
                    

Output:

Before update - Age: 30
After update - Age: 35

Pro Tip:

💡 Pro Tip

Structures are useful for organizing related data. When you want to combine different data types into a single unit, structs make your code cleaner and more maintainable. If you need more functionality or control over data encapsulation, you may consider using classes instead of structs, as classes provide the added benefits of access control (public, private, etc.) and methods.