Operator Example and Strings - Lecture Slides | CISC 181, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computer Science II; Subject: Computer/Information Sciences; University: University of Delaware; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-p9f
koofers-user-p9f 🇺🇸

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
operator example
and strings
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Operator Example and Strings - Lecture Slides | CISC 181 and more Study notes Computer Science in PDF only on Docsity!

operator example

and strings

last class

three ways to do it

some operators only work with some ways

parameters and return values - thinking about const and references

mixing types - adding Money and int

applying operators

how could we make it easier to work with the Store example?

overload << to make it easier to display

overload +=, -= for deposit/withdrawal

strings

a sequence of characters

two different kinds

C-style string (char arrays)

string class

character manipulation

has some handy tools:

int toupper(char)

int tolower(char)

bool isalpha(char)

and many more! (p. 380)

be very careful when using the ones that return ints!

C strings

an array of characters: char str[] = “Hi”;

in this case (no array size), it picks the size automatically

null terminator - the end of the string is a special character ‘\0’

as a numeric value, this is 0

C strings

significance of the null terminator

loops use it to process a string: int i = 0; while (str[i] != ‘\0’) cout << str[i] << “ “;

if the null terminator is overwritten somehow, it’ll just keep going through memory

C strings

= and == don’t work like normal

= just fails in normal settings, use strcpy(to, from) in cstring instead

strcpy(to, from, num_chars) is another form

== will just check that the array pointers are the same, which isn’t comparing contents

I/O with C strings

can use with cout (works normally)

can use with cin (gets one word)

if you want to get multiple words, use cin.getline(string, max_chars)

be careful about using both >> and getline in the same program!

I/O stuff

cin has some other handy functions:

cin.get() - gets a char

cin.peek() - looks at the next char but doesn’t change the buffer

cin.putback(char) - puts a char back

cin.ignore(int num_chars, char terminator) - ignores up to num_chars on the stream or until it hits the terminator char

next time

C++ string class

pointers overview

midterm review