Below is a summary of some of the basics of C++, along with a few pointers and tips on UNIX and Make. This only serves as a brief introduction, and you should rely heavily on a C++ programming textbook. One such textbook is suggested on the course outline. Because most students are already familiar with C++, it is not appropriate to require a textbook in the course. Feel free to choose any C++ textbook that you feel comfortable with (either in the campus bookstore or at Barnes and Noble, Amazon.com, etc.) If you are uncertain, then get the book that is recommended for the course.
When dealing with compiling and building programs, the UNIX command line is often used rather than the mouse-based interfaces. The following is a summary of handy commands:
Make is the primary means for managing the compilation of large C++ projects on UNIX. Make is a program that keeps track of which files have been edited/modified, and rebuilds any executable files that are affected by the changes. To build the program, all you need to do is type "make" in the directory containing the Makefile! When you do this, you should see some output like the following as the program is built (assuming the program is named vecdemo, and the class file is Vector.cc):
% make CC -g -DMAIN -DSVR4 -c Vector.cc -o Vector.o CC -g -DMAIN -DSVR4 -c vecdemo.cc -o vecdemo.o CC -g -DMAIN -DSVR4 vecdemo.o Vector.o -o vecdemo -lm
(Without the makefile, you would have to do this by hand each tme you edit your program.)
The operation of the command "make" is governed by the presence of a file called "Makefile" in the current directory. The details of the structure of the makefile can be pretty complicated, so I have provided a simplified Makefile for your use. The Makefile supplied in the projects includes a command:
PROGRAM=f1
This line establishes the name of the program. So, all you have to do is modify that line (unless you need to add new libraries). The file presumes that your main source-code file will be named f1.cc, and that you will be using the same C++ class filename that I provided (Myclass.cc, or Iclass.cc). If all goes well, this is the only modification that you will need.
As far as libraries go (and headers include files), type "man log", and notice the sample compile line and header include statement. Just modify the compile line to include the needed "-lwhatever" to add library "whatever." In the case of log, "-lm" is indicated. (Also note the format of the include statement "#include
$(CCC) $(PROGRAM).o Iclass.o -o $(PROGRAM) -lm -lwhatever
Finally, a brief note on the meaning of the lines in the Makefile. The 2 lines:
say that if either file Vector.cc or Vector.h have more recent
dates/times than the file Vector.o, execute the second line. In
the second line $(CCC) is replaced by the earlier definition of CCC.
The basic idea of these statements are to automatically update the
appropriate files when the source files or headers are edited/changed.
You may wish to try out the gnu compiler, too. These lines are
commented out using pound signs at the beginning of the lines.
Finally, if you type "make clean" the ".o" files and executable are
deleted. Occasionally this is needed in complex compilings to
"start over".
The main features of C++ are that it introduces the notion of "classes"
and permits "Object-oriented Coding." Classes allow the programmer
to create new types of data structures, AND the class member functions
are permitted to operate on the class data structures.
Further, "firewalls" can be built around the data to
prevent inadvertent data destruction. In general, it is good style to
only access data through these class functions - NEVER directly access the data.
But before we describe classes, we describe a few commands in C++.
Let's start with the world's simplest C++ program, hello.cc:
Above, "int main() { ... }" is the main routine that will execute is defined.
This is what will run when you compile hello.cc and type "hello" at the command
line to execute the program. The "#include < iostream.h >" statement is
used to include input/output functions from the UNIX system. In particular, it
would include the definition of "cout <<." If file IO was used,
"#include < fstream.h >" would also appear at the top of the file.
Other commonly included files are "#include < stdlib.h > ,"
"#include < new.h >," "#include < math.h >," and
"#include < string.h >."
"cout <<" is C++ handy method of printing to the terminal, and "\n" is a carriage return. Just as in C, curly brackets are used to begin and end functions and subprograms, and semicolon is used to end each command.
Finally, "//" is used to start comments in C++. (C programmers can still use /* */, but "//" is much easier.)
OK, let's make it a bit more complicated:
An integer and floating point variable are created and initialized. A character array was also created of length 20, loaded with "one,", then concatenated with "two." (Note the added include file string.h, some systems call this strings.h.) The "(float)" in the printout is ud]sed to cast the integer variable to a floating point variable. Sometimes this is useful for integer types that occasionally are mishandled/mangled in the printing statements. After building and running this, the output will be:
Finally, the typical math operators are available, +, -, =, *, /.
Pointers are not unique to C++, but are so frequently used that they are mentioned here. In the computer, the data is stored in memory locations. Each memory location has an unique address. Thus, there is a distinction between the address of the data (or location of the data) and the data itself (or contents of the location).
In C and C++, pointers are used to indicate the address of data - or to "point to" data. To define a pointer to an integer data item, one would write:
As written above, xp is a pointer to some integer data. When the program runs, it creates space large enough to hold an integer and loads the variable xp with a pointer to the data area (loads xp with the address/location of the data). Thus, if one were to print out xp, it would print the address of the data rather than the data itself.
Given a pointer xp as defined above, it is necessary to provide some mechanism for accessing the data -- since we usually aren't interested in the address/location of data. To access the data, the "*" is used again. For example,
where the first command stores the value 4 in the locaction pointed to by xp. In the second command, the variable y is loaded with the contents of the location pointed to by xp. The use of "*" to access the data is called "dereferencing the pointer."
The above example is not very useful. The real power of pointers is in accessing arrays of data. By convention, the pointer points to the first storage location. Then,
would access the 12th element in an array (0 offset is the first element). ( Note: *x + 11 would add 11 to the contents of the location pointed to by x.)
Finally, arrays can be dimensioned using the "[]" notation.
The above creates a size 100 array of integers. The 8th item could be accessed using pointer notation as "*(x+7)" since the name of the array can by default be used as a pointer to the first element of an array. Note, array indices start with zero. Also, it is possible to access the 8th element as "x[7]"
Again, the main feature of C++ is its class definitions. The overall format of the class definition is:
In the above example, data is placed in a private area denoted by the keyword private.
This data can only be accessed directly by "member functions." Member functions,
are functions that are contained in and defined in the class. These generally would
appear after the "public:" keyword above. In addition, the class definition above
would typically contain only "prototypes" of the functions, not the source code
implementing the functions. (An exception is made for extremely short/trivial
functions that can be done "in-line.") As a point of style, you are to include
the class definitions in header files (i.e., Classname.h) with sourcecode in
another file (i.e., Classname.h). This makes it simple to use the class in
other programs just by including the line #include "Classname.h" (Note,
parentheses are used to include local files, and the brackets are used to
include global system files such as #include < new.h >
).
Let's now consider a more complex class, one designed for doing vector math. The class is called Vector, and its class definition is in the header file Vector.h.
A somewhat mysterious element of C++ is "this." Recall that the method of invoking functions is generally something like:
In the above, function "myfunction" is invoked by class object "b" and passes arguments "c,4,3,..." to the function. Once inside the function, the arguments in the parentheses are identified by the argument variables in the function definition such as "Vector::myfunction(Myclass & x, int y, float z)." However, the invoking object "b" is not an argument, so some mechanism is required to get access to the invoking object. This mechanism is through the pointer "this" which points to the invoking object. Thus, the object is "*this" using "*" to dereference the pointer.
It is not always necessary to use "this" to access the invoking objec, although it helps prevent confusion. The foregoing example class "Vector" contained data "int x", "int y" and "char *Message". This data, for the invoking object, could be accessed by (*this).x, (*this).y, and (*this).Message. However, it is understood within a class function by default that x, y, and Message alone (without the *this) refer to the corresponding data items of the invoking object.
The "new" and "delete" commands in the C++ language are used to allocate and deallocate memory for data. There are two ways to allocate memory: 1) allocate the memory in some fixed quantity at the time of compiling the program, 2) allocate memory dynamically as the program is running. In the first case, the size of the memory area is fixed and must be determined by the programmer beforehand. One example would be a fixed size array declaration such as:
The command above would create a fixed-size array with size 1000. A second method, is to allocate memory from the computer while the program is running. This is useful for handling unknown data sizes such as when reading in a file. In this case, the "new" command is used to allocate memory from the "free store". For example
Use the functions provided in the projects to access files.
Vector.o:Vector.cc Vector.h
$(CCC) -c Vector.cc -o Vector.o
C++ Summary
First. C is a subset of C++. Any function that you can use in C can be
used in C++. This means C programmers can quickly adapt to C++. It
is even better if you don't have any C background, since you will not
be tempted to do things the "wrong way." In particular, C programmers
should be particularly careful in using "prototypes" and resist all
temptation to use "malloc" (instead use "new").
Some C++ Commands
#include <iostream.h>
int main () // this is the main executable
{
cout << "hello\n";
}
#include <iostream.h>
#include <string.h>
int main () // this is the main executable
{
int x=2;
float y=3;
char s[20];
strcpy(s,"one");
strcat(s," two");
cout << "hello x=" << x <<" x=" << (float) x << " y=" << y << "\n";
cout <<"hello2:" << s <<"\n";
}
hello x=2 x=2 y=3
hello2:one two
Some pointers on pointers and arrays
int * xp;
int y;
int * xp;
* xp =4;
y = * xp;
*(x + 11)
int x[100];
Classes
class classname //the class name
{
private: //what follows is not accessible to outside world
int x; // generally your data goes here
float y;
public: // what follows is accessible to the outside world
// generally the functions that access and
//manipulate the data follow this point
};
A larger example class
// Vector.h
#ifndef _Vector_h // this prevents multiple inclusion
// of the header
#define _Vector_h 1
#include <stdlib.h> // standard library
#include <iostream.h> // basic I/O library
#include <new.h> // library for new/delete
#include <fstream.h> // library for file I/O
#include <string.h>
class Vector //our new class
{
private: // (private) data in our class:
char *Message; // a character string
int x; // an integer variable
int y; // another
public: // (public) functions in our class:
Vector(); // default constructor
Vector(char *, int, int); // "full" constructor
~Vector(); // destructor
void print() // function to print out the contents
// implemented "in-line" below:
{cout << Message << " x=" << x << " y=" << y <<"\n";}
Vector &operator=(Vector&); //"overloaded" equal
Vector inner(Vector&); //inner product, x1*x2 + y1*y2
};
#endif
The main features to point out in this example are:
this and *this
a = b.myfunction(c,4,3,...);
new and delete
int z[1000];
int * xp;
xp = new int[s];
where "s" is a variable containing the size of the array to be created. The above creates an array of size s, and sets the pointer xp to point at the first element in the array. Thus, "*(xp+4)=55;" would set the contents of the 4th element of the array to 55.
file IO
Copyright information