Download presentation
Presentation is loading. Please wait.
1
Jeff West - Quiz Section 4
CSE 143 Section AD Quiz Section 4 6/26/2001 Jeff West - Quiz Section 4
2
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. 6/26/2001 Jeff West - Quiz Section 4
3
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! 6/26/2001 Jeff West - Quiz Section 4
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? 6/26/2001 Jeff West - Quiz Section 4
5
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;
6
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 << "Thanks for using The Greeter version 1.0."; return 0;
7
Jeff West - Quiz Section 4
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 6/26/2001 Jeff West - Quiz Section 4
8
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? 6/26/2001 Jeff West - Quiz Section 4
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.