Structure in c++

GOCOURSE
Structure in c++


Structure:

A structure is a composite data type in C++ that gives a single name to variables of various data types. In C++, a structure is like a class, but the default access specifier is public rather than private.

Example:

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    float height;
};

int main() {
    Person p1 = {"Alice", 25, 1.65};
    Person p2 = {"Bob", 30, 1.75};

    cout << "Person 1: " << p1.name << ", " << p1.age << " years old, " << p1.height << " meters tall." << endl;
    cout << "Person 2: " << p2.name << ", " << p2.age << " years old, " << p2.height << " meters tall." << endl;

    return 0;
}

Output:

Person 1: Alice, 25 years old, 1.65 meters tall.
Person 2: Bob, 30 years old, 1.75 meters tall.


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send