CPSC 427a: Object-Oriented Programming
Michael J. Fischer
September 8, 2011
Insertion Sort Example (continued)
C++ version See code demo 02-InsertionSortCpp and following notes.
A more efficient but non-standard replacement for include guards:
defines a new class named DataPack.
By convention, class names are capitalized.
Note the required semicolon following the closing brace.
If omitted, here’s the error comment:
../datapack.hpp:11: error: new types may not be defined in a return type
../datapack.hpp:11: note: (perhaps a semicolon is missing after the definition of ’DataPack’)
../datapack.cpp:12: error: two or more data types in declaration of ’readData’
A constructor is a special kind of method.
Automatically called whenever a new class instance is allocated.
Job is to initialize the raw data storage of the instance to become a valid
representation of an initial data object.
In dataPack example, store must point to storage of max bytes, n of which are currently in use.
new does the job of malloc() in C.
cout is name of standard output stream (like stdout in C).
<< is output operator.
readData() is private function to read data set from user.
Design question: Is this a good idea?
A destructor is a special kind of method.
Automatically called whenever a class instance about to be deallocated.
Job is to perform any final processing of the data object and to return
any previously-allocated storage to the system.
In dataPack example, the storage block pointed to by store must be deallocated.
Name of the destructor is class name prefixed with ~.
delete does the job of free() in C.
Empty square brackets  are for deleting an array.
Ordinary (non-inline) functions are defined in a separate implementation
Function name must be prefixed with class name followed by :: to
identify which class’s member function is being defined.
Example: DataPack::readData() is the member function readData() declared in class DataPack.
C++ file I/O is described in Chapter 3 of textbook. Please read
ifstream infile( filename ); creates and opens an input stream
The Boolean expression !infile is true if the file failed to open.
This works because of a built-in coercion from type ifstream to type
bool. (More later on coercions.)
readData() has access to the private parts of class dataPack and is responsible for maintaining their consistency.
As usual, the header file is included in each file that needs it: #include
banner(); should be the first line of every program you write for this
course. It helps debugging and identifies your output. (Remember to
modify tools.hpp with your name as explained in Chapter 1 of
Similarly, bye(); should be the last line of your program before the
return statement (if any).
The real work is done by the statements DataPack theData; and theData.sortData();. Everything else is just printout.
Building Your Code
Manual compiling and linking One-line version
g++ -o isort main.cpp datapack.cpp tools.cpp
g++ -c -o main.o main.cpp
g++ -c -o datapack.o datapack.cpp
g++ -c -o tools.o tools.cpp
g++ -o isort main.o datapack.o tools.o
Makefile make is a tool to automate the build process.
It is controlled by a Makefile or makefile.
A minimal example:
Note: The g++ line must begin with a tab character.
Integrated Development Environment (e.g., Eclipse) Advantages
Integrated Development Environment (e.g., Eclipse) Disadvantages
Integrated Development Environment (e.g., Eclipse) If you use Eclipse, before submitting your assignment, you should: