2. INDEX
Objective
Introduction to Structures
Declare structure variable
Assign and access values of structure variable
Array of structure
Write program using structures
3. OBJECTIVES
At the end of this module, students should:
Declare and use structure variable
Identify the difference between structures and array
Use array in structure
4. INTRODUCTION
Array can be used to store and process large
amount of similar data.
However, it is limited as does not allow to store
and manipulate dissimilar data items.
Structure can be used to store and manipulate
dissimilar data items.
5. INTRODUCTION TO STRUCTURE
Structure is a collection of related data items
stored in one place and referenced under one
name.
Each data item do not have to be the same type.
struct student
{
char id [5];
char name [18];
char gender[10];
int age;
};
7. STRUCTURE DECLARATION
Used struct keyword
The variable in a structure are called structure
elements or members
Syntax
struct <structure name>
{
<struct member>;
};
11. DECLARING STRUCTURE VARIABLE
First method
struct student
{
char id [ 5 ];
char name [ 18 ];
char gender[10];
int age;
} stud_1, stud_2; variable
12. DECLARING STRUCTURE VARIABLE
Second method
struct student
{
char id [ 5 ];
char name [ 18 ];
char gender[10];
int age;
}; variable
struct student stud_1, stud_2;
13. ASSIGN & ACCESS THE STRUCTURE
A structure element can be accessed and
assigned a value by using the structure variable
name, the dot operator and the element’s name.
For example;
stud_1.name = “Miriam”;
stud_1.age = 21;
17. #include<iostream>
using namespace std;
struct student
{
char id[10];
char name[25];
int age;
}stud1;
void main()
{
cout<<"enter your id:";
cin.getline(stud1.id,10);
cout<<"enter your name:";
cin.getline(stud1.name,25);
cout<<"enter your age:";
cin>>stud1.age;
cout<<"nDisplay result"<<endl;
cout<<"your id is:"<< stud1.id<<endl;
cout<<"your name is:"<< stud1.name<<endl;
cout<<"your age is:"<< stud1.age<<endl;
}
18. EXERCISE
Write a program to declare a structure named
parent with structure member name and age.
Create two structure variable named father and
mother . Assign these two variable with the value
name of your father and mother and their age.
Display all of these value
19. Write a program to declare a structure named
parent with structure member name and age.
#include<iostream.h>
struct parent{
char name[25];
int age;
};
20. Create two structure variable named father and
mother . Assign these two variable with the value
name of your father and mother and their age.
#include<iostream.h>
struct parent{
char name[25];
int age;
};
void main(){
struct parent father={"Ngadengon", 73};
struct parent mother={"Satirah", 66};
}
21. Display all of these value
#include<iostream.h>
struct parent{
char name[25];
int age;
};
void main(){
struct parent father={"Ngadengon", 73};
struct parent mother={"Satirah", 66};
cout<<"Father:"<<father.name<<" Age:"<<father.age;
cout<<"nMother:"<<mother.name<<" Age:"<<mother.age;
}
22.
23. ARRAY OF STRUCTURE
struct student
{
char id [5];
char name [18];
char gender[10];
int age;
};
struct student stud [2];
24. #include<iostream>
using namespace std;
struct student
{
char id[10];
char name[25];
int age;
}stud1[2];
void main()
{
for(int i=0;i<2;i++){
cout<<"nenter your id:";
cin>>stud1[i].id;
cout<<"enter your name:";
cin>>stud1[i].name;
cout<<"enter your age:";
cin>>stud1[i].age;
}
cout<<"nDisplay result"<<endl;
for(int x=0;x<2;x++){
cout<<"your id is:"<< stud1[x].id<<endl;
cout<<"your name is:"<< stud1[x].name<<endl;
cout<<"your age is:"<< stud1[x].age<<endl;
}
}
25. SUMMARY
struct is a reserved keyword
Members of struct can also be functions,
including constructors and destructors.
Member of struct by default are public.