Structures: combining data elements

The program below uses a struct to declare an variable person which has as members a string name and an int yearBorn.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Structures
#include<iostream>
#include<string>
using namespace std;
struct Person {
  string name;
  int yearBorn;
}; //Person

int main() {
  Person student;
  student.name="Joe";
  student.yearBorn=1989;
  cout<<student.name<<" was born in "<<student.yearBorn<<endl;
  system("pause");
  return 0;
} //main
CODE
Output:
Joe was born in 1989
Press any key to continue . . .

Notice that members of a struct are accessed using the name of the structure following by a dot and the name of the member element.

NEXT: Declaring with initial values