cs 106 intro to cs 1 wednesday 10 23 02 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS 106 Intro to CS 1 Wednesday, 10/23/02 PowerPoint Presentation
Download Presentation
CS 106 Intro to CS 1 Wednesday, 10/23/02

Loading in 2 Seconds...

play fullscreen
1 / 10

CS 106 Intro to CS 1 Wednesday, 10/23/02 - PowerPoint PPT Presentation


  • 93 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS 106 Intro to CS 1 Wednesday, 10/23/02' - prisca


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs 106 intro to cs 1 wednesday 10 23 02
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
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
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
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
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
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
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
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 decrement operators 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
Other operators: Compound Assignment
  • x += y;Replaces x with x + y
  • x -= y;Replaces x with x - y
  • Examples:
    • Balance += Deposit;
    • TimeLeft -= TimeOfTurn;