The designer of Java thought that C++ has too many features and designed a "stripped down" version of C++
(In that spirit, Microsoft's C# is also a "stripped down" language of C++. They first wanted to call the new language C--...)
|
class ClassName { void function1() // A member function { .... } } |
A member function in C++ always has an implicit parameter - and it is similar to a member function in Java
void function2() // A stand-alone function { .... } |
A stand-alone function in C++ done not have an implicit parameter - and it is similar to a class function in Java
int main(int argc, char *argv[]) { ... } |
(I will not explain the strange char * notation now... see pointer variables)
The main function will usually call (invoke) other (stand-alone or member) functions during the program execution.
#include <iostream.h> // Header file containing useful definitions... int main(int argc, char *argv[]) { cout << "Hello World\n"; } |
CC [options] ProgramFileName.C |
a.out |
Option | Meaning | Example | What does it do |
---|---|---|---|
-o FileName | Rename the executable output file | CC -o myProg myProg.C |
Compiler will rename executable output file from a.out to myProg |
-g | Debug Mode | CC -g myProg.C |
Compiler will insert additional information in output a.out that can be use with a debugger (a tool to help programmers find errors in the program) |
-c | Create relocatable object code | CC -c myProg.C |
Compiler will NOT produce a directly executable computer program, but
a program that can be combined with other "relocatable" codes
NOTE: use this option when your program consists of multiple text files |
-lLibFile | Specify an additional library file | CC myProg.C -lmath |
Tell the C++ compiler to "link in" (include) functions from the "math" function library. The actual UNIX filename for a C++ library file "math" is "libmath.a" |
CC -o myProg myProg.C | Compile myProg.C into the executable output file myProg (instead of a.out |
CC -g -c myProg.C | Produce relocatable object code with debug information |
The Hello World program can also be compiled and run as follows:
Compile: CC -o Hello Hello.C Run: Hello |
The -o option will rename the output file a.out to Hello
CC -g -c myFile1.C // This command will produce the // relocatable object file myFile1.o // (which can be combined with other // relocatable object files CC -g -c myFile2.C // This command will produce another // relocatable object file myFile2.o // // We can now combine these files ! CC -o myProg myFile1.o myFile2.o // Make runnable program named "myProg" // from myFile1.o and myFile2.o |
When it crashes, you can use the debugger to track down the errors.
Once you have tested your program thoroughly and made sure that it works, then recompile ALL the program files again, but WITHOUT the -g option:
CC -fast -c myFile1.C (optimize code to run fast) CC -fast -c myFile2.C CC -o myProg myFile1.o myFile2.o |
#define SYMBOL VALUE (Defines a symbolic constant) #include <FILENAME> (Insert file FILENAME in this spot) #include "FILENAME" (Insert file FILENAME in this spot) |
The brackets < ... > indicates that the inserted file is found in C++'s include directory
The quotes " ... " indicates that the inserted file is found in the current directory
#include <iostream.h> |
This header file contains commonly used constants and variables.
Example:
#define ABC abcdefghijk #define XYZ "Hello World !" int main(int argc, char *argv[]) { ABC XYZ ABC abc } |
The C pre-processor will replace each ABC with abcdefghijk
|
To see the effect of #define, do the following:
|
Example:
#include "define.C" ================================== #include "define.C" |
The line #include "define.C" is replaced by the content of the file
|