Sample Source Code for C++ for Java Programmers

LAST UPDATE: November 4, 2003

The materials here are copyrighted. I have successfully compiled and tested the programs under Visual Studio Dot Net.

Complete Bundle

Compilation Instructions

Visual Dot Net batch file; seems to work for Visual Studio 6.0 also.
Solaris, g++ 2.95 batch file. Note that some I/O is unsupported, so formattedOutput.cpp, lastChars.cpp, and concordance.cpp will need modificiations. Also, the exceptions in logic_error hierarchy are not implemented correctly. I have added exceptions.h with this implementation, and if the code is compiled with -DGNU_EXCEPTION_BUG, so header file will be included.
Borland 5.5 free compiler. You can get the free command line compiler at: ZDNet. Here's info on setup and using bcc32.

Basic Types and Control Structures

First.cpp: first C++ program first Java program

Functions, Arrays, String, and Parameter Passing

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
intQueue4.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

Object-Oriented Programming: Inheritance

inherit1.cpp: Person and Student class
shapes.cpp: Illustrates abstract classes and polymorphism with collections in C++
interfaces.cpp: Illustrates multiple interface inheritance in C++
multiple.cpp: Illustrates multiple implementation inheritance in C++
privateInheritance.cpp: Illustrates private inheritance in C++
hiding.cpp: Illustrates hiding of base class member functions


findMax.cpp: findMax function template
ambiguous.cpp: Illustrates ambiguous calls to max
ObjectCell1.h, TestObjectCell1.cpp: Illustrates class template, without separate compilation
Matrix.h, TestMatrix.cpp: Illustrates Matrix class template
ObjectCell.h, ObjectCell.cpp, ObjectCellExpand.cpp, TestObjectCell.cpp: Illustrates class template, with separate compilation and explicit instantations
memberTemplates.cpp: member templates
funcObjects1.cpp: function objects using Java style Comparator
funcObjects2.cpp: function objects using Java style Comparator, but no inheritance
funcObjects3.cpp: function objects using C++ style overloading operator()

Abnormal Flow Control

bad1.cpp: Illustrates that uninitialized variables generate at most a warning in C++
bad2.cpp: Illustrates that type confusion is allowed in C++
bad3.cpp: Illustrates that const can be thrown away
bad4.cpp: Illustrates that array indexes are not checked
atexitDemo.cpp: Illustrates the use of atexit
assertDemo.cpp: Illustrates the use of assert
exceptionDemo.cpp: Illustrates the use of exceptions

Input and Output

readData.cpp: Function template to read array of items, with error checking
formattedOutput.cpp: Illustrates manipulators for formatted output
getlineDemo.cpp: Illustrates an implementation of getline
lastChars.cpp: Prints end of a file Java code uses StringBuffer to reads lines containing exactly two ints
twoInt1s.cpp: C++ code uses istringstream to reads lines containing exactly two ints (version 1, block local istringstream)
twoInt2s.cpp: C++ code uses istringstream to reads lines containing exactly two ints (version 2, single istringstream)
toString.cpp: Function template for toString using ostringstream

Collections: The Standard Template Library

printContainer.cpp: Prints STL containers, including maps
heterogeneousContainers.cpp: STL containers storing objects related by inheritance
printReverse.cpp: Prints container in reverse, using both forward iterators and reverse iterators
queueDemo.cpp: Illustrates queue adapter
heterogeneousSet.cpp: STL set storing objects related by inheritance; uses function object
mapDemo.cpp: Illustrates two ways to acccess values in a map
concordance.cpp: Generates a concordance

Primitive Arrays and Strings

getInts.cpp: Reads an umlimited number of ints using array doubling
pointerHopping.cpp: Illustration of pointer hopping
testEcho.cpp: Echo program
printEnvironment.cpp: Prints environment variables
twoD.cpp: Two dimensional primitive arrays is not pretty

C-Style C++

printDebug.cpp: Illustrates macro for debugging prints
reinterpretCast.cpp: Illustrates reinterpret_cast
swapPtr.cpp: Illustrates C-style simulation of call-by-reference via pointers
friday13.cpp: Illustrates mktime and asctime
copyFile.cpp: Copying files using C-style I/O
getlineFastDemo.cpp: Uses fgets to implement faster getline
lastCharsInC.cpp: Prints end of a file, using C-style I/O
qsortDemo.cpp: Illustrates qsort
varargsDemo.cpp: Illustrates variable-length argument lists

Using Java and C++: The JNI, HelloNative.h, HelloNative.cpp: Simple native method to print hello world,, Date.h, Date.cpp: Native method to implement (three different ways) printDate method in Date, StringAdd.h, StringAdd.cpp. StringAdd.c: Native method to implement (two different ways in C++, one way in C) string concatenation method add, NativeSumDemo.h, NativeSumDemo.cpp: Native method to implement (two different ways both with and without exceptions) the sum method to add elements in an array, StringStuff.h, StringStuff.cpp: Native method that illustrates DeleteLocalRef, InvokeHello.cpp : Illustrates invocation API