For those that are not so familiar with C++ and want to do something on the source code themselves I start a online lecture in thread-format on this subject. Note weather I can hold it on a regularly base depends on my time. It is based on a script to lecture by Andreas Döring, hold at the start of the summer semester 2004 at the Freie Universität Berlin. Note that this script is in German and therefore I have to translate it, so that I will post it stepwise. Actual this is a good thing so that it is posted in small portions. It is entitled "Einführung in C++" in English "Introduction into C++". So let's start:
Of course first the obligatory motivation:
Why C++?
Here are some answers:
1. C++ is fast.
2. C++ is widespread.
3. C++ has a lot of language constructs.
4. C++ allows machine-oriented programming.
And of course the most important reason:
5. CTP2 is written in C++.
1. Well in spite of opposite messages C++ is still faster then e.g. comparable programs written in Java, but note bad written C++ might be slower then well written Java.
3. As the complexity of C++ is high we won't probably cover everything, fortunately this isn't necessary.
4. Machine-oriented programming is an advantage but also a disadvantage. The advantage is that the machine-orientation makes C++ efficient, the disadvantage is that it is more difficult to handle.
-------------------------------------------------------------------------
Some useful things to know about C++:
-It is an imperative programming language.
-Contains more then 60 reserved keyword, some of them with multiple meaning, that is context depending.
-The source code is translated (compiled) by a compile into an executable program.
Imperative programming language: Instructions are executed consecutively. The control flow is handled by instructions.
Compiling C++ programs takes often a lot of rime. JIT (just in time) compiling like Java is not possible. One reason for this is that C++ compiler use very good optimizer to make the resulting program to run as fast as possible. But note don't rely always on the optimizer, if you chose the wrong algorithm, or you make the code to complicated the compile doesn't has any chance.
--------------------------------------------------------------------------
Now enough with all the drivel let's write our first program in C++, of course it is the obligatory "Hello Apolyton!" example.
Type out the following code, don't use copy and paste:
Save the file under test.cpp or whatever name you want.
If you are on a LINUX system, open a shell and compile the program on the command line like this:
g++ test.cpp -o test
To execute the program enter ./test
g++ is the GNU compiler, very common on UNIX/LINUX systems. And should also be available for windows, use Google to find one if you don't have any other compiler, for the start this should be enough, even if we don't have a public available version that compiles on a GNU compiler. Of course under windows you just have to enter the name of the program without extension from the command line to start the program.
If you have VC++ 6 you just can write your program in its integrated development environment and press Ctrl+F5 and a project file is created automatically and the program is compiled and after compiling it is executed. For .NET you have to create a project file manually.
#include is a preprocessor directive, that binds the standard library iostream into the program. Alternatively you can replace the angle brackets by quotation marks, but angel brackets are the usual way to include standard libraries.
main is the name of the function that serves as the entry point of the program, it is the first function of the program that is explicitly called. But don't think that contains the first piece of code that is executed, but that is a story for the advanced ones.
int is the return type of the main function, the function has to return anything of type int. In that case its value is always 0.
std::cout is defined in iostream and is an output stream it writes to the console/shell the string "Hello Apolyton!" if the program is executed from the command line.
The operator << writes the string "Hello Apolyton" to std::cout so that std::cout can write it onto the console/shell.
sdt is the name of a name space, I don't explain what a name space is at this place.
And note a semicolon completes each instruction.
----------------------------------------------------------------------
Let's talk about the workflow of compiling:
- Input: *.cpp and *.h files.
- For each *.cpp file:
-- Preprocessor: Include header files (*.h)
-- Compiler: Create *.obj files
- Linker:
-- Creates *.exe files from all *.obj files
-- Takes all the pieces from the used libraries
-Output *.exe file
Instead of *.cpp the extensions *.c and *.C are also common, note that LINUX has a case sensitive files system, therefore *.c and *.C are two different extensions there.
Alternatively for *.h *.hpp or in the case of the new standard libraries no extensions are in use.
Some compilers use *.o instead of *.obj, that is for instance the case for the GNU compiler.
*.h files are called header files. They are used to tell the *.cpp file to be compiled what other functionalities can be found in other *.obj files and libraries. Header files contain declarations.
OK next time I continue with preprocessor and declarations and definitions, just to mention some of the stuff to come.
-Martin
Of course first the obligatory motivation:
Why C++?
Here are some answers:
1. C++ is fast.
2. C++ is widespread.
3. C++ has a lot of language constructs.
4. C++ allows machine-oriented programming.
And of course the most important reason:
5. CTP2 is written in C++.
1. Well in spite of opposite messages C++ is still faster then e.g. comparable programs written in Java, but note bad written C++ might be slower then well written Java.
3. As the complexity of C++ is high we won't probably cover everything, fortunately this isn't necessary.
4. Machine-oriented programming is an advantage but also a disadvantage. The advantage is that the machine-orientation makes C++ efficient, the disadvantage is that it is more difficult to handle.
-------------------------------------------------------------------------
Some useful things to know about C++:
-It is an imperative programming language.
-Contains more then 60 reserved keyword, some of them with multiple meaning, that is context depending.
-The source code is translated (compiled) by a compile into an executable program.
Imperative programming language: Instructions are executed consecutively. The control flow is handled by instructions.
Compiling C++ programs takes often a lot of rime. JIT (just in time) compiling like Java is not possible. One reason for this is that C++ compiler use very good optimizer to make the resulting program to run as fast as possible. But note don't rely always on the optimizer, if you chose the wrong algorithm, or you make the code to complicated the compile doesn't has any chance.
--------------------------------------------------------------------------
Now enough with all the drivel let's write our first program in C++, of course it is the obligatory "Hello Apolyton!" example.
Type out the following code, don't use copy and paste:
Code:
#include <iostream> int main() { std::cout << "Hello Apolyton!"; return 0; }
If you are on a LINUX system, open a shell and compile the program on the command line like this:
g++ test.cpp -o test
To execute the program enter ./test
g++ is the GNU compiler, very common on UNIX/LINUX systems. And should also be available for windows, use Google to find one if you don't have any other compiler, for the start this should be enough, even if we don't have a public available version that compiles on a GNU compiler. Of course under windows you just have to enter the name of the program without extension from the command line to start the program.
If you have VC++ 6 you just can write your program in its integrated development environment and press Ctrl+F5 and a project file is created automatically and the program is compiled and after compiling it is executed. For .NET you have to create a project file manually.
#include is a preprocessor directive, that binds the standard library iostream into the program. Alternatively you can replace the angle brackets by quotation marks, but angel brackets are the usual way to include standard libraries.
main is the name of the function that serves as the entry point of the program, it is the first function of the program that is explicitly called. But don't think that contains the first piece of code that is executed, but that is a story for the advanced ones.
int is the return type of the main function, the function has to return anything of type int. In that case its value is always 0.
std::cout is defined in iostream and is an output stream it writes to the console/shell the string "Hello Apolyton!" if the program is executed from the command line.
The operator << writes the string "Hello Apolyton" to std::cout so that std::cout can write it onto the console/shell.
sdt is the name of a name space, I don't explain what a name space is at this place.
And note a semicolon completes each instruction.
----------------------------------------------------------------------
Let's talk about the workflow of compiling:
- Input: *.cpp and *.h files.
- For each *.cpp file:
-- Preprocessor: Include header files (*.h)
-- Compiler: Create *.obj files
- Linker:
-- Creates *.exe files from all *.obj files
-- Takes all the pieces from the used libraries
-Output *.exe file
Instead of *.cpp the extensions *.c and *.C are also common, note that LINUX has a case sensitive files system, therefore *.c and *.C are two different extensions there.
Alternatively for *.h *.hpp or in the case of the new standard libraries no extensions are in use.
Some compilers use *.o instead of *.obj, that is for instance the case for the GNU compiler.
*.h files are called header files. They are used to tell the *.cpp file to be compiled what other functionalities can be found in other *.obj files and libraries. Header files contain declarations.
OK next time I continue with preprocessor and declarations and definitions, just to mention some of the stuff to come.
-Martin
Comment