Structures: combining data elements

In this example, the manager is given initial values. Notice that the info (name and yearBorn) is enclosed in { and } inside the outer { and }.

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
//Structures: an Employee is a Person
#include<iostream>
#include<string>
using namespace std;
struct Person {
  string name;
  int yearBorn;
}; //Person
struct Employee {
  Person info;
  double hourlyRate;
}; //Employee
int main() {
  Employee manager;
  double hours,pay;
  manager.info.name="Smith";
  manager.info.yearBorn=1989;
  manager.hourlyRate=15.00;
  cout<<"How many hours did "<<manager.info.name<<" work:";
  cin>>hours;
  pay=hours*manager.hourlyRate;
  cout<<manager.info.name<<" earned $"<<pay<<endl;
  system("pause");
  return 0;
} //main
CODE
Output:
How many hours did Smith work:20
Smith earned $300
Press any key to continue . . .

Notice that we must use manager.info.name to access the Employees name because the name is inside the struct called Person.

NEXT: The time_t struct