C++语言程序设计

一个结构变量也可以是另一个结构类型的成员。例如，在例8-1程序中，结构employee的一个成员类型是room，room也是一个结构，它有数据成员number、floor和capacity：

| 例8-1

#include <iostream.h>
const int MAXIMUM_NAME_LENGTH = 50;
typedef char name_string[MAXIMUM_NAME_LENGTH];
struct room
{
int number;
int floor;
int capacity;
};
struct employee
{
name_string name;
int age;
double salary;
room location;
};
int main()
{
int place;
employee first_employee;
room room_one, room_two;
room_one.number = 1;
room_one.floor = 0;
room_one.capacity = 6;
room_two.number = 2;
room_two.floor = 3;
room_two.capacity = 4;
cout << "Please enter the employee's last name: ";
cin >> first_employee.name;
cout << "Please enter the employee's age: ";
cin >> first_employee.age;
cout << "Please enter the employee's current salary: ";
cin >> first_employee.salary;
cout << "Put in room 1 or in room 2? (type '1' or '2'): ";
cin >> place;
if (place == 1)
first_employee.location = room_one;
if (place == 2)
first_employee.location = room_two;
cout << endl << first_employee.name << " is now on floor ";
cout << first_employee.location.floor << "." << endl; return 0;
}

上例中的表达式first_employee.location.floor是读取first_employee 变量location成员的floor 成员。假定运行该程序，并给出下面的输入：
Please enter the employee's last name: Miller
Please enter the employee's age: 37
Please enter the employee's current salary: 110.76
Put in room 1 or in room 2? (type '1' or '2'): 1
则输出结果如下：
Miller is now on floor 0.
上面的程序中有两个赋值语句，均为将一个room结构变量复制到另一个结构变量（包括三个成员）：
...
first_employee.location = room_one;
...
first_employee.location = room_two;