Structure
Structure is represented by struct keyword.
Structure is a data type which can hold different type of data in a single unit.
Variable required :
Can hold whole student record.
OR
Book record
OR
Table record
OR
Laptop record
etc
Example:1
#include<iostream>
using namespace std;
int main()
{
struct Student
{
int roll;
char name[30];
int Total_marks;
}; // semicolon required like do while
struct Student s={101,"Geeta",78};
struct Student o={161,"Goeta",98};
//struct Student s={101,"Geeta",78};
co<< "Roll Number of the student is : "<<o.roll<<endl;
cout<< "Name of the student :"<
Output:
Roll Number of the student is : 161
Name of the student :Goeta
Total_marks of the student :98
--------------------------------
Process exited after 0.07178 seconds with return value 0
Press any key to continue . . .
Example:2
#include<iostream>
using namespace std;
int main<<
{
struct Student
{
int roll;
char name[30];
int Total_marks;
}; // semicolon required like do while
struct Student s;
cout<<"Enter the roll no :"<<endl;
cin>>s.roll;
cout<<"Enter the name :"<<endl;
cin>>s.name;
cout<<"Enter the total_marks"<<endl;
cin>>s.Total_marks;
cout<<"The Output is :"<<endl;
cout<< "Roll Number of the student is : "<<s.roll<<endl;
cout<< "Name of the student :"<<s.name<<endl;
cout<< "Total_marks of the student :"<<s.Total_marks<<endl;
}
Output:
Enter the roll no :
101
Enter the name :
Sachin
Enter the total_marks
498
The Output is :
Roll Number of the student is : 101
Name of the student :Sachin
Total_marks of the student :498
--------------------------------
Process exited after 14.17 seconds with return value 0
Press any key to continue . . .
Function Overriding
Example:3
#include<iostream>
using namespace std;
int main()
{
int num,i;
struct Student
{
int roll;
char name[30<<
int marks[6];
}; // semicolon required like do while
struct Student s[10];
cout<<"Enter the number of students"<<endl;
cin>num;
for(i=0;i
Output:
Enter the number of students
1
Enter the roll no :
101
Enter the name :
Sachin
Enter the Marks in Hindi
98
Enter the Marks in English
78
Enter the Marks in Math
67
Enter the Marks in Science
87
Enter the Marks in Social Science
98
Enter the Marks in Health Education
95
The Output is :
---------------------------------------------------------
Roll Number of the student is : 101
Name of the student :Sachin
the Marks in Hindi98
the Marks in English78
the Marks in Math67
the Marks in Science87
the Marks in Social Science98
the Marks in Health Education95
---------------------------------------------------------
--------------------------------
Process exited after 22.58 seconds with return value 0
Press any key to continue . . .
Example:4
#include<iostream>
using namespace std;
int main()
{
int num,i;
union Student
{
int roll;
char name[30];
int marks[6];
}; // semicolon required like do while
union Student s[10];
cout<<"Enter the number of students"<>num;
for(i=0;i<num;i++){
cout<<"Enter the roll no :"<<endl;
cin>>s[i].roll;
cout<<"Enter the name :"<<endl;
cin>>s[i].name;
cout<<"Enter the Marks in Hindi"<<endl;
cin>>s[i].marks[0];
cout<<"Enter the Marks in English"<<endl;
cin>>s[i].marks[1];
cout<<"Enter the Marks in Math"<<endl;
cin>>s[i].marks[2];
cout<<"Enter the Marks in Science"<<endl;
cin>>s[i].marks[3];
cout<<"Enter the Marks in Social Science"<<endl;
cin>>s[i].marks[4];
cout<<"Enter the Marks in Health Education"<<endl;
cin>>s[i].marks[5];
}
cout<<"The Output is :"<<endl;
for(i=0;i<num;i++){
cout<<"---------------------------------------------------------"<<endl;
cout<< "Roll Number of the student is : "<<s[i].roll<<endl;
cout<< "Name of the student :"<<s[i].name<<endl;
cout<<"the Marks in Hindi";
cout<<s[i].marks[0]<
Output:
Enter the number of students
1
Enter the roll no :
101
Enter the name :
Geeta
Enter the Marks in Hindi
98
Enter the Marks in English
67
Enter the Marks in Math
98
Enter the Marks in Science
89
Enter the Marks in Social Science
100
Enter the Marks in Health Education
97
The Output is :
---------------------------------------------------------
Roll Number of the student is : 98
Name of the student :b
the Marks in Hindi98
the Marks in English67
the Marks in Math98
the Marks in Science89
the Marks in Social Science100
the Marks in Health Education97
---------------------------------------------------------
--------------------------------
Process exited after 20.32 seconds with return value 0
Press any key to continue . . .