C++




Obtaining, Setting up, and Using a Development Environment

C++ is a very popular language so there are many free and pay compilers/environments to select from that can be used on many different platforms. It is possible to compile C code as well in most C++ development environments.
Go to cplusplus.com: for free, shareware and commercial options. More: 1, 2, 3.
Borland Downloads

For general information on using an environment go to: cpp_edit_comp.html

Basic Structure

#include < iostream.h > // Library headers, ".h" means header file

//"main" code block
main() {

cout << "Hello"; //code goes between the { }

}
Other blocks are added, but all programs start at main() or void main(). ; indicates the end of a line of code. Values between quotes are literal strings.

Input and Output

cin>> for command line input, example:
int myAge;
cin>>myAge;
Puts a typed int value into "myAge"

cout<< for screen output
cout<<"My Age is "<<myAge;
Prints the previously entered value to the screen.

Data Types

int integer, a number. int aNum = 5;
float a number with a decimal. float aDec = 5.6;
double big numbers with decimals
char charcters, meaning single letters of the alphabet, numerals and special escape chars. If a number is declared as a char it is character not a numerical value. char letter = 'a'; Notice that single-quotes are used.

Strings are made by creating char arrays. char myName[] = "Joseph"; Notice that doube-quotes are used.

Arrays

const int a = 4; //const means constant, it cannot be changed while program is running
int myarr[a]; //Creates an array of int values with a size of "a" which is 4 in this case

const int b = 5;
const int c = 5;
int myarr2D[b][c]; //creates a two dimentional array that is 5 x 5
sample array program
General array info

Control Structures

herrons.cpp uses while
fibonacci.cpp uses for
clock.cpp uses nested for
ebcdic_converter200.cpp uses switch/case
General information

Commenting

Text on a single line following // is not read by the compiler
Multi-line text between /* and */ is not read by the compiler. Example:

aNum = 7 * 8; <---- Read by compiler

//aNum is 7 times 8 <---- Not read by compiler

/*aNum is an integer
that takes number values*/
<---- Also not read by compiler


File I/O

To read from or write to a file, you must declare a stream type. First, place these libraries in the include block:
#include<fstream.h> //opens file streams
#include<iomanip.h> //alows stream manipulation

For writing to files the declaration is: ofstream streamname("filename"); Example:
ofstream outfile("output.txt");

For reading from files the declaration is: ifstream streamname("filename"); Example:
ifstream infile("input.txt");
These only create the streams, the actual read an writes are done like this:
#include<fstream.h>
#include<iomanip.h>
#include <iostream.h>
#include <stdio.h>


void main(){

ifstream infile("input.txt"); //Create a simple text file with a few lines for testing, will error out otherwise
ofstream outfile("output.txt"); //Will be created if it does not exist, will be overwritten if it does exist
char oneChar; //declare a char that will hold the file data, one char at a time

infile>>oneChar; //using the stream declared above get the first char in the file

while(!infile.eof()){

cout<<oneChar;
infile>>oneChar;

}

/* Using a while loop we print out the contents of a file one at a time. A character is printed and a new character is taken from from the file stream. The condition in the while loop, !infile.eof(), means: keep doing this until we reach the end of the file. the ! "not", infile is our file stream, eof() is a function that determines if we have reached the end of the file. */

infile.close; //close the file stream when we're done

/* The following is a variation on the above that reads from one file and writes the contents to another file. Basically, we substitute cout with the name of our write file stream: outfile.*/

ifstream infile2("input.txt"); //create a new stream
infile2>>oneChar;

while(!infile2.eof()){

outfile<<oneChar;
infile2>>oneChar;

}

infile2.close; //close the file stream when we're done
outfile.close; //close the file stream when we're done

}//end main
This is a very basic file I/O scheme, there is much more to it: C++ File I/0

Functions

In C++ functions must be declared outside of the main() block, usually below the #include statements and any #define statements. If functions are not declared here first, the compiler will not recognize them. The return type, name and parameter types must be declared(but not the actual parameter names).

#include <iostream.h>
#include <stdio.h>

int addNums(int, int);
So, this is not the function itself, this is not the call to the function. This merely tells the compiler that there will be a function called "addNums" that takes two integers and returns an integer. The following would be the call to the function from the main() block:

addNums(5, 6);
Here we are passing 5 and 6(two integers) to the function. Below is the actual function, this must be outside the main block in its own block:

int addNums(int iOne, int iTwo){

return iOne + iTwo;

}
The whole program is below:
#include <stdio.h>
#include <iostream.h>

int addNums(int, int);

void main(){

cout<<addNums(5, 6);

}

int addNums(int iOne, int iTwo){

return iOne + iTwo;

}



Walk through the creation of a program, step-by-step.
        This program calculates the day of the week any particular day fell on in any year from 1900 up.

Variables.
        A simple program uses Roman Numerals to illustrate the use of variables.

Introduction to C++ Header Files
        What are header files? How do you use them?

This program illustrates the use of nested for loops
        For loops can be very confusing for the new programmer, especially when they are nested.


A C++ program for extracting data from complex logs.
        I developed it for a special purpose, but it can easily be adapted for
        any program requiring reading in from a file, sorting data and outputing to another file.

An EBCIDIC to ASCII & ASCII to EBCDIC converter
        This program will open an EBCDIC code file and convert it to ASCII or an EBCDIC file and convert it to ASCII
        This brief program gives you a good picture of file translation and conversion through character search and replacement.

Code Samples

Text Editor: editor.cpp
     Simple program allows a user to type in a page or two of text, and then displays that text.

2 Dimentional Array: d2array.cpp
     Creates a "matrix" with two arrays

Grade Computation: grades.cpp
     Takes in data from a file and calculates a grade, then reports back to a file

Fibonacci Sequence: fibonacci.cpp
     See the math section for a better description.

Conway's Game of Life: conway.cpp
     Not really a game. This program uses multi-dimentional arrays to randomly generate a simulated "life community."

Herron's Formula: herrons.cpp
        May require some experimentation.




Useful C++ Links

UNIX man pages for cpp
msdn: c++
cplus.about.com
Bjarne Stroustrup's C++ Style and Technique FAQ
C plus plus
C++
How to Design Programs
C++ In Action
Rational Rose C++ Guide
Various C++ code libraries
Borland Development(registration required)
C++ and zip files
C++ Programming Fundamentals
CProgramming.com
Standard library functions(stdio.h)
C++ Tutorial for C programmers
More conway
C++ in AP Computer Science
freshmeat.net code library
Accessing a directory and all the files within it