example1_definition_usage.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

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

int main() {
    Person person1;
    person1.name = "Alice";
    person1.age = 30;
    person1.height = 1.65;

    Person person2 = {"Bob", 25, 1.80};

    std::cout << person1.name << " is " << person1.age << " years old and " 
              << person1.height << "m tall." << std::endl;
    std::cout << person2.name << " is " << person2.age << " years old and " 
              << person2.height << "m tall." << std::endl;

    return 0;
}
Back to struct