Sample Code for COP-4338 (C++ Programming)
LAST UPDATE: October 23, 2002
BUG REPORTS ARE APPRECIATED!!
The materials here are copyrighted.
I have successfully compiled and tested the programs under Visual Studio Dot Net.
Simplest C++
First.cpp: first C++ program
FirstProgram.java: first Java program
max2.h, max2.cpp, testMax2.cpp: separate compilation example
squares.cpp: uses vector to store perfect squares
stringConcat.cpp: illustrates that StringBuffers not needed
swap2.cpp: call-by-reference used to implement swap routine
binarySearchBad.cpp: bad binary search routine because of call-by-value
binarySearch.cpp: binary search routine with call-by-constant reference
Pointers and Reference Variables
stringPointer.cpp: allocates strings from memory heap
leak.cpp: illustrates memory leak (on some machines)
stale.cpp: illustrates stale pointers, via returning pointer to stack-allocated local variable
returnStatic.cpp: illustrates returning pointer to static local variable
Object-Based Programming: Classes
deepIntCellBad.cpp: Data member is a pointer, so default Big-Three is no good
deepIntCell.cpp: Data member is a pointer, with correct Big-Three
intQueue1.cpp: Linked list queue implementation (version #1)
intQueue2.cpp: Linked list queue implementation (version #2), ListNode is friend
intQueue3.cpp: Linked list queue implementation (version #3), ListNode is nested class
intQueue3.cpp: Linked list queue implementation (version #4), ListNode is nested struct
refreturn.cpp: Two versions of findMax show difference between return by constant reference and return by value
IntCell.h, IntCell.cpp,
TestIntCell.cpp: IntCell class declaration, implementation, and test program
Ticket.h, Ticket.cpp,
TestTicket.cpp: Ticket class declaration, implementation, and test program illustrates static members
MathUtils.h, MathUtils.cpp,
TestMathUtils.cpp: MathUtils class declaration, implementation, and test program illustrates static initializer
Operator Overloading
MatrixOfDouble.h, TestMatrixOfDouble.cpp: MatrixOfDouble class and test program
Rational.h, Rational.cpp,
TestRational.cpp: Rational class declaration, implementation, and test program