Please explain answer 16.(10 pts) Consider the following structure declarations
ID: 3604819 • Letter: P
Question
Please explain answer
16.(10 pts) Consider the following structure declarations when answering the questions below. struct Date int day; int month; int year; 1i struct Person string name; string city; int age; Date dob ; // dob date of birth 1i a) Write a statement that declares the identifier Sam as a variable of DataType Person. b) Write a cout statement that will output the value of age of the variable Sam. c) Write a statement that declares the identifier Jane_Doe as a variable of DataType Person. d) Write a statement that assigns a value of 1950 to the year of the dob of Jane_Doe. e) Write a cout statement that will output the value of month of the dob of Jane_Doe.Explanation / Answer
NOTE: struct type variable can be decalred in the same way as we used to declare variable of predefined data type.
For example to declare a variable of data type int we write
int var;
but in order to access any member of struct variable we have to use dot operator which will be shown in answer
a) Person Sam;
above line will declare Sam as variable of struct type Person
b) Now in order to access any member of struct type we need to use dot operator, So to access age of the struct type variable Sam we need to use dot operator like Sam.age
so correct answer would be
cout << Sam.age;
c) Same as quetion a)
Person Jane_Doe;
d) if a variable of type struct conatin another member which is a struct and if we want to access the member , then we need to use nested dot operator. In this part Jane_Doe is a variable of struct Person which contain another struct Date type variable so to access year we need to use nested loop as Jane_Doe.dob.year.
So correct answer for this part would be
Jane_Doe.dob.year = 1950;
e) similar to above we can write
cout << Jane_Doe.dob.month ;