A Computer Science Tapestry5.1Topicsz Date and Dice Classesz Repetition and Iterationz Using classes and creating objectsz Assignment¾ Pig Latin & Perfect Numbersz Reading¾ Through the end of Chapter 5z Upcoming¾ Using classes¾ Chapter 6A Computer Science Tapestry5.2Using classesz Using only strings, ints, and doubles limits the kinds of programs we can write¾ What about graphics?¾ What about calendars, address books?¾ What about web-servers, games, …?z Using object-oriented techniques means we develop new types that correspond to the real-world artifact we’re writing code for¾ What about an online roulette game?¾ What about appointment book that synchs with PDA?z New types are called classes, variables are called objects and objects are instances of a class, e.g., 3 for int, “hello” for stringA Computer Science Tapestry5.3The class Datez The class Date is accessible to client programmers by¾ #include "date.h" to get access to the class• The compiler needs this information, it may contain documentation for the programmer¾ Link the implementation in date.cpp, which has been compiled to date.o (and maybe stored in a library)z The class Date models a calendar date:¾ Month, day, and year make up the state of a Date object¾ Dates can be printed, compared to each other, day-of-week determined, # days in month determined, many other behaviors• Behaviors are called methods or member functionsA Computer Science Tapestry5.4Constructing Date objectsz See usedate.cppint main(){Date today;Date birthDay(7,4,1776);Date million(1000000L);Date badDate(3,38,2001);Date y2k2(1,1,2002);cout << "today \t: " << today << endl;cout << "US bday \t: " << birthDay << endl;cout << "million \t: " << million << endl;cout << "bad date \t: " << badDate << endl;cout << y2k << " is a " << y2k.DayName() << endl;A Computer Science Tapestry5.5Constructing/defining an objectz Date objects (like string objects) are constructed when they’re first defined¾ Three ways to construct a Date, what are they?¾ How have we constructed string objects?z Constructors for Date objects look like function calls¾ We’ll see that constructor is special member function¾ Different parameter lists means different constructorsz Once constructed many ways to manipulate a Date¾ Increment it, subtract an int from it, print it, …¾ MonthName(), DayName(), DaysIn(), …A Computer Science Tapestry5.6Finding Thanksgiving in the USz Thanksgiving occurs on fourth Thursday in NovemberDate Thanksgiving(int year)// post: return date for Thanksgiving in yearcout << "what year ";cin >> year;cout << "bird day is " << Thanksgiving(year) << endl;z How do we write the function?¾ How is it similar to Labor Day, Mother’s Day, Flag Day?¾ Can we generalize the function?A Computer Science Tapestry5.7The class Dicez Accessible to client programmers using #include "dice.h"¾ How do clients get access to implementation?¾ Why are quotes used instead of angle brackets < .. > ?z What do we do with Dice outside of programs (real world)¾ What would be nice to model with the class Dice?¾ What would be hard?z Dice objects will work as pseudo-random number generators¾ Not truly random in a strict mathematical sense¾ Still useful to introduce randomness into programs¾ Some random numbers are more random than othersA Computer Science Tapestry5.8Using the class Diceint main(){Dice cube(6); // six-sided dieDice dodeca(12); // twelve-sided diecout << "rolling " << cube.NumSides() << " sided die" << endl;cout << cube.Roll() << endl;cout << cube.Roll() << endl;cout << "rolled " << cube.NumRolls() << " times" << endl;// more herez See roll.cpp, how is a Dice object constructed?A Computer Science Tapestry5.9From Selection to Repetitionz The if statement and if/else statement allow a block of statements to be executed selectively: based on a guard/testif (area > 20.0){cout << area << " is large" << endl;} z The while statement repeatedly executes a block of statements while the guard/test is trueint month = 0;while (month < 12){PrintCalendar(month, 1999);month += 1; // month = month + 1;}A Computer Science Tapestry5.10Semantics of while loopif (test) while (test){ {statements; statements;statements; statements;} }testStatement listNext statementtruefalsetestStatement listNext statementtruefalseA Computer Science Tapestry5.11Print a number backwardsz Given 12345, print 54321¾ How can we get just one digit from a number?¾ How can we remove the digit from the number?void ReversePrint(int num)// post: print num backwardsint Reverse(int num)// post: return reverse of num¾ What to return for 123? For 100?, what about printing?z We need a loop: what’s the loop test? What’s the loop body?A Computer Science Tapestry5.12Print a string backwardsz Determine # characters in string, access each character ¾ What string functions do we have ?¾ How many times should the loop iterate ?cout << "enter string: ";cin >> s;cout << s << " reversed is ";k = s.length() - 1; // index of last character in swhile (k >= 0){ cout << s.substr(k,1);k -= 1;}cout << endl;z Modify to create a new string that’s the reverse of a string.A Computer Science Tapestry5.13ReverseString as a functionz First step, what is the prototype?string Reverse(string s)// pre: s = c0c1c2…cn-1// post: return cn-1…c2c1c0z Second step, how do we build a new string?¾ Start with an empty string, ""¾ Add one character at a time using concatenation, +rev = rev + s.substr(k,0);z Use Reverse to determine if a string is a palindromeA Computer Science Tapestry5.14Anatomy of a loopz Initialize variables used in loop/loop test (before loop)¾ Loop test affected by initial values of variablesz The loop test or guard is evaluated before each loop iteration¾ NOT evaluated after each statement in loopz The loop body must update some variable/expression used in the loop test so that the loop eventually terminates¾ If loop test is always true, loop is infinitek = s.length() - 1;string rev = ""; while (k >= 0){ rev = rev + s.substr(k,1);k -= 1;}return rev;A Computer Science Tapestry5.15Infinite loopsz Sometimes your program will be “stuck”, control-C to stop¾ What’s the problem in the loop below? Fixable?cin >> num;int start = 0;while (start != 0){ start += 2;cout << start << endl;}z It’s impossible to write one program that detects all infinite loops (the compiler
View Full Document