History of the C++ language

JAN29


History of the C++ language
 
In this first C++ programming language tutorial we are going to look at the history of the C++ language.
The C programming language was devised in the early 1970s by Dennis M. Ritchie an employee from Bell Labs (AT&T).
       Many other programming languages are derived from the C language. Some did well and some did not. The languages Objective-C and C++ for instance are derived from the C language.  Both languages add the “object oriented” element to the language C. One of the most recent languages, that used much of the C language, is Java.


        The programming language C++ (originally named “C with Classes”) was devised by Bjarne Stroustrup also an employee from Bell Labs (AT&T). Stroustrup started working on C with Classes in 1979.
The idea of creating a new language originated from a wish, to do things, that were not possible with other languages. He had experience with the language Simula and BCPL (Simula is a slow language, but it had some features that were very helpful for large software development projects. BCPL was to low-level). So he chose to enhance the C language with Simula-like features, because the C language was fast and portable. Stroustrup did not only use features from Simula but also borrowed features from the languages Ada, CLU, ALGOL 68 and ML.

        In 1983, the name of the language was changed from C with Classes to C++. (The ++ is C language operator). The first commercial release of the C++ language was in October of 1985. At the same time, the first edition of the book “The C++ Programming Language” was released. (Keep in mind that there was no official standard at that time. So the book became an important reference to the language). In 1989, version 2.0 was released of the C++ language.

        In 1990, “The Annotated C++ Reference Manual” was published. This work became the basis for the future standard. (Keep in mind that there were additions to the language after 1990). In 1998, a joint ANSI-ISO committee standardized the C++ language.

        As you can see it took a long time (almost 20 years) to come to a standardized version of the language. In 2003 the committee published a corrected version of the C++ standard. The last addition was in 2005. (The last addition is not part of the standard, it is a so called “Library Technical Report”. (TR1 for short).


continue reading

C++ Compilers [GNU and Visual Studio]

JAN29


C++ Compilers [GNU and Visual Studio]

A compiler is a program that translates one language (high level) into another language (e.g., assembly language or machine specific language). A compiler translates source code (plain text) into object code (normally in a form suitable for processing by other programs (like a linker)). The most common reason for wanting to translate source code is to create an executable program.
After the compiler translates the source code in object code, the object(‘s) have to be linked into an executable. This is done by a program called a linker (in most cases the compile stage and link stage are done automatically. It is also possible to do these stages separately).
There are many different compilers available. Some compilers are operating system specific other can be used on different platforms. In the following two sections we take a look at two compilers: GNU compiler and Visual Studio.

GNU Compiler:

The GNU Compiler Collection (GCC) includes front ends for C, C++ , Objective-C, etc., as well as libraries for these languages. GCC can be used on many different platforms, but is commonly used on Unix and Linux.
If you want to use GCC on a windows machine you should take a look at Cygwin. It is also possible to install a Linux partition besides a Windows partition (multi boot system). We recommend you use Ubuntu distribution for this.
You can use your favorite text editor to write your programs in. After writing your program you can use the following command to compile the program:
For c programs:
# gcc -o <output name> <your-source.c>
For c++ programs:
# g++ <your-source.cpp> -o <output name>
On some systems, g++ is also installed with the name c++.
It is also possible to compile C++ programs with gcc, however the use of gcc does not add the C++ library.
g++ is a program that calls GCC
and treats .c, .h and .i files as C++ source files instead of C source files
unless -x is used, and automatically specifies linking against the C++ library.

Visual Studio 2005/2008 (Express edition):

Visual studio is the developer studio from Microsoft. It provides a complete set of development tools to create windows programs in many different languages (like visual basic, visual c++, etc.). The complete developer studio is not free. But it is possible to download an express edition of Visual Studio C++ 2005 or 2008. (You have to register).
If you want to use Visual Studio Express for compiling win32 (console) applications you have to install the platform SDK as well. A complete instruction on how to install can be found here. Note: the tutorials on this site are win32 console applications.
After you installed Visual Studio C++ and the platform SDK you can start your project.
To set-up a win32 console application do the following:
  • File, New, Project
  • Project types : Visual C++, win32
  • Templates : Win32 Console Application
  • Give the project a name and press OK and then click next.
  • Check Console Application and Empty project by Application settings.
  • Click finish.
An empty project is now made. To add a new source file do the following:
  • In the solution explorer select Sources files and right click on it.
  • Add, New item, Templates: C++ file (.cpp), Name the file and press add.
  • File, Save all.
The last thing to do is to set some Project properties:
  • Project, Properties….
  • Select General
  • Set Character from “Use Unicode Character Set” to “Use Multi-Byte Character Set”.
  • Exit with OK.
You are now ready to program your first program. After writing your program you can compile it by pressing F7.

Important:

The examples included in the C and C++ tutorials are all console programs. That means they use text to communicate. All compilers support the compilation of console programs. Check the user’s manual of your compiler for more info on how to compile them. (It is not doable for us to write this down for every compiler).

continue reading

C++ First Program by Hello World

JAN29


C++ First Program, Hello World


 
// A hello world program in C++
 
#include<iostream>
 
using namespace std;
 
int main()
{
    cout << "Hello World!";
    return 0;
} 
 
We start the tutorial series with one of the simplest programs that can be written in the C++ language.
The hello world program is one of the simplest programs, but it already contains the fundamental components that every C++ program has. Don’t be overwhelmed, we will take a look at the code line by line.
Type the code in your favorite editor (always type, don’t use cut/paste. This is better for learning purposes).
Save the program with the name: hello.cpp or C2Hari.CPP.
After saving the source code you can compile it. It should compile without any errors.
Note: Remember, the examples included in the C and C++ tutorials are all console programs. That means they use text to communicate. All compilers support the compilation of console programs. Check the user manual of you compiler for more information on how to compile them.
(It is not doable for us to write this down for every compiler).

// A hello world program in C++

The first line in our program is a comment line. Every line that starts with two slash signs ( // ) are considered comments and will have no effect on the behavior or outcome of the program. (Words between /* and */ will also be considered as comments (old style comments)). Use comments in your programs to explain difficult sections, but don’t overdo it. It is also common practice to start every program with a brief description on what the program will do.

#include<iostream>

Lines beginning with a pound sign (#) are used by the compilers pre-processor. In this case the directive #include tells the pre-processor to include the iostream standard file. This file iostream includes the declarations of the basic standard input/output library in C++. (See it as including extra lines of code that add functionality to your program).

using namespace std;

All the elements of the standard C++ library are declared within what is called a namespace. In this case the namespace with the name std. We put this line in to declare that we will make use of the functionality offered in the namespace std. This line of code is used very frequent in C++ programs that use the standard library. You will see that we will make use of it in most of the source code included in these tutorials.

int main()

int is what is called the return value (in this case of the type integer. Integer is a whole number). What is used for will be explained further down.
Every program must have a main() function. The main function is the point where all C++ programs start their execution. The word main is followed by a pair of round brackets. That is because it is a function declaration (Functions will be explained in more detail in a later tutorial). It is possible to enclose a list of parameters within the round brackets.

{}

The two curly brackets (one in the beginning and one at the end) are used to indicate the beginning and the end of the function main. (Also called the body of a function). Everything contained within these curly brackets is what the function does when it is called and executed. In the coming tutorials you will see that many other statements make use of curly brackets.

cout << “Hello World”;

This line is a C++ statement. A statement is a simple expression that can produce an effect. In this case the statement will print something to our screen.
cout represents the standard output stream in C++.
(There is also a standard input stream that will be explained in another tutorial). In this a sequence of characters (Hello World) will be send to the standard output stream (in most cases this will be your screen).
The words Hello World have to be between ” “, but the ” ” will not be printed on the screen. They indicate that the sentence begins and where it will end.
cout is declared in the iostream standard file within the std namespace. This is the reason why we needed to include that specific file. This is also the reason why we had to declare this specific namespace.
As you can see the statement ends with a semicolon (;). The semicolon is used to mark the end of the statement. The semicolon must be placed behind all statements in C++ programs. So, remember this. One of the common errors is to forget to include a semicolon after a statement.

return 0;

The return statement causes the main function to finish. You may return a return code (in this case a zero) but it depends on how you start your function main( ). We said that main ( ) will return an int (integer). So we have to return something (in this case a zero). A zero normally indicates that everything went ok and a one normally indicates that something has gone wrong. (This is standard practice. After running an UNIX/Linux program there is often a check on the return code).

Indentations

As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. In a program as Hello World, it seems a stupid thing to do. But as the programs become more complex, you will see that it makes the code more readable. (Also you will make fewer errors, like forgetting a curly bracket on the end of a function).

continue reading

C++ Escape code & Constants & Strings Codes

JAN29


C++ constants, escape codes and strings

1.Escape Codes :

                Escape codes are used to represent characters that are difficult to express otherwise in the source code.  For instance a tab (\t). Escape codes all start with a backslash (\).


\n
Newline
\r
carriage
return
\t
Tab
\v
vertical
tab
\b
Backspace
\f
form feed
(page feed)
\a
alert
(beep)
\’
single quote (‘)
\”
double quote (“)
\?
question mark (?)
\\
backslash (\)
Escape codes can be used like so:
 
        "jane\n"
 
        "jane\t doe"
 
        etc.
 
Escape codes can also be used to express octal (base-8) or hexadecimal (base-16) numbers. An octal number can be used like this: \10 (backslash followed by a number.)
A hexadecimal number can be used like this: \xF0 (a backslash followed by an x and the number.)
It is also possible to define an escape code.
For example:
 
 
        #include<iostream>
 
        using namespace std;
 
        #define PI 3.14159     // note (1)
 
        #define NEW_LINE '\n';         // note (2)
 
        int main ()
 
        {
 
               float my_var=5.0;
 
               my_var = my_var * 2; 
 
               cout << my_var;
 
               cout << NEW_LINE; 
 
               return 0;
 
        }
 
Note: (1) The #define directive is not a C++ statement but a directive for the pre-processor. It assumes the entire line is the directive and does not require a semicolon.
(2) If a semicolon character ( ; ) is appended at the end of the define statement, the pre-processor will replace all NEW_LINE for \n.

2.Constants:

The difference between variables and constants is that variables can change their value at any time but constants can never change their value. (The constants value is locked for the duration of the program.)
Constants can be very useful, PI for instance is a good example to declare as a constant.

Defining a constants:

With the pre-processor directive “define” it is possible to define your own constants.
The format of the pre-processor directive #define is:
#define identifier value
Take a look at an example:
 
        #define PI 3.14
 
        #define A 5

Consent Prefix:

The const prefix can be used to declare a constant of a specific type.
(The same way as you would declare any other variable.)
Take a look at the example:
 
        const int x = 50;
 
        const char newline = '\n';
 
        const y = 50;
 
Note: If no data type is presented (last example) the compiler will assume a data type!
In this case it will assume that the data type is an integer (int).

3.Strings:

Introduction to Strings:

In this paragraph we take a brief look at strings. In a later tutorial we will take a closer look at strings.
Variables that can store more then a single character are known as strings.
The C++ Standard Template Library (STL) contains a string class that has to be included before you can make use of strings. Also you need to have access to the standard namespace.
Take a look at an example:
 
 
  #include<iostream>
 
  #include<string> //add for std::string
 
  using namespace std;
 
  int main()
 
  {
 
               string mystring = "This is a string";
 
               cout << mystring;
 
               return 0;
 
  }
 
Note: Instead of “string mystring” you could also say “std::string mystring” if you don’t want to include using namespace std. Instead of mystring = “This is a string”; it is also possible to make use of parentheses: mystring = (“This is a string”);
Strings can also perform all the other basic operations that fundamental data types can. For instance, being declared without an initial value and being assigned values during execution.
Take a look at the following example:
 
 
        #include<iostream>
 
        #include<string>
 
        using namespace std;
 
        int main ()
 
        {
 
               string mystring;
 
               mystring = "First value";
 
               cout << mystring << endl;
 
               mystring = "Second value";
 
               cout << mystring << endl;
 
               return 0;
 
        }
 
That’s all for this tutorial. In the next tutorial we will look at operators.

continue reading