View
366
Download
4
Category
Tags:
Preview:
Citation preview
F2037 - PROGRAMMING FUNDAMENTAL WITH C++Unit 4.2 - Understand the use of structures
Objective Introduction to Structures Declare structure variable Assign and access values of structure
variable Array of structure Write program using structures
INDEX
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
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.
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;
};
INTRODUCTION TO STRUCTURE
Id
Name
Gender
age
STRUCTURE DECLARATION
Used struct keyword The variable in a structure are called
structure elements or members Syntax
struct <structure name>
{
<struct member>;
};
STRUCTURE DECLARATION
EXERCISE
Create a struct named car with structure member type and year
struct car{
char type[10];int year;
};
DECLARING STRUCTURE VARIABLE
First method
struct student
{
char id [ 5 ];
char name [ 18 ];
char gender[10];
int age;
} stud_1, stud_2; variable
DECLARING STRUCTURE VARIABLE
Second method
struct student
{
char id [ 5 ];
char name [ 18 ];
char gender[10];
int age;
};
struct student stud_1, stud_2;
variable
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;
EXAMPLE#include<iostream>
using namespace std;
struct student
{
char id[10];
char name[25];
int age;
};
void main()
{
struct student stud1 = {"123","Ani",12};
cout<<"Student id: "<<stud1.id<<endl;
cout<<"Student name: "<<stud1.name<<endl;
cout<<"Student age: "<<stud1.age<<endl;
}
#include<iostream>
using namespace std;
struct student
{
char id[10];
char name[25];
int age;
}stud1={"123","Ani",12};
void main()
{
cout<<"Student id: "<<stud1.id<<endl;
cout<<"Student name: "<<stud1.name<<endl;
cout<<"Student age: "<<stud1.age<<endl;
}
#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;
}
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
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;
};
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};
}
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;
}
ARRAY OF STRUCTURE
struct student
{
char id [5];
char name [18];
char gender[10];
int age;
};
struct student stud [2];
#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;
}
}
SUMMARY
struct is a reserved keyword Members of struct can also be functions,
including constructors and destructors. Member of struct by default are public.
Recommended