Review for Exam 1
CS
210
Spring 2004
Text chapters
You may use one sheet of notes during the exam
Weiss
Chapter 1 Basic
types and Control Structures
Chapter 2
Functions, Arrays, Strings and Parameter Passing
Chapter 3
Pointers and Reference Variables
Chapter 4
Object-based Programming: Classes
Lafore
Chapter 2 C++
Programming Basics
Chapter 4
Structures
Chapter 5
Functions
Chapter 6 Objects
and Classes
Chapter 7 Arrays
and Strings
Chapter 10
Pointers
Chapter 13 Multifile Programs (p634-647)
Manipulators and Escape Sequences
Manipulators are used with the insertion operator
<< to modify the way output is displayed.
endl is a manipulator
The setw(n)
manipulator
#include <iomanip>
There is a table
of the manipulators on page 572 of the Lafore book
Escape sequences can also be used to format output.
An example is the \n
Escape sequences
always start with a backslash
They can be used
either as separate characters or embedded in a string constant
Functions
Declaring and defining
Functions are
usually declared with a prototype
Calling functions
Overloading functions
Default parameters for functions
Example: void myCell(char
someC=*, int someV=0);
So this function
can be called by a client either with zero, one or two arguments.
myCell( );
myCell(A)
myCell(A,
82);
Inline functions
Parameter passing
Call by value
A copy of the actual
argument is passed so it will not be changed in the calling function.
Call by reference
The parameter is
an address, so the called function has direct access to the object to which the
address points, and can change it, but it cannot change the address
Call by
constant reference
Use this when you
do not want the argument changed
Vectors
Vectors are less error prone than standard C++ arrays
Declaring vectors
vector <int> intArray;
vector<string> stringArray;
#include <vector>
Vector member functions
assume a vector vec has
been declared
vec.pushback( elem) inserts an element at the end of the vector
vec.size(
) returns the actual number of
elements in the vector
vec.capacity( ) returns
the maximum possible number of elements without reallocation
vec.empty(
) returns true if the vector contains
no elements
vec[idx] returns the element with the index idx (no range checking)
vec.at(idx) throws range
error exception if idx is out of range
The string class in C++
#include
<string>
Strings can be
initialized at the same time they are declared, or they can be left empty.
string s1(Lots of snow);
string s2 = more snow;
string s3; // empty string
s3 = s1;
s3 = s1 + is here.
Accessing
characters in a string
You can treat a
string object like an array of characters, and access individual characters
with the [ ] operator.
The
[ ] operator does not check range
If you want range check, use str.at(index)
The .at( )
member function is safer that [ ], and lets the compiler find errors for
you
Disk file I/O
#include <fstream>
You must declare an object of type ofstream
ofstream
fileOut (myData.txt)
fileOut
<< variableName;
Close the file
when done fileOut.close( );
If the file exists, the above will overwrite the file .
If you want to append the file, you must specifically
open the file to append (the default is to overwrite). To do this use:
ofstream appendFile; //
declare the ofstream object
appendFile.open(filename.txt, ios::app);
Input of data from a file
ifstream fileIn (myData.txt)
fileIn >> variableName;
Check for end of file using the eof( )
member function: while (!fileIn.eof( ))
Practice with Pointers
int var1, var2;
int* ptr;
ptr =
&var1;
*ptr = 37;
var2 = *ptr;
cout
<< var2 << endl;
More practice
int y = 5, x = 8;
int *ptr;
ptr = &y;
What is the result of each of these statements?
ptr = x; //error
*ptr = x;
Ptr = &x;
*ptr +=6;
cout << x
<< y;
cout << *ptr;
Sample exam question
int x = 12, y = 7;
int *pt1, *pt2 ;
pt1 = &x; pt2 = &y;
*pt1+=3;
pt2 = pt1;
*ptr2 = 28;
cout << *pt1<< *pt2;
cout << x << y;
pt2=&y;
*pt2=*pt1;
*pt2 +=2;
cout << *pt1<< *pt2;
The stack and heap
Static RAM is often called the runtime stack
The program itself, functions, and named variables are stored on the
runtime stack
Dynamic RAM is called the memory heap
This is memory available from the OS to be allocated
to programs as they execute
One of the main uses of pointers is to access data
allocated on the heap
Allocating RAM dynamically
string *strPtr;
strPtr = new string( "hello" );
cout << "The string is: " << *strPtr << endl;
cout << "Its length is: " << (*strPtr).length( )
<< endl;
*strPtr +=
" world";
cout <<
"Now the string is " << *strPtr
<< endl;
delete strPtr;
The delete operator
No garbage collection in C++
The programmer must delete all memory allocated by
new, or there is a memory leak
When you say delete strPtr it deallocates the space pointed
to by strPtr, but strPtr still exists and
can be reused
Types of lists
Advantages and disadvantages of each
Arrays
Linked lists
Linked lists
First declare a struct to
hold the node
struct
node
{
string item; // this will be a list of strings
node
*nextPtr;
}
Create a class
to hold a pointer to the head of the list and the list list
operations
class linkList
{
private:
node
*headPtr;
public:
linkList ( ) //
constructor; sets up an empty list
void addToFront(string item)
}
Client of the
linked list instantiates the list (driver)
Coding linked lists
Adding nodes
Create the new
node
Set the
pointer(s) in the new node
Link in the new
node
Also need to
check to see if there are any special cases
Special
cases usually take place at the beginning or end of the list
What
you always need is a pointer to the place to insert the node.
Always draw diagrams
If you draw a
diagram, it should tell you how the code should be written;
The hard part may
be knowing how to name each pointer you want to change
Deleting nodes
Pointers and arrays
Array names are really pointers to the first element
in the array
So pointer notation is often used for arrays
int intarray[5] = { 31, 54, 77, 52, 93 };
for(int j=0;
j<5; j++)
cout
<< intarray[j] << endl;
for(int j=0;
j<5; j++)
cout
<< *(intarray+j) << endl;
Passing arrays to
functions
When an array is passed to a function two
notations can be used
We can use array notation
void fun(int A[ ] )
Or we can use pointer notation
void fun(int *iPtr )
Summary of class constructs (so far)
Constructors
Default values
for arguments
Initialization
lists
The use of
explicit
Copy constructors
Used when you
initialize an object with another object, not the primitive components
Mutators and Accessors
By default C++
considers all functions mutators
Must mark accessors with the keyword const