Nested Structures : Structures within Structures
By rancidTaste
:: Big Index :: C/C++ Programming Tutorials ::
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
Discussion of the above program
At first, the graphical representation of the program is given below:
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 without votingMore about "Structures"
- Write a C/C++ program for displaying today's date by...
Programming Tutorials : C/C++ Programming It's a simple program to start learning structure in C/C++. This program will display today's date. Suppose today's date is 30.10.2008. So, the output of the program... - Structures and arrays
Programming Tutorials : C/C++ Programming Structure and array can be implemented together. An array is a group of identical data items. All array elements are sorted consecutive memory location under a... - Structure Basics in Programming :: C++ Programming S...
Programming Tutorials : C/C++ Programming Collection of heterogeneous data types are known as Structue. Structue is one of the data types of C/C++ programming languages. Several data types are grouped... - Structues and Functions
Programming Tutorials : C/C++ Programming Function decomposes any complex program into several manageable modules. Each module is referred as function. Functions are compiled separately. So, they can be...
My recent activities
- Drawing a line: How to draw a line using HTML 5?
HTML5 canvas API provides basics tools and great supports to draw and style different types of sub paths including lines, arcs, Quadratic curves, and Bezier curves, as well as a means for creating paths by connecting sub paths. - 5 days ago
- SEO Pages
SEO, Search Engine Optimization and Keywords - these common three terms are the brainstorming terms for any content publisher, seo company, seo service provider, seo researcher, seo firm, seo consultant, seo professional or other seo lover. - 3 months ago
- WordPress.org vs. WordPress.com: An explained comparison between WordPress.org an WordPress.com
WordPress is a common name but do you know that there are two things: WordPress. - 3 months ago
![]() | Amazon Price: $17.13 List Price: $29.99 |
![]() | Amazon Price: $18.36 List Price: $34.99 |
![]() | Amazon Price: $34.99 |
![]() | Amazon Price: $2.99 |
Comments
there is a mistake in thid program.its not death of birth.its date of birth
Thanks diya..for your comment. I solved that.
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. :)
hm.tats.....good
i want the program of date of birthday.
means user give date and month and year. the program print the day.
this program is of date. cool
There is one problem in program.
If the user enters Date of Birth, the program should itself give his/her age.
nested structure beautifully explained awesome Thanx!! :)
thanks but can u expalin more deeply and easily




zampano 2 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.