Objectives:
To develop the basic techniques of building a class
General pattern of a class.
Default argument
Operator overload
To become aware of separate compilation
To become aware of the debugger gdb
Print out the (completed) program listSimple.cp before the lab and hand in at the beginning of the lab.
Read the lab exercise once and prepare necessary
materials.
(10 minutes) Modify the method cons() in listSimple.cp
(shown below) and make the second argument/parameter have a default value of
null.
Node *cons( int const& x, Node const *const& y)
{
Node *new_Node = new Node;
new_Node->item = x;
new_Node->next = const_cast<Node*> (y);
return new_Node;
}
Then change the first declaration statement in main():
Node *a(cons(37, cons(52, cons(96, 0))));
to
Node *a(cons(37, cons(52, cons(96))));
Compile and execute the program.
(15 minutes) Modify the program set list.h,
list.cc, and list-test.cp to allow the program to overload the operator << for output rather than using the method
print().
Start to work on the class Roster in the Practice Problems
in CTE Exercise 3, finish it at home, and demonstrate it next lab. For
a sample of execution, download from here.
To run the sample, make sure names.dat (available from CTE) is there and run
it as: Roster-test < names.dat.
2 for listSimple.cp -- no marks given when wrong/unexpected output for more than two executions.
1 for adding default arguments.
1 for overloading the operator <<.
Marks are awarded according to the completeness and quality of a program/answer.
0 -- absent (without extenuating circumstance) or no participation at all.
The following exercises do not affect your marks but are for the sake of more practice (make perfect), using useful utilities, and/or challenge.
Read Multiple files and separate compilation, have the program set list* ready and try different ways to compile the program set list*.
Follow "Using gdb" in Practice Problems of Exercise 3. It will lead you to get familiar with the debugger gdb and it will be useful to assist your future debugging.
Write a program as specified in I/O Formatting in Practice
Problems of Exercise 3.
Last modified: September 20, 2002