ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Nested Structures : Structures within Structures

Updated on December 27, 2009

Structures can be implemented with functions and arrays. Moreover, structures can be implemented as the member of other structure. This is termed as structure within structure.  

When a structure is decleared as the member of another structue, it is called Structure within a structure. It is also known as nested structure.

Example of nested structure

One example of nested structure is given below:

struct date{
	// members of structure
        int day;
	int month;
	int year;
};

struct company{
	char name[20];
	long int employee_id;
	char sex[5];
	int age;
	struct date dob;};

Write a program which can collect employee information (such as employee name, id, sex, date of birth etc.) of a company and display the collected information t

#include<iostream.h>
#include<string.h>
#define MAX 1000

// structure block defination
struct date{
	// members of structure definition
	int day;
	int month;
	int year;
};

struct company{
	char name[20];
	long int employee_id;
	char sex[5];
	int age;
	// nested block defination
	struct date dob;
};

// structuer object defination
company employee[MAX];
// main function starts
int main(){
	company employee[MAX];
	int n;
	cout << "A program for collecting employee information";
	cout << endl;
	cout << "And displaying the collected information";
	cout << endl << endl;
	cout << "How many employees:";
	cin >> n;
	cout << endl;
	// functions defination
	void CollectInformtion(company employee[MAX], int n);
	void Display(company employee[MAX], int n);
	
	// functions calling
	CollectInformtion(employee, n);
	Display(employee, n);
	cin.get();
	return 0;
}
// collecting information form the user
void CollectInformtion(company employee[MAX], int n){
	cout << "Enter the following information:";
	cout << endl << endl;
	
	for (int i=1; i<=n; i++){
		cout << "Enter information for employee no: " << i;
		cout << endl;
		cout << "Name   :"; cin >> employee[i].name;
		cout << "ID     :"; cin >> employee[i].employee_id;
		cout << "Sex    :"; cin >> employee[i].sex;
		cout << "Age    :"; cin >> employee[i].age;
		cout << "Date of Birth:" << endl;
		cout << "Day    :";	cin >> employee[i].dob.day;
		cout << "Month  :"; cin >> employee[i].dob.month;
		cout << "Year   :";	cin >> employee[i].dob.year;
		
		cout << endl;
	}
}
// displaying entered information to the standard output
void Display(company employee[MAX], int n){
	cout << "Employee entered information:";
	cout << endl;
	cout << "Name	ID	Sex	Age	Date of Birth" << endl;
	for (int i=1; i<=n; i++){
		cout << employee[i].name		<< "\t";
		cout << employee[i].employee_id << "\t";
		cout << employee[i].sex; cout	<< "\t";
		cout << employee[i].age; cout	<< "\t";
		cout << employee[i].dob.day		<< ".";
		cout << employee[i].dob.month   << ".";
		cout << employee[i].dob.year    ;
		
		cout << endl;
	}
}


Output of the program

Output of the program
Output of the program

Discussion of the above program

At first, the graphical representation of the program is given below:

Graphical representation of the structure
Graphical representation of the structure

So, the code block will be:

struct date{
	// members of structure
        int day;
	int month;
	int year;
};
struct company{
	char name[20];
	long int employee_id;
	char sex[5];
	int age;
	struct date dob;
};
 

Look "struct date dob;" is used to link the nested structure.

To initialize the values in nested structue it uses like the followings:

employee[i].dob.day;
employee[i].dob.month;
employee[i].dob.year;

The program has two functions:

void CollectInformtion(company employee[MAX], int n);
void Display(company employee[MAX], int n);
 

Function "CollectInformtion()" is used to take inputs from the users.And function "Display()" is used to display the final output. 

Give your vote about this post

Is this tutorial "Nested Structues" helpful to make your basic strong?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      rahul 

      5 years ago

      ghant jevo chhe easy lakh

    • profile image

      sam 

      6 years ago

      cool program

    • profile image

      misbah 

      6 years ago

      thanks but can u expalin more deeply and easily

    • profile image

      kilo 

      6 years ago

      nested structure beautifully explained awesome Thanx!! :)

    • profile image

      Saad 

      7 years ago

      There is one problem in program.

      If the user enters Date of Birth, the program should itself give his/her age.

    • profile image

      nehai 

      7 years ago

      this program is of date. cool

    • profile image

      raj 

      8 years ago

      i want the program of date of birthday.

      means user give date and month and year. the program print the day.

    • profile image

      san 

      8 years ago

      hm.tats.....good

    • profile image

      kaye 

      8 years ago

      uhm hi! good day.

      i just want to ask..

      our programming starts with:

      #include

      #include

      is it possible that this will work ?

      my programming language is c++ also.

      but i'm using microsoft visual studio.

      well. that's it. :)

      thank you for your time reading this. :)

    • rancidTaste profile imageAUTHOR

      rancidTaste 

      8 years ago

      Thanks diya..for your comment. I solved that.

    • profile image

      diya 

      8 years ago

      there is a mistake in thid program.its not death of birth.its date of birth

    • profile image

      zampano 

      9 years ago

      Hi, man.

      I appreciate very much the art and aesthetics of your program. Quite nice use of C++.

      It's nice also to have a fancy user interface and store data in a database.

      keep on.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)