C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Code Snippets C/C++ Code Snippets

C++ - program for Nested Structure (Structure with in Structure).

By: IncludeHelp, On 29 SEP 2016






In this program we will learn how to declare, read and access Nested Structure, Structure within Structure?

In this program we will declare two structure student and date_of_birth. date_of_birth will be accessed inside the student structure.

C++ program - Demonstrate Example of Nested Structure

/*C++ - program for Nested Structure 
(Structure with in Structure).*/

#include <iostream>
using namespace std;

struct date_of_birth{
	int dd,mm,yy;
};

struct student{
	char name[30];
	int rollNumber;
	date_of_birth dob;
};

int main(){
	student s;
	cout<<"Enter name : ";
	cin.getline(s.name,25);
	cout<<"Enter roll number : ";
	cin>>s.rollNumber;
	cout<<"Enter date of birth (dd mm yy) : "  ;
	cin>>s.dob.dd>>s.dob.mm>>s.dob.yy;
	
	cout<<"Name:"<<s.name<<",Roll Number:"<<s.rollNumber<<endl;
	cout<<"Date of birth:"<<s.dob.dd<<"/"<<s.dob.mm<<"/"<<s.dob.yy<<endl;
	
	return 0;
}
    Enter name : Mike 
    Enter roll number : 101 
    Enter date of birth (dd mm yy) : 29 09 2000 
    Name:Mike,Roll Number:101 
    Date of birth:29/9/2000 

In this program we are reading Name, Roll number and Date of birth of a student, here Name and Roll Number are declared in student structure, while date_of_birth is another structure which contains dd,mm,yy variables to read date of birth. And we used date_of_birth structure in student structure.