You are on page 1of 8

Object Oriented Programming

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*/
};

Notice that the declaration is terminated by a semicolon. This is because a structure


declaration is a statement. The type name of the structure is employee. As such,
employee identifies this particular data structure and is its type specifier.
At this point, no variable has actually been created. Only the form of the data has
been defined. When you define a structure, you are defining a compound variable
type, not a variable. Not until you declare a variable of that type does one actually
exist. In C, to declare a variable (i.e., a physical object) of type employee, write
struct employee emp;

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)

The general form of a structure declaration is


struct struct-type-name {
type member-name;
type member-name;
type member-name;
.. .
} structure-variables;
where either struct-type-name or structure-variables may be omitted, but not both.

Accessing Structure Members


Individual members of a structure are accessed through the use of the . operator
(usually called the dot operator). For example, the following code assigns 35 to the
age field of the structure variable employee declared earlier:
emp.age = 35;
The structure variable name followed by a period and the member name references
that individual member. The general form for accessing a member of a structure is
structure-name.member-name
Therefore, to print the age on the screen, write
printf("%d", emp.age);
This prints the 35 contained in the age member of the structure variable employee.
In the same fashion, the character array emp.name can be used to call gets() , as
shown here:
gets(emp.name);
This passes a character pointer to the start of name. Since name is a character array,
you can access the individual characters of emp.name by indexing name. For
example, you can print the contents of emp.name one character at a time by using the
following code:
register int t;
for(t=0; emp.name[t]; ++t)
3
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.

Examples 1: C++ program to declare structure student (student name, 7 degrees


for one subject and average) and input the information of 30 student and find the
average of each student then print the information of all students with results (pass
or fail).

#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;

void print(book temp)


{
puts(temp.title);
puts(temp.authorname);
cout<<temp.year<<endl;

}
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;

void print(book temp)


{
puts(temp.title);
puts(temp.authorname);
cout<<temp.year<<endl;

}
main()
{
book B[1000];

for (int i=0;i<1000;i++)


input(B[i]);

for (int i=0;i<1000;i++)


print(B[i]);
}

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++;

cout<<"The number of pass students in stage1 = "<<num1<<endl;


cout<<"The number of pass students in stage2 = "<<num2<<endl;
cout<<"The number of pass students in stage3 = "<<num3<<endl;
cout<<"The number of pass students in stage4 = "<<num4<<endl;

You might also like