C++ tutorial page 1:
This tutorial currently only has this first page as I haven't gotten around to making the rest of the pages yet- so this tutorial is definitely an "under construction" zone of the website for the time being.
Introduction:
________________
C++ is one of the most established, powerful and widely used computer languages as of 2009. C++ was originally based on C, which was invented by Dennis Ritchie in 1972 at Bell Laboratories and was used in writing the code for the Unix operating system [1].
C++, which builds on C, was invented by Bjarne Stroustrup at Bell Laboratories in 1978 and was originally named "C with classes" [2].
A C++ program is generated by translating written human code into an executable file by a C++ compiler. C++ code can be written in a text editor or a programming editor that aids in organization through colorizing and spacing.
An executable file, such as an ".exe" file can be directly read by a computer and is the final product of all C++ programs.
A compiler is a program that translates what a programmer has written into an executable file. Many companies, such as Microsoft and Borland have created C++ compilers for general use. A downloadable free version of Microsoft's Visual C++ Express 2008 can be found here.
Breaking up a program into multiple parts by using separate files is a common practice. A typical C++ program will have a file of the form "xxx.cpp" as it's main file and one or more "xxx.h" files that are called header files.
Header files include function prototypes (which are declarations of functions) and type definitions (that define types of varibles) that can be used in the main file because they have been declared and expressed in the header file.
In order for the main .cpp file to understand header file content, the main file must include what is called a preprocessor directive to include the header file. The code for this inclusion takes the form "#include <xxx.h>".
All computer programs are basically composed of Functions can take a set of variables, manipulate them, and return a result to the part of the program that called the function. For example, the function "addxy" could be sent the values of "x" and "y" and return their sum as some variable "z".
The best way to observe the concepts being discussed in reality is to look at a basic piece of code that constitues a fully functioning C++ program. The code is colored to represent various types of elements in C++. It should be noted that standard color coding is different than that which I am using on this site because the background is black. Consider the following line of code from a main C++ program:
Please keep in mind the color coding system used on the rest of this site to denote degrees of quality of words, people, etc. is not in effect for these computer science programming tutorials. The colors are used to stand out and colorize similar parts of the programming code or language.
| ex. 1 |
#include <iostream>
using namespace std;
main ()
{
cout << "Welcome";
return 0;
} |
In the above program, the bright green line [ ] is a comment that is ignored by the compiler because the double slash indicates that what follows is not code. The [ #include <iostream> ] line is called a preprocessor directive and instructs the compiler to include the functions included in the "iostream" library. The iostream library, among many other common libraries, contains input and output (or i/o) stream functions.
The [ using namespace std; ] line instructs the compiler to use the standard elements of the C++ library. All elements occur within a namespace and the standard library's namespace is called "std". This line is used in many modern C++ programs.
The line that reads [ main () ] instructs the processor to call the "main" function, the necessary initial function in all C++ programs. The open brace [ { ] on the next line tells the compiler that everything after it and before the next closed brace is the content of the [ main () ] function.
The line that reads [ cout << "Welcome"; ] tells the processer to use the [ cout ] object, which is a member of the [ iostream ] library, in order to output the string of characters to the right of the [ << ].
The quotes around [ Welcome ] denote that the interior of the quotes is to be output. The [ ; ] semicolon after [ "Welcome" ] is used to terminate all expressions in C++. If a semicolon is not used at the end of a C++ statement, then the compiler does not know where to end the statement and errors occur. This is one of the most common errors in C++.
The [ return 0; ] line tells the main function to end and return a value of 0, which, in the case of the main function, tells the computer that all went well with the program's execution. The closed brace [ } ] indicates the end of the [ main () ] function. In order to understand the syntax and organization of C++ programs, consider the following code:
| ex. 2 |
#include <iostream>
using namespace std; main (){ cout << "Welcome"; return 0; } |
The above program, example 2, is exactly the same program as the previous example in example 1. The difference is in the spacing and organization, not in the meaning. The compiler's output from progam example 1 and program example 2 is exactly the same.
References:
_______________
[1] The C Programming Language, Brian W. Kerningham and Dennis M. Ritchie, Prentice Hall, 1978
[2] C++ The Complete Reference Third Edition, Herbert Schildt, Osborne McGraw-Hill, 1998