Professional Documents
Culture Documents
12 Structures
12 Structures
Structures
DEFINITION OF A STRUCTURE
struct structure_name
{
type member_name1;
type member_name2;
type member_name3;
.
.
.
type member_nameN;
};
Structures
DEFINITION OF A STRUCTURE
• Example:
struct person_info
{
int height_cm;
float weight_kg;
};
Structures
STRUCTURED VARIABLES
person2 Example:
Structures
ACCESSING struct MEMBERS
person1.height_cm = 166;
person2 person1.weight _kg= 72.7;
person2.height_cm = 160;
160 54.5 person2.weight_kg = 54.5;
height_cm weight_kg
• The dot (.) is an operator called the member
access operator.
Structures
ACCESSING struct MEMBERS
• Sample Program
#include <iostream>
using namespace std;
main()
{
struct person_info
{
int height_cm;
float weight_kg;
};
Structures
ACCESSING struct MEMBERS
cout << "\nPerson 1's height in cm is " << person1.height_cm << ".";
cout << "\nPerson 1's weight in kg is " << person1.weight_kg << ".";
cout << "\n\nPerson 2's height in cm is " << person2.height_cm << ".";
cout << "\nPerson 2's weight in kg is " << person2.weight_kg << ".";
}
Structures
ACCESSING struct MEMBERS
• Another example:
struct book_info
{
char title[50];
char author[50];
int book_id;
};
Structures
ACCESSING struct MEMBERS
strcpy (book1.title,
book1.title "C++ Programming");
strcpy (book1.author,
book1.author "Mitch M. Andaya");
book1.book_id = 123456;
Structures
ACCESSING struct MEMBERS
• Sample Program
#include <iostream>
using namespace std;
main()
{
struct book_info
{
char title[50];
char author[50];
int book_id;
};
Structures
ACCESSING struct MEMBERS
Structures
ACCESSING struct MEMBERS
cout << "\nThe title of the first book is " << book1.title << ".";
cout << "\nThe author of the first book is " << book1.author << ".";
cout << "\nThe book ID of the first book is " << book1.book_id << ".";
cout << "\nThe title of the second book is " << book2.title << ".";
cout << "\nThe author of the second book is " << book2.author << ".";
cout << "\nThe book ID of the second book is " << book2.book_id << ".";
Structures
OPTIONS IN DECLARING STRUCTURED VARIABLES
Structures
THE ASSIGNMENT OPERATOR & STRUCTURE VARIABLES
Example:
struct sample
{
int field1;
char field2;
float field3;
} var1, var2;
Structures
THE ASSIGNMENT OPERATOR & STRUCTURE VARIABLES
var2 = var1;
Example:
struct book_info
{
char title[50];
char author[50];
int book_id;
};
book_info books[10];
Structures
ARRAY OF STRUCTURES
Structures
ARRAY OF STRUCTURES
• Sample Program
#include <iostream>
using namespace std;
main()
{
struct book_info
{
char title[50];
char author[50];
int book_id;
} books[10];
int index;
Structures
ARRAY OF STRUCTURES
Structures
ARRAY OF STRUCTURES
Structures
FUNCTIONS AND STRUCTURE VARIABLES
Example:
struct sample
{
int field1;
char field2;
float field3;
} var1;
var1.field1 = 100;
var1.field2 = 'A';
var1.field3 = 10.5;
Structures
FUNCTIONS AND STRUCTURE VARIABLES
func1 (var1.field1);
This function call passes the value of field1 of var1 (which is 100) to
function func1.
func2 (var1.field2);
This function call passes the value of field2 of var1 (which is 'A') to function
func2.
func3 (var1.field3);
This function call passes the value of field3 of var1 (which is 10.5) to
function func3.
Take note that the functions are not "aware" that the values passed on to them
came from structured variables. All they see are the values. For all they know,
these values may have come from ordinary variables.
Structures
FUNCTIONS AND STRUCTURE VARIABLES
• Sample Program
#include <iostream>
using namespace std;
answer = x * 2;
return answer;
}
Structures
FUNCTIONS AND STRUCTURE VARIABLES
answer = tolower(x);
return answer;
}
answer = x / 2;
return answer;
}
Structures
FUNCTIONS AND STRUCTURE VARIABLES
main()
{
struct sample
{
int field1;
char field2;
float field3;
} var1;
var1.field1 = 100;
var1.field2 = 'A';
var1.field3 = 10.5;
Structures
FUNCTIONS AND STRUCTURE VARIABLES
var1.field1 = multiply2(var1.field1);
var1.field2 = lower(var1.field2);
var1.field3 = divide2(var1.field3);
• An entire structure variable (all of its members) can also be passed to a function
using the standard call-by-value method.
Example:
struct sample
{
int field1;
char field2;
float field3;
} var1;
func (var1);
This function call passes the values of all the members or fields of var1
to function func.
• The important thing to remember is that the type of the argument must match
the type of the parameter of the function.
Structures
FUNCTIONS AND STRUCTURE VARIABLES
• Sample Program
#include <iostream>
using namespace std;
struct sample
{
int field1;
char field2;
float field3;
};
Structures
FUNCTIONS AND STRUCTURE VARIABLES
sample
sample process (sample
sample x)
x
{
x.field1 = x.field1 * 2;
x.field2 = tolower(x.field2);
x.field3 = x.field3 / 2;
return x;
}
Structures
FUNCTIONS AND STRUCTURE VARIABLES
main()
{
sample var1;
var1.field1 = 100;
var1.field2 = 'A';
var1.field3 = 10.5;
var
var1 = process (var1);
1
cout << "\nField 1 is " << var1.field1;
cout << "\nField 2 is " << var1.field2;
cout << "\nField 3 is " << var1.field3;
}
Structures
POINTERS AND STRUCTURE VARIABLES
book_info *p;
p = &book1;
Structures
POINTERS AND STRUCTURE VARIABLES
Structures
POINTERS AND STRUCTURE VARIABLES
• Sample Program
#include <iostream>
using namespace std;
main()
{
struct book_info
{
char title[50];
char author[50];
int book_id;
} book1;
book_info *p;
Structures
POINTERS AND STRUCTURE VARIABLES
p = &book1;
cout << "\nThe title of the book is " << (*p).title << ".";
cout << "\nThe author of the book is " << (*p).author << ".";
cout << "\nThe book ID of the book is " << (*p).book_id << ".";
}
Structures
THE ARROW (->) OPERATOR
• The arrow operator can take the place of the dot operator when
accessing a structure element.
Example:
(*p).book_id = 12345;
Structures
THE ARROW (->) OPERATOR
• Sample Program
#include <iostream>
using namespace std;
main()
{
struct book_info
{
char title[50];
char author[50];
int book_id;
} book1;
book_info *p;
Structures
THE ARROW (->) OPERATOR
p = &book1;
cout << "\nThe title of the book is " << p->title << ".";
cout << "\nThe author of the book is " << p->author << ".";
cout << "\nThe book ID of the book is " << p->book_id << ".";
}
Structures
STRUCTURE VARIABLES AND CALL-BY-REFERENCE
#include <iostream>
using namespace std;
struct book_info
{
char title[50];
char author[50];
int book_id;
};
Structures
STRUCTURE VARIABLES AND CALL-BY-REFERENCE
void
void getdata (book_info
book_info *bk)
*bk From main():
{
cout << "Enter title of book: "; getdata (&book1);
cin.get (bk
bk -> title,
title 50);
cin.get();
cout << "Enter author of book: ";
bk -> author,
cin.get (bk author 50);
cout << "Enter book ID of book: " ;
cin >> bk
bk ->
-> book_id;
book_id;
}
Structures
STRUCTURE VARIABLES AND CALL-BY-REFERENCE
main()
{
book_info book1;
getdata (&book1);
&book1
book1
printdata (book1);
}
Structures
STRUCTURES WITH POINTER FIELDS
Example:
struct sample
{
int number;
int *ptr;
} var1;
Structures
STRUCTURES WITH POINTER FIELDS
var1.ptr = &x;
*var1.ptr = 5;
Structures
struct WITHIN struct
struct person_info
{
char first_name[25]; int age;
char middle_name[25]; int height_cm;
char last_name [25] float weight_kg;
char no_street[25]; char spouse_fname[25];
char barangay[25]; char spouse_mname[25];
char city [25] char spouse_lname [25]
};
Structures
struct WITHIN struct
Structures
struct WITHIN struct
struct person_info
{
name_type name;
address_type address;
int age;
int height_cm;
float weight_kg;
name_type spouse_name;
};
Structures
struct WITHIN struct
Structures
struct WITHIN struct
Structures
MACHINE PROBLEMS
Structures
MACHINE PROBLEMS
Structures
MACHINE PROBLEMS
Structures
MACHINE PROBLEMS
2. Repeat Machine Problem 1 but this time, use pointers instead of array indexing.
Also, the program should assign the grade based on the score entered by the
user using the following table:
Score Grade
0–69 5.00
70–73 3.00
74–77 2.75
78–80 2.50
81–83 2.25
84–87 2.00
88–90 1.75
91–93 1.50
94–97 1.25
98–100 1.00
Structures
MACHINE PROBLEMS
Structures
MACHINE PROBLEMS
3. Write a program to help a local restaurant automate its breakfast billing system.
The program should do the following:
a. Show the customer the different breakfast items offered by the
restaurant.
b. Allow the customer to select more than one item from the menu.
c. Calculate and print the bill.
Assume that the restaurant offers the following breakfast items (the price
of each item is shown to the right of the item):
Continental Breakfast 349.99 pesos
Filipino Breakfast 285.00 pesos
Fruit Platter 172.50 pesos
French Toast 95.25 pesos
Pancakes 65.00 pesos
Use an array, menu, of the struct menu_type, with two fields (food which is a
string with a maximum of 20 characters and price which is of type float).
Structures
MACHINE PROBLEMS
After entering the data in the array, the program will prompt the user to make
choices from the menu.
6 Finish
Structures
MACHINE PROBLEMS
The program should not accept an invalid choice (any choice that is not 1, 2, 3,
4, 5, or 6). The user should be able to choose several items from the menu
(even the same item). For example:
Structures
MACHINE PROBLEMS
Structures