Home | | Computer Science 11th std | C++: Nested Structures

Chapter: 11th Computer Science : Chapter 12 : Arrays and Structures

C++: Nested Structures

The structure declared within another structure is called a nested structure.

Nested Structures

 

The structure declared within another structure is called a nested structure. A structure ‘Student’was used to hold the student’s information in the earlier examples. Date of birth can be included in the student’s information. There are three components in the date of birth namely, date, month and year like 25-NOV-2017. Hence, another structure is used to keep the date of birth of a student. The following code creates a structure for the date of birth.

struct dob

{

int date;

char month[3];

int year;

};

 

Values can be assigned to this structure as follows.

dob= {25,”NOV”,2017}

 

The date of birth can be assigned as one of the elements in the student structure

nested structures act as members of another structure and the members of the child structure can be accessed as parent structure name. Child structure name. Member name.

struct Student

{

      int age;

      float height, weight;

      struct dob

      {

      int date;

      char month[4];

      int year;

      };

}mahesh;

void main( )

{

      cout<< “ Enter the age:”<<endl;

      cin>>mahesh.age;

      cout<< “Enter the height:”<<endl;

      cin>>mahesh.height;

      cout<< “Enter the weight:”<<endl;

      cin>>mahesh.weight;

      cout<< “The Date of birth:”<<endl;

      cout<< “ Enter the day:”<<endl; cin>>mahesh.dob.date;

      cout<< “Enter the month:”<<endl;

      cin>>mahesh.dob.month;

      cout<< “Enter the year:”<<endl;

      cin>>mahesh.dob.year;

      cout<< “The values entered for Age, height and weightare”<<endl;

      cout<<mahesh.age<< “\t”<<mahesh.height<< “\t”<<mahesh.weight<<endl;

      cout<< “His date of Birth is:”<<endl<<mahesh.dob.date<< “-”<<mahesh.

      dob.month<< “-” <<mahesh.dob.year;

}

Output:

Enter the age:

18

Enter the height:

160.5

Enter the weight:

46.5

The Date of birth

Enter the day:

25

Enter the month:

NOV

Enter the year:

2017

The values entered for Age, height and weight are

18  160.5  46.5

His date of Birth is:

25-NOV-2017

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 12 : Arrays and Structures : C++: Nested Structures |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.