Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | kristin-berry |
View: | 217 times |
Download: | 0 times |
C++ Programming: From Problem Analysisto Program Design, Fourth Edition
Topic 4 : Records
Edited from Powerpoint Slides
provided by Thomson Learning
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2
Objectives
In this chapter, you will:• Learn about records (structs)• Examine various operations on a struct• Explore ways to manipulate data using a struct
• Learn about the relationship between a struct and functions
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3
Records (structs)
• struct: collection of a fixed number of components (members), accessed by name− Members may be of different types
• Syntax:
Programming Example
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4
struct studentType
{
char firstName[20];
char lastName[20];
char courseGrade;
int testScore;
int programmingScore;
double GPA;
};
void main( )
{
studentType newStudent;
studentType student;
newStudent.GPA = 0.0;
strcpy(newStudent.firstName,"John");
strcpy(newStudent.lastName,"Brown");
}
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5
Records (structs) (continued)
• A struct is a definition, not a declaration
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6
Records (structs) (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7
Accessing struct Members
• The syntax for accessing a struct member is:
• The dot (.) is an operator, called the member access operator
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8
Accessing struct Members (continued)
• To initialize the members of newStudent:newStudent.GPA = 0.0;strcpy(newStudent.firstName,"John“);strcpy(newStudent.lastName,"Brown“);
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9
Accessing struct Members (continued)
• More examples:cin.getline(newStudent.firstName,20);cin >> newStudent.testScore >> newStudent.programmingScore;
score = (newStudent.testScore + newStudent.programmingScore) / 2;
if (score >= 90)newStudent.courseGrade = 'A';
else if (score >= 80)newStudent.courseGrade = 'B';
else if (score >= 70)newStudent.courseGrade = 'C';
else if (score >= 60)newStudent.courseGrade = 'D';
elsenewStudent.courseGrade = 'F';
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10
Assignment
• Value of one struct variable can be assigned to another struct variable of the same type using an assignment statement
• The statement:
student = newStudent;
copies the contents of newStudent into student
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11
Assignment (continued)
• The assignment statement:
student = newStudent;
is equivalent to the following statements:
student.firstName = newStudent.firstName;student.lastName = newStudent.lastName;student.courseGrade = newStudent.courseGrade;student.testScore = newStudent.testScore;student.programmingScore =
newStudent.programmingScore;
student.GPA = newStudent.GPA;
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 12
Comparison (Relational Operators)
• Compare struct variables member-wise− No aggregate relational operations allowed
• To compare the values of student and newStudent:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13
Input/Output
• No aggregate input/output operations on a struct variable
• Data in a struct variable must be read one member at a time
• The contents of a struct variable must be written one member at a time
Exercise 11. Define a struct workerType that have 5 variables or members. It is
name, staffID, age, salary and depatmentName.
2. Write a main program that will:
a) declare two struct variable which is worker1 and worker2.
b) input a details for worker1.
c) assign the given value for worker2.
d) display name and staff id of the worker that have more salary
e) display the details for worker1.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14
Variables Value
name Rohaya Alias
staffID 3451
age 32
salary 4500
departmentName Finance
Exercise 2
1)Write a struct definition called studentRec that have 7 variables/data member. The data member for this struct are name, matric number, quiz mark, test mark, final mark and total mark.
2)Write a main program that will:
a) Declare one struct variable called student.
b) Input name, matric number, quiz mark, test mark and final mark.
c) Calculate the total mark by using the given formula:
Total mark= 20% of quiz mark + 20% of test mark + 60% of final mark
15
Exercise (cont.)
d) Display the details of the student as below:
*********************************
Name : XXXXXX
Matric Number:XXXXXX
Total Mark :XXXXXXX
*********************************
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 16
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17
struct Variables and Functions
• A struct variable can be passed as a parameter by value or by reference
• A function can return a value of type struct
Programing Example (struct involve function)
struct studentType{ char firstName[20]; char lastName[20]; char courseGrade; int testScore; int programmingScore; double GPA; };void inputData(studentType &s);
void main(){ studentType newStudent; newStudent.GPA = 0.0; strcpy(newStudent.firstName,"John"); strcpy(newStudent.lastName,"Brown"); inputData(newStudent);
cout<<"\nCourse Grade for newStudent:“ <<newStudent.courseGrade; cout<<"\nfirstName for newStudent:"<<newStudent.firstName;}
void inputData(studentType &s){ cout<<"Course Grade:"; cin>>s.courseGrade; cout<<"\nProgramming Score:"; cin>>s.programmingScore;}
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 18
Exercise 1
1. Define a struct workerType that have 5 variables or members. It is name, staffID, age, salary and depatmentName.
2. Write a main program that will:
a) declare two struct variable which is worker1 and worker2.
b) call all the functions
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 19
Exercise 1(continue)
3. Write 4 functions that will:
a) input a details for worker1 and return the struct variable by using parameter by reference .
b) assign the given value for worker2 and return the struct variable by using parameter by
reference .
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 20
Variables Value
name Rohaya Alias
staffID 18225
age 32
salary 4500
departmentName Finance
Exercise 1(continue)
c) display name of the worker that have more salary
-this function will receive two struct variable
d) display the details for worker1.
-this function will receive one struct variable
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 21
Exercise2(function)Write a C++ program to help a local restaurant owner to automate his/her breakfast
billing system. The program should do the following tasks:
• Show the customer the different breakfast items offered by the restaurant.• Allow the customer to select one item and the amount of item purchased from
the menu.• Calculate and print the bill.
Assume that the restaurant offers the following breakfast items:
22
Items Prices/Unit
Plain egg RM1.50
Muffin RM2.00
French toast RM2.50
Fruit Basket RM3.00
Exercise3(function)..Continued..Your program must contain at least the following functions:
• Function showMenu: this function shows the different items offered by the
restaurant and tells the user how to select the items.• Function CalculateTotal: this function calculates the total price to be paid by the
customer. The total price should include a 5% tax.• Function printCheck: this function prints the check.
Your program should use a structure to store information about the customer’s purchase: menu item, item price, the amount of item, tax value and the total amount due.
A sample output is as follows:
Thanks for dining at Jonny’s Place
Plain egg 2 RM3.00
Tax RM0.15
Amount Due RM3.15
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 23