F2037 - PROGRAMMINGFUNDAMENTAL WITH C++Unit 4.2 - Understand the use of structures
INDEX Objective Introduction to Structures Declare structure variable Assign and access values of structure variable ...
OBJECTIVES   At the end of this module, students should:     Declare  and use structure variable     Identify the diffe...
INTRODUCTION Array can be used to store and process large  amount of similar data. However, it is limited as does not al...
INTRODUCTION TO STRUCTURE Structure is a collection of related data items  stored in one place and referenced under one  ...
INTRODUCTION TO STRUCTUREIdNameGenderage
STRUCTURE DECLARATION Used struct keyword The variable in a structure are called structure  elements or members Syntax ...
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...
DECLARING STRUCTURE VARIABLE   Second method                     struct student                       {                  ...
ASSIGN & ACCESS THE STRUCTURE   A structure element can be accessed and    assigned a value by using the structure variab...
EXAMPLE#include<iostream>using namespace std;struct student{     char id[10];     char name[25];     int age;};void main()...
#include<iostream>using namespace std;struct student{    char id[10];    char name[25];    int age;}stud1={"123","Ani",12}...
#include<iostream>using namespace std;struct student{    char id[10];    char name[25];    int age;}stud1;void main(){    ...
EXERCISE Write a program to declare a structure named  parent with structure member name and age. Create two structure v...
   Write a program to declare a structure named    parent with structure member name and age.    #include<iostream.h>    ...
   Create two structure variable named father and    mother . Assign these two variable with the value    name of your fa...
 Display all of these value#include<iostream.h>struct parent{       char name[25];       int age;  };  void main(){  stru...
ARRAY OF STRUCTURE struct student   {   char id [5];   char name [18];   char gender[10];   int age;   }; struct student s...
#include<iostream>using namespace std;struct student{    char id[10];    char name[25];    int age;}stud1[2];void main(){ ...
SUMMARY struct is a reserved keyword Members of struct can also be functions,  including constructors and destructors. ...
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
Upcoming SlideShare
Loading in …5
×

FP 201 - Unit4 Part 2

677 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
677
On SlideShare
0
From Embeds
0
Number of Embeds
76
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

FP 201 - Unit4 Part 2

  1. 1. F2037 - PROGRAMMINGFUNDAMENTAL WITH C++Unit 4.2 - Understand the use of structures
  2. 2. INDEX Objective Introduction to Structures Declare structure variable Assign and access values of structure variable Array of structure Write program using structures
  3. 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. 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. 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; };
  6. 6. INTRODUCTION TO STRUCTUREIdNameGenderage
  7. 7. STRUCTURE DECLARATION Used struct keyword The variable in a structure are called structure elements or members Syntax struct <structure name> { <struct member>; };
  8. 8. STRUCTURE DECLARATION
  9. 9. EXERCISE Create a struct named car with structure member type and year
  10. 10. struct car{ char type[10]; int year;};
  11. 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. 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. 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;
  14. 14. 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;}
  15. 15. #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;}
  16. 16. #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;}
  17. 17. 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
  18. 18.  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; };
  19. 19.  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};}
  20. 20.  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;}
  21. 21. ARRAY OF STRUCTURE struct student { char id [5]; char name [18]; char gender[10]; int age; }; struct student stud [2];
  22. 22. #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; }}
  23. 23. SUMMARY struct is a reserved keyword Members of struct can also be functions, including constructors and destructors. Member of struct by default are public.

×