C++ struct Keyword

The struct keyword in C++ is used to define a structure, which is a user-defined data type that groups together variables under one name. Structures are commonly used to represent data objects with multiple attributes.

A structure in C++ is similar to a class, but with one key difference: by default, all members of a structure are public, whereas members of a class are private. Structures can contain variables, functions, and even constructors and destructors.


Syntax

</>
Copy
struct StructName {
    // Members of the structure
    data_type member1;
    data_type member2;
    ...
};
StructName
The name of the structure.
member1, member2
Variables or functions that belong to the structure.

Examples

Example 1: Defining and Using a Simple Structure

In this example, we will see how to define and use a structure to group related variables.

</>
Copy
#include <iostream>
using namespace std;

struct Point {
    int x;
    int y;
};

int main() {
    Point p1;
    p1.x = 10;
    p1.y = 20;

    cout << "Point: (" << p1.x << ", " << p1.y << ")" << endl;
    return 0;
}

Output:

Point: (10, 20)

Explanation:

  1. The Point structure is defined with two integer members: x and y.
  2. In the main function, a Point object p1 is created, and its members are assigned values.
  3. The structure’s members are accessed using the dot operator (.).

Example 2: Structures with Member Functions

Structures in C++ can contain member functions just like classes.

</>
Copy
#include <iostream>
using namespace std;

struct Rectangle {
    int length;
    int width;

    int area() {
        return length * width;
    }
};

int main() {
    Rectangle rect;
    rect.length = 5;
    rect.width = 10;

    cout << "Area: " << rect.area() << endl;
    return 0;
}

Output:

Area: 50

Explanation:

  1. The Rectangle structure has two members (length and width) and one member function area().
  2. The area function calculates and returns the area of the rectangle.
  3. In the main function, the member function area() is called using the dot operator.

Example 3: Structures with Constructors

Structures can have constructors to initialize their members.

In this example, we will write a structure that has a constructor to initialise property of the structure.

</>
Copy
#include <iostream>
using namespace std;

struct Circle {
    double radius;

    Circle(double r) { // Constructor to initialize radius
        radius = r;
    }

    double area() {
        return 3.14159 * radius * radius;
    }
};

int main() {
    Circle c(5.0); // Initialize using constructor

    cout << "Area of Circle: " << c.area() << endl;
    return 0;
}

Output:

Area of Circle: 78.5397

Explanation:

  1. The Circle structure has a constructor that initializes the radius member.
  2. When a Circle object is created, the constructor is called to set the initial value of radius.
  3. The area function calculates and returns the area of the circle.

Key Points to Remember about struct Keyword

  1. Structures are user-defined data types that group related variables.
  2. By default, members of a structure are public.
  3. Structures can contain variables, functions, and constructors.
  4. The dot operator (.) is used to access structure members.