Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

C++ Structures

In C++, structures are used to group related data together to create a custom data type. Structures allow you to define your own data types with multiple members, making it easier to organize and manipulate complex data. In this article, we will explore the usage of structures in C++ with examples.

1. Defining Structures

To define a structure in C++, you use the struct keyword followed by the structure name and a list of member variables. Here's an example:

#include <iostream>

// Define a structure
struct Person {
    std::string name;
    int age;
    std::string address;
};

int main() {
    // Create a structure variable
    Person person1;

    // Access structure members
    person1.name = "John Doe";
    person1.age = 25;
    person1.address = "123 Main St";

    // Print structure members
    std::cout << "Name: " << person1.name << std::endl;
    std::cout << "Age: " << person1.age << std::endl;
    std::cout << "Address: " << person1.address << std::endl;

    return 0;
}

In the above code, we define a structure named Person with three member variables: name, age, and address. We then create a structure variable person1 and access its members using the dot operator (.). Finally, we print the structure members.

2. Initializing Structures

You can initialize structure variables at the time of declaration or later using the assignment operator. Here's an example:

#include <iostream>

struct Person {
    std::string name;
    int age;
    std::string address;
};

int main() {
    // Initialize structure at declaration
    Person person1 = {"John Doe", 25, "123 Main St"};

    // Initialize structure later
    Person person2;
    person2.name = "Alice Smith";
    person2.age = 30;
    person2.address = "456 Elm St";

    // Print structure members
    std::cout << "Name: " << person1.name << std::endl;
    std::cout << "Age: " << person1.age << std::endl;
    std::cout << "Address: " << person1.address << std::endl;

    std::cout << "Name: " << person2.name << std::endl;
    std::cout << "Age: " << person2.age << std::endl;
    std::cout << "Address: " << person2.address << std::endl;

    return 0;
}

In the above code, we demonstrate two ways to initialize structure variables. In the first method, we initialize person1 at the time of declaration using an initializer list. In the second method, we declare person2 and initialize its members separately using the assignment operator.

3. Passing Structures to Functions

You can pass structures to functions by value or by reference. Here's an example:

#include <iostream>

struct Person {
    std::string name;
    int age;
};

void printPerson(const Person& person) {
    std::cout << "Name: " << person.name << std::endl;
    std::cout << "Age: " << person.age << std::endl;
}

int main() {
    Person person = {"John Doe", 25};
    printPerson(person);

    return 0;
}

In the above code, we define a function printPerson that takes a constant reference to a Person structure. The function prints the name and age of the person. We pass the person variable to the function for printing.

Structures provide a convenient way to organize and manipulate related data in C++. They allow you to define custom data types with multiple members, making your code more structured and readable. Utilize structures to represent real-world entities and complex data structures effectively in your C++ programs.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners