Download presentation
Presentation is loading. Please wait.
1
Wednesday, 10/23/02, Slide #1 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
2
Wednesday, 10/23/02, Slide #2 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()?
3
Wednesday, 10/23/02, Slide #3 Using the const modifier on parameters We use reference parameters if we do want to change the argument -- the actual argument is used We use value parameters 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
4
Wednesday, 10/23/02, Slide #4 Type char objects char is a built-in (“primitive”) object type, like int, float, double char declaration: char ch; or char c1(‘%’); or char c2 = ‘x’; char constants: single characters enclosed in single quotes Operators that can be used with type char: Extract: >>, Insert: >, 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
5
Wednesday, 10/23/02, Slide #5 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) ?
6
Wednesday, 10/23/02, Slide #6 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);
7
Wednesday, 10/23/02, Slide #7 The library: for changing to uppercase, lowercase 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
8
Wednesday, 10/23/02, Slide #8 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 /
9
Wednesday, 10/23/02, Slide #9 Increment and Decrement Operators ++ 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--;
10
Wednesday, 10/23/02, Slide #10 Other operators: Compound Assignment x += y; Replaces x with x + y x -= y; Replaces x with x - y Examples: Balance += Deposit; TimeLeft -= TimeOfTurn;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.