+ All Categories
Home > Documents > C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited...

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited...

Date post: 02-Jan-2016
Category:
Upload: kristin-berry
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
23
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning
Transcript
Page 1: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

C++ Programming: From Problem Analysisto Program Design, Fourth Edition

Topic 4 : Records

Edited from Powerpoint Slides

provided by Thomson Learning

Page 2: C++ Programming: From Problem Analysis to 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

Page 3: C++ Programming: From Problem Analysis to 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 3

Records (structs)

• struct: collection of a fixed number of components (members), accessed by name− Members may be of different types

• Syntax:

Page 4: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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");

}

Page 5: C++ Programming: From Problem Analysis to 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 5

Records (structs) (continued)

• A struct is a definition, not a declaration

Page 6: C++ Programming: From Problem Analysis to 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 6

Records (structs) (continued)

Page 7: C++ Programming: From Problem Analysis to 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 7

Accessing struct Members

• The syntax for accessing a struct member is:

• The dot (.) is an operator, called the member access operator

Page 8: C++ Programming: From Problem Analysis to 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 8

Accessing struct Members (continued)

• To initialize the members of newStudent:newStudent.GPA = 0.0;strcpy(newStudent.firstName,"John“);strcpy(newStudent.lastName,"Brown“);

Page 9: C++ Programming: From Problem Analysis to 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 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';

Page 10: C++ Programming: From Problem Analysis to 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 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

Page 11: C++ Programming: From Problem Analysis to 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 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;

Page 12: C++ Programming: From Problem Analysis to 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 12

Comparison (Relational Operators)

• Compare struct variables member-wise− No aggregate relational operations allowed

• To compare the values of student and newStudent:

Page 13: C++ Programming: From Problem Analysis to 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 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

Page 14: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 15: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 16: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 17: C++ Programming: From Problem Analysis to 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 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

Page 18: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 19: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 20: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 21: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 22: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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

Page 23: C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning.

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


Recommended