Professional Documents
Culture Documents
Lecture (2)
Structures
A structure is a collection of variables referenced under one name, providing a
convenient means of keeping related information together. A structure declaration
forms a template that may be used to create structure objects (that is, instances of a
structure). The variables that make up the structure are called members. (Structure
members are also commonly referred to as elements or fields.)
Generally, all of the members of a structure are logically related. For example, the
name and address information in a mailing list would normally be represented in a
structure. The following code fragment shows how to declare a structure that defines
the name and address fields of employee. The keyword struct tells the compiler that
a structure is being declared.
Struct employee
{
char name[50];
char address[50];
int age;
float salary;
char ms; /*marital status*/
};
1
Object Oriented Programming
Lecture (2)
This declares a variable of type employee called emp. In C++, you may use this
shorter form.
employee emp;
As you can see, the keyword struct is not needed. In C++, once a structure has been
declared, you may declare variables of its type using only its type name, without
preceding it with the keyword struct. The reason for this difference is that in C, a
structure's name does not define a complete type name. In fact, Standard C refers to a
structure's name as a tag. In C, you must precede the tag with the keyword struct
when declaring variables. However, in C++, a structure's name is a complete
type name and may be used by itself to define variables. Keep in mind, however, that
it is still perfectly legal to use the C-style declaration in a C++ program. Since the
programs in this Part are valid for both C and C++, they will use the C declaration
method. Just remember that C++ allows the shorter form.
When a structure variable (such as emp) is declared, the compiler automatically
allocates sufficient memory to accommodate all of its members.
You may also declare one or more structure variables when you declare a structure.
For example,
Struct employee
{
char name[50];
char address[50];
int age;
float salary;
char ms;
}emp1,emp2,emp3;
defines a structure type called employee and declares variables emp1, emp2, and
emp3 of that type.
2
Object Oriented Programming
Lecture (2)
putchar(emp.name[t]);
Structure Assignments
The information contained in one structure may be assigned to another structure of the
same type using a single assignment statement. That is, you do not need to assign the
value of each member separately. The following program illustrates structure
assignments:
#include <stdio.h>
int main()
{
struct {
int a;
int b;
} x, y;
x.a = 10;
y = x; /* assign one structure to another */
printf("%d", y.a);
return 0;
}
After the assignment, y.a will contain the value 10.
Arrays of Structures
Perhaps the most common usage of structures is in arrays of structures. To declare an
array of structures, you must first define a structure and then declare an array variable
of that type. For example, to declare a 100-element array of structures of type
employee, defined earlier, write
struct employee emp[100];
This creates 100 sets of variables that are organized as defined in the structure
employee. To access a specific structure, index the structure name. For example, to
print the age of structure 3, write
4
Object Oriented Programming
Lecture (2)
printf("%d", emp[2].age);
Like all array variables, arrays of structures begin indexing at 0.
#include<iostream>
#include<stdio.h>
using namespace std;
struct student
{
char name[20];
float degree[7];
float av;
};
main()
{
student a[30];
int i,j;
float sum;
for(i=0;i<30;i++)
{
cout<<"Enter the name of student"<<endl;
gets(a[i].name);
sum=0;
cout<<"Enter the 7 degrees of student"<<endl;
for(j=0;j<7;j++)
{
cin>>a[i].degree[j];
sum+=a[i].degree[j];
}
a[i].av=sum/7;
}
for(i=0;i<30;i++)
{
cout<<a[i].name<<"\t"<<a[i].av<<"\t";
if(a[i].av>=50)
cout<<"pass"<<endl;
else
cout<<"fail"<<endl;
}
}
5
Object Oriented Programming
Lecture (2)
Examples 2: C++ program to declare structure book (book title, book author and
year of publication ) and include function input to input the information of one
book and print function to print the information of one book. In main function
create one book and input and print the its information using input and print
functions.
#include<iostream>
#include<stdio.h>
using namespace std;
struct book
{
char title[20];
char authorname[20];
int year;
};
void input (book &temp)
{
gets(temp.title);
gets(temp.authorname);
cin>>temp.year;
}
main()
{
book B;
input(B);
print(B);
}
6
Object Oriented Programming
Lecture (2)
Examples 3: C++ program to declare structure book (book title, book author and
year of publication ) and include function input to input the information of one
book and print function to print the information of one book. In main function
create array of 1000 book then input and print the information of all books.
#include<iostream>
#include<stdio.h>
using namespace std;
struct book
{
char title[20];
char authorname[20];
int year;
};
void input (book &temp)
{
gets(temp.title);
gets(temp.authorname);
cin>>temp.year;
}
main()
{
book B[1000];
7
Object Oriented Programming
Lecture (2)
Examples 4: C++ program to declare structure student (student name, stage and
average) and input the information of 200 students and find the number of pass
students in each stage.
#include<iostream>
#include<stdio.h>
using namespace std;
struct student
{
char name[200];
int stage;
float av;
};
main()
{
student a[200];
int i,num1=0,num2=0,num3=0,num4=0;
for(i=0;i<200;i++)
{
gets(a[i].name);
cin>>a[i].stage;
cin>>a[i].av;
}
for(i=0;i<200;i++)
if(a[i].stage==1 && a[i].av>=50)
num1++;
else
if(a[i].stage==2 && a[i].av>=50)
num2++;
else
if(a[i].stage==3 && a[i].av>=50)
num3++;
else
if(a[i].stage==4 && a[i].av>=50)
num4++;