80 likes | 87 Views
CSE 143 Section AD. Quiz Section 4. Homework 1. string vs. char[] – C++ strings are a lot easier to use but character arrays (C strings) seem to have a much larger library of functions to use for separating strings, etc.
E N D
CSE 143 Section AD Quiz Section 4 Jeff West - Quiz Section 4
Homework 1 • string vs. char[] – C++ strings are a lot easier to use but character arrays (C strings) seem to have a much larger library of functions to use for separating strings, etc. • Whenever you make a loop in a program think about how the INITIAL and FINAL cases will operate – many people had loops that were guaranteed to run at least once when certain data might require they never be entered at all. Jeff West - Quiz Section 4
Example of a Looping Program Take five minutes to create a function that will prompt a user for their name and a number of times they would like to be greeted, after which it will greet them that many times. Stop as soon as the user inputs “Cindy” as their name and 1 as the number of times they wish to be greeted. NOTE: After Cindy has asked to be greeted 1 time you should NOT greet her, you should just end the function! Jeff West - Quiz Section 4
Notes About This Program • The user should be prompted for information before the loop decides whether or not it should enter the first time. • What should the loop require to be entered? To end? Jeff West - Quiz Section 4
A Sample Solution #include <iostream> #include <string> using namespace std; int main() { string currentName; // stores name of current user int numTimes; // stores number of times // current user wishes to be // greeted // prompt user for information cout << "What is your name? "; cin >> currentName;
cout << "How many times should I greet you? "; cin >> numTimes; while(currentName != "Cindy" || numTimes != 1) { for(int i = 0; i < numTimes; i++) cout << "Hello, " << currentName << endl; // prompt user for information cout << "What is your name? "; cin >> currentName; cout << "How many times should I greet you? "; cin >> numTimes; } cout << "Thanks for using The Greeter version 1.0."; return 0; }
Classes • Classes give us a chance to create our own datatypes that do really neat things! Think of classes as nouns which describe “categories.” Examples might include: * Person * Book * Fraction * Animal * Beverage Jeff West - Quiz Section 4
Class Members • The way classes do “totally cool things” is by storing and manipulating their own data! As an example, what member functions might a book class have? What data members might it have? * Which members do you think should be private? Which members should be public? * How should the data be initialized? What constructor(s) might you wish to provide? Jeff West - Quiz Section 4