Download presentation
Presentation is loading. Please wait.
1
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes
2
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 these http://cs.boisestate.edu/~tcole/cs117/notes.html
3
Arrays a collection of values/objects of the same type –they can be of any type int 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.
4
For arrays, know how to declare an array int score[30]; initialize an array when it is declared double x[ ] = {1.9, 2.8, 3.7, 4.6, 5.5}; access an element of the array score[3] –first element has index 0
5
Array know how cont. use a loop to do something with every element of the array for (int i=0; i<numElements; i++) sum = sum + score[i]; pass the entire array to a function highScore = max( score, nstudents);
6
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
7
C-style Strings know how to declare a C-style string char 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
8
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 <<
9
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
10
C++ string class An object-oriented way to work with text strings #include You can –declare a string string str1; –initialize a string when you declare it string 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()
11
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 operators str1 == str2 str2 < str1 concatenate with + str1 + str2
12
File I/O #include classes –ifstream for input –ofstream for output Constructors –default –initializing takes c-string filename
13
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
14
iomanip setiosflags(ios::flagname) resetiosflags(ios::flagname) –right / left –fixed –scientific –showpoint setw( int fieldwidth) setprecision( int digits)
15
Class Declaration what member variables are needed –data needed to represent the object what functions are needed –functions represent behavior and/or services
16
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 &); };
17
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
18
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
19
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
20
Member Function Signatures Inside the class definition, prototypes look just like those of a regular function double someFunction( int param1, int param2); member function has access to –member variables –parameters –any locally declared variables
21
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 executed return aDouble; } scope resolution operator: aClass:: indicates to the compiler that this is a member function of class aClass
22
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 arguments theClass obj2( 12, 3.4); This calls a constructor which has int and double parameters.
23
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;
24
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
25
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.