example3_nested_structs_inheritance.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <vector>

struct Address {
    std::string street;
    std::string city;
    std::string country;
};

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

struct Employee : Person {
    int employeeId;
    double salary;

    void displayInfo() const {
        std::cout << "Name: " << name << ", ID: " << employeeId 
                  << ", Salary: $" << salary << std::endl;
        std::cout << "Address: " << address.street << ", " 
                  << address.city << ", " << address.country << std::endl;
    }
};

int main() {
    Employee emp;
    emp.name = "Charlie";
    emp.age = 35;
    emp.address = {"123 Main St", "Anytown", "USA"};
    emp.employeeId = 1001;
    emp.salary = 50000.0;

    emp.displayInfo();

    return 0;
}
Back to struct