25
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

CS 117 Spring 2002

Review for Exam 3

arrays

strings

files

classes

Page 2: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Chapters covered

• Hanly– Chapter 6

• skip sections 1, 7

– Chapter 7• skip section 6

– Chapter 9

• Friedman-Koffman– Section 3.7

– Chapter 8

– Chapter 9• not section 7

– Chapter 10

– Section 11.7

Don't forget that there are web notes about all of thesehttp://cs.boisestate.edu/~tcole/cs117/notes.html

Page 3: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Arrays

• a collection of values/objects of the same type– they can be of any typeint values[20]

• sets aside memory for 20 integers• The elements are accessed by putting the index of

the element in square brackets, e.g. values[3]

• For an array declared to have n elements, the array index runs from 0 to n-1– You have to keep track of how many of the array

elements have been assigned values.

Page 4: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

For arrays, know how to

• declare an arrayint score[30];

• initialize an array when it is declareddouble x[ ] = {1.9, 2.8, 3.7, 4.6, 5.5};

• access an element of the arrayscore[3]– first element has index 0

Page 5: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Array know how cont.

• use a loop to do something with every element of the arrayfor (int i=0; i<numElements; i++)

sum = sum + score[i];

• pass the entire array to a functionhighScore = max( score, nstudents);

Page 6: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

C-style strings

• A C-style string is an array with elements of type char

• It should be terminated with the null character – character whose ASCII code is 0– '\0'

• Some useful functions for using C-style strings are in <string.h>

Page 7: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

C-style Strings know how to

• declare a C-style stringchar cstr[6];– this can hold 5 characters plus the termination character

• access an element of a C-style string – array index starts at 0– ith element is cstr[i-1]

• initialize a C-style string when you declare it– char dstr[7]="bat";– number of elements optional in this case

Page 8: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

I/O for C-style strings

• input a C-style string with >> – get next sequence of non-space characters from

the input stream

• get multiple words with istream::getline( char[], int)

• output a C-style string with <<

Page 9: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

string.h• strlen - returns number of characters in the string

– doesn't count the null character– strlen(dstr) will return 3

• strcpy - to copy a string into another one– strcpy( cstr, "man") will put man into cstr

• strcat - to append one string onto another– strcat( dstr, cstr) puts "batman in dstr

• strcomp - for comparing two string: – returns 0 if they are the same– strcomp( dstr, cstr) will be non-zero– strcomp( cstr, "man") will return 0

Page 10: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

C++ string class

• An object-oriented way to work with text strings#include <string>

• You can– declare a string

string str1;

– initialize a string when you declare itstring str2("two");

– access an element of a string• the ith element is str[I-1] or str1.at(i-1)

– get the length with str1.length()

Page 11: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

C++ string class operators

• input a string with >> - get a sequence of non-space characters

• output a string with <<• assign a value using =

str1 = "one";

• compare using familiar operatorsstr1 == str2str2 < str1

• concatenate with +str1 + str2

Page 12: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

File I/O

#include <fstream.h>

• classes– ifstream for input– ofstream for output

• Constructors– default– initializing takes c-string filename

Page 13: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

File I/O

• functions you should know how to use– open( char filename[])– eof() - to test for end of file– fail() - to test for all errors– close()

• read/write the same as for cin/cout

Page 14: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

iomanip

• setiosflags(ios::flagname) resetiosflags(ios::flagname)– right / left– fixed – scientific– showpoint

• setw( int fieldwidth)• setprecision( int digits)

Page 15: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Class Declaration

• what member variables are needed– data needed to represent the object

• what functions are needed– functions represent behavior and/or services

Page 16: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Form of declaration

class aClass {public:

aClass(); // zero parameter constructor aClass( int v1, double v2); int getVar1() const; // accessors void setVar2( double d); // mutator functions

private: int var1; double var2;friend operator<<( ostream &, const Angle &); };

Page 17: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Class functions

• constructors used to initialize objects– name of constructor is same as the class name

• member functions – accessors provide read-only access to member variables

– mutators allow user to change member variables

– friend functions

– overloaded operators

Page 18: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Functions associated with classes

• overloaded operators provide arithmetic and/or comparisons in same ways as for numbers

• friend functions allowed access to the private members of the class

Page 19: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Member functions

• function declarations (prototypes) go into class definition

• function definitions – usually outside of class definition (need scope

resolution operator ::)

– inline definitions (usually very short) are contained in the class definition and don't need the scope resolution operator

Page 20: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Member Function Signatures

• Inside the class definition, prototypes look just like those of a regular functiondouble someFunction( int param1, int param2);

• member function has access to – member variables– parameters – any locally declared variables

Page 21: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Member Function Definitions

• The function definition contains the code that is executed when the function is called.double aClass::memberFunction( int param1, int param2)

{// code that is executedreturn aDouble; }

• scope resolution operator: aClass:: indicates to the compiler that this is a member function of class aClass

Page 22: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Declaring Objects

• You declare an object by giving the className (its type) and a variable name.theClass obj1;

• This calls the zero parameter constructor• You can initialize an object when you declare it by

providing argumentstheClass obj2( 12, 3.4);

• This calls a constructor which has int and double parameters.

Page 23: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Calling member functions

• From outside the class, you call a member function using the . operator double x = obj1.someFunction( arg1, arg2);

• Inside the class, only need the . operator for an object other than the current one

• You use overloaded operators the same way you always use operators:obj1 < obj2;cout << obj1;

Page 24: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Things to remember

• member variables should not be passed to member functions – these functions already have access to them

• all class members are private unless they appear in the public section of the class definition – private members are not directly accessible from outside the

class

• don't redeclare the member variables – they are declared in the class declaration

• member functions that use only member variables need no parameters

Page 25: CS 117 Spring 2002 Review for Exam 3 arrays strings files classes

Member Functions

• accessor functions (get functions) return the values of the member variables – don't always have one for every member

variable

• provide set functions if you want the user of the class to be able to change the value of a member variable