25
F2037 - PROGRAMMING FUNDAMENTAL WITH C++ Unit 4.2 - Understand the use of structures

FP 201 - Unit4 Part 2

Embed Size (px)

Citation preview

Page 1: FP 201 - Unit4 Part 2

F2037 - PROGRAMMING FUNDAMENTAL WITH C++Unit 4.2 - Understand the use of structures

Page 2: FP 201 - Unit4 Part 2

Objective Introduction to Structures Declare structure variable Assign and access values of structure

variable Array of structure Write program using structures

INDEX

Page 3: FP 201 - Unit4 Part 2

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

Page 4: FP 201 - Unit4 Part 2

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.

Page 5: FP 201 - Unit4 Part 2

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;

};

Page 6: FP 201 - Unit4 Part 2

INTRODUCTION TO STRUCTURE

Id

Name

Gender

age

Page 7: FP 201 - Unit4 Part 2

STRUCTURE DECLARATION

Used struct keyword The variable in a structure are called

structure elements or members Syntax

struct <structure name>

{

<struct member>;

};

Page 8: FP 201 - Unit4 Part 2

STRUCTURE DECLARATION

Page 9: FP 201 - Unit4 Part 2

EXERCISE

Create a struct named car with structure member type and year

Page 10: FP 201 - Unit4 Part 2

struct car{

char type[10];int year;

};

Page 11: FP 201 - Unit4 Part 2

DECLARING STRUCTURE VARIABLE

First method

struct student

{

char id [ 5 ];

char name [ 18 ];

char gender[10];

int age;

} stud_1, stud_2; variable

Page 12: FP 201 - Unit4 Part 2

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

Page 13: FP 201 - Unit4 Part 2

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;

Page 14: FP 201 - Unit4 Part 2

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;

}

Page 15: FP 201 - Unit4 Part 2

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

}

Page 16: FP 201 - Unit4 Part 2
Page 17: FP 201 - Unit4 Part 2

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

}

Page 18: FP 201 - Unit4 Part 2

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

Page 19: FP 201 - Unit4 Part 2

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;

};

Page 20: FP 201 - Unit4 Part 2

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

}

Page 21: FP 201 - Unit4 Part 2

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;

}

Page 22: FP 201 - Unit4 Part 2
Page 23: FP 201 - Unit4 Part 2

ARRAY OF STRUCTURE

struct student

{

char id [5];

char name [18];

char gender[10];

int age;

};

struct student stud [2];

Page 24: FP 201 - Unit4 Part 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;

}

}

Page 25: FP 201 - Unit4 Part 2

SUMMARY

struct is a reserved keyword Members of struct can also be functions,

including constructors and destructors. Member of struct by default are public.