100 likes | 228 Views
CS 106 Intro to CS 1 Wednesday, 10/23/02. QUESTIONS?? Today: Discussion of HW #3 The const modifier for functions and parameters The char object type Some new numeric operators Reading: pp. 135-142 of Chapter 6 Exercises: p. 150 #9, 10, 19.
E N D
CS 106 Intro to CS 1Wednesday, 10/23/02 • QUESTIONS?? • Today: • Discussion of HW #3 • The const modifier for functions and parameters • The char object type • Some new numeric operators • Reading: pp. 135-142 of Chapter 6 • Exercises: p. 150 #9, 10, 19
Using the const modifier on member functions • If a member function does not change the invoking object, we can prevent accidental change by adding the const modifier (Recall: Standing for “Constant”): • Complex Complex::AddC (Complex x) • Function code permitted to change invoking object • Complex Complex::AddC (Complex x) const • Function code not permitted to change invoking object • Which Complex member functions should be const? Constructor? InputC()? OutputC()? AddC()? SubC()? MultC()?
Using the const modifier on parameters • We use reference parametersif we do want to change the argument -- the actual argument is used • We use valueparameters if we don’t want to change the argument -- a copy of the argument is used • If parameters are class objects, sometimes copies can be big -- we want to use the actual object, but not change it • Const reference parameters let us use the actual object, but don’t let us change it: Complex Complex::AddC (Complex x) const //uses a copy of x Complex Complex::AddC (Complex& x) const //uses x, can change x Complex Complex::AddC (const Complex& x) const //uses x, can’t // change x
Type char objects • char is a built-in (“primitive”) object type, like int, float, double • char declaration: char ch;orchar c1(‘%’);orchar c2 = ‘x’; • char constants: single characters enclosed in single quotes • Operators that can be used with type char: • Extract: >>, Insert: << • Extraction operator ignores whitespace -- uses it to separate one (non-whitespace) char from the next. • Assign: =, Comparisons: ==, !=, >, <, >=, <= • Comparisons are based on the order of characters in terms of their ASCII codes
Changing types (in general) • There are two main ways to change objects from one type to another: • Assign the object to a variable of the new type: • int num = 3.14 (what value does num have?) • char letter = “Hi Mom” (what’s letter now?) • Use “Typecasting” – type acts as a function: • int(3.14) ? int(‘b’) ? char(126) ? double(10) ?
char library functions • Since char is a primitive type (not a class type), we don’t use the ‘dot’ notation (unless the function has a char parameter, but is a member of some other class) • Some functions that return true/false (bool) values (I’ve given prototypes to show return and parameter types): • bool islower (char c); • bool isupper (char c); • bool isalpha (char c); • bool isdigit (char c); • bool isalnum (char c); • bool isspace (char c); • bool ispunct (char c); • bool isprint (char c);
The <ctype.h> library: for changing to uppercase, lowercase • <ctype.h> contains the following two functions: • int tolower(char c); • int toupper(char c); • Returns the ASCII code of c changed to lower/upper case • To get type char, either assign to char object or use char() typecasting Program segment: char c1 = 'a', c2 = 'B'; cout << c1 << c2 << endl; cout << toupper(c1) << tolower(c2) << endl; char c3 = toupper(c1); cout << c3 << char(tolower(c2)) << endl; • Output: • aB • 6598 • Ab
More int operators: Mod operator % • Produces the remainder of the integer division • Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4 • % has same precedence as * and /
Increment and DecrementOperators ++ and -- • Increment and Decrement (prefix and postfix): • ++x, x++ : increase value of x by 1 • --x, x-- : decrease value of x by 1 • Examples: • ++count; • turnsLeft--;
Other operators: Compound Assignment • x += y;Replaces x with x + y • x -= y;Replaces x with x - y • Examples: • Balance += Deposit; • TimeLeft -= TimeOfTurn;