C++ Structures (struct)

In C++, a structure (or struct) allows multiple related variables to be grouped together under one name. This makes organizing and managing complex data more efficient than using individual variables.

Tutorials dojo strip



Defining a Structure

Unlike arrays, structures can store multiple data types, including int, string, and bool.

Example: Declaring a Structure

struct {  
    int engineSize;  // Integer member  
    string bikeModel;  // String member  
} motorcycle;  // Structure variable

This structure groups an engine size (int) and a model name (string) under motorcycle.




Assigning and Accessing Structure Members

Use the dot (.) operator to assign values and access structure members.

Example: Assigning and Printing Structure Members

#include <iostream>
using namespace std;

int main() {
    struct {  
        int engineSize;  
        string bikeModel;  
    } motorcycle;  

    // Assign values to structure members  
    motorcycle.engineSize = 1000;  
    motorcycle.bikeModel = "ZX-10R";  

    // Print structure members  
    cout << motorcycle.engineSize << "cc\n";  
    cout << motorcycle.bikeModel << "\n";  

    return 0;
}

Output:

1000cc  
ZX-10R  




Using One Structure for Multiple Variables

To create multiple instances of the same structure, separate structure variables with commas (,).

Example: Two Motorcycles Using One Structure

#include <iostream>
using namespace std;

int main() {
    struct {  
        string brand;  
        string model;  
        int year;  
    } bike1, bike2;  

    // Assign values to the first bike  
    bike1.brand = "Ducati";  
    bike1.model = "Panigale V4";  
    bike1.year = 2022;  

    // Assign values to the second bike  
    bike2.brand = "Yamaha";  
    bike2.model = "R1";  
    bike2.year = 2021;  

    // Print details  
    cout << bike1.brand << " " << bike1.model << " " << bike1.year << "\n";  
    cout << bike2.brand << " " << bike2.model << " " << bike2.year << "\n";  

    return 0;
}

Output:

Ducati Panigale V4 2022  
Yamaha R1 2021  




Named Structures

To make a structure reusable anywhere, give it a name after the struct keyword.

Example: Creating a Named Structure

struct Motorcycle {  
    string brand;  
    string model;  
    int year;  
};

To declare a variable using this structure:

Motorcycle myBike;



Example: Using a Named Structure

#include <iostream>
using namespace std;

// Declare a named structure
struct Motorcycle {  
    string brand;  
    string model;  
    int year;  
};

int main() {
    // Create motorcycle instances
    Motorcycle bike1;  
    bike1.brand = "Honda";  
    bike1.model = "CB1000R";  
    bike1.year = 2020;  

    Motorcycle bike2;  
    bike2.brand = "Kawasaki";  
    bike2.model = "Z900";  
    bike2.year = 2021;  

    // Print structure members  
    cout << bike1.brand << " " << bike1.model << " " << bike1.year << "\n";  
    cout << bike2.brand << " " << bike2.model << " " << bike2.year << "\n";  

    return 0;
}

Tutorials dojo strip
Scroll to Top