Download presentation
Presentation is loading. Please wait.
Published byAbigayle Garrett Modified over 6 years ago
1
Auburn University http://www.eng.auburn.edu/~xqin
COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn University Some slides are adapted from notes by Dr. Walter Savitch (UCSD)
2
Structure Assignments
Given structure named CropYield Declare two structure variables: CropYield apples, oranges; Both are variables of "struct type CropYield" Simple assignments are legal: apples = oranges; Simply copies each member variable from apples into member variables from oranges Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6-2 2
3
Structures as Function Arguments
Passed like any simple data type Pass-by-value Pass-by-reference Or combination Can also be returned by function Return-type is structure type Return statement in function definition sends structure variable back to caller Can you rewrite the getData() function in accountv1.cpp in a way that an account is returned by getData()? Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6-3 3
4
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Exercise 4 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
5
Hierarchical Structures
A member of a structure is a smaller structure struct Date { int month; int day; int year; }; //Improved structure for a bank certificate of deposit: struct CDAccount double initialBalance; double interestRate; int term;//months until maturity Date maturity; //date when CD matures double balanceAtMaturity;
6
Access Hierarchical Structures
Two doc operators cout << "When the CD matured on " << account.maturity.month << "-" << account.maturity.day << "-" << account.maturity.year << endl See accountv2.cpp
7
Initializing Structures
Can initialize at declaration Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003}; Declaration provides initial data to all three member variables Define Declare <- Initialize Use Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6-7 7
8
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Exercise 5 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
9
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Summary Structure is collection of different types structure types can be formal parameters to functions Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6-9 9
10
Auburn University http://www.eng.auburn.edu/~xqin
COMP 3000 Object-Oriented Programming for Engineers and Scientists Homework 5 Dr. Xiao Qin Auburn University
11
Goals To learn how to use structures To learn how to use strings
To learn creating multiple versions via conditional compilation To use arrays To design and implement functions To perform unit testing (Two versions) 6-11 11
12
Assignment Implement a simple trivia quiz game.
Players can create their trivia questions and answers. Ask a question to the player, input the player’s answer, and check if the player’s answer matches the actual answer. Award the player the award points for that question. If the player enters the wrong answer your program should display the correct answer. When all questions have been asked display the total amount that the player has won. 6-12 12
13
Trivia structure Information about a single trivia question.
What does this structure contain? a string for the question, a string for the answer to the question, an integer representing points the question is worth. 6-13 13
14
Exercise 1: Please implement the trivia structure.
a string for the question, a string for the answer to the question, an integer representing points the question is worth. struct TriviaQuestion { string question; string answer; unsigned int points; }; 6-14 14
15
Exercise 2: Create an array of trivia questions
struct TriviaQuestion{ … }; TriviaQuestion triviaArray[MAX_SIZE]; unsigned int total_num_of_trivia; 6-15 15
16
Exercise 3 Implement a function that initialize the array by hard-
coding the following one trivia question. struct TriviaQuestion{ … }; TriviaQuestion triviaArray[MAX_SIZE]; int triviaInit(TriviaQuestion triviaAry []) { triviaAry[0].question = "How long was the shortest war on record? (Hint: How many minutes)"; triviaAry [0].answer = "38"; triviaAry [0].points = 100; ... } 6-16 16
17
Exercise 4 Implement a function to create and add new Trivia into the array. Remember to check that adding a new trivia does not exceed the array’s capacity. 6-17 17
18
Exercise 4 int addTrivia(TriviaQuestion triviaAry[], unsigned int &num_of_trivia) { string award; if (num_of_trivia == MAX_SIZE) { /* output error message */ /* return error number */ return 1; } /* get a question: /* getline(cin, ...); /* get award points */ getline(cin, award); stringstream(award) >> triviaAry [num_of_trivia].points; /* how to update num_of_trivia? */ return 0; getline(cin, triviaAry[num_of_trivia].answer 6-18 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.