C++ is an Multi-paradigmed programming language derived from C.

Type

edit

C++ is a full, Multi-paradigmed programming language implementing the following paradigmen: generic (template metaprogramming), imperative and object-oriented (class-based)

Execution Entry Point

edit

An executable starts with the main() method.

General Syntax

edit

The typical statement is completed by a semi-colon. For the assignment of b to a use:

 a = b;

Comments

edit
 // this is an inline comment.  Everything after the // is a comment.

Block comments are specified by a starting /* and ending */ They can span multiple lines.

 /*
  * this is a block comment 
  */

Variable Declarations

edit

Declarations can appear anywhere in the implementation.

Declare i as an integer:

 int i;

Here are two ways to declare i as an integer and give it an initial value of 0:

 int i = 0;
 int i(0);

Method Declaration/Implementation

edit

Methods are declared by specifying the interface for the method. It is often declared within the scope of a class.

 return_type function_name(argument_1_type arg_1_name, 
                           argument_2_type arg_2_name, 
                           default_argument_type default_arg_name = default_arg_value);

Method implementation repeats much of the same information

 return_type class_name::function_name(argument_1_type arg_1_name, 
                           argument_2_type arg_2_name, 
                           default_argument_type default_arg_name)
 {
     // work with arg_1_name, arg_2_name, and default_arg_name
     // depending on the argument types the variables are passed by 
     //   value, reference, or are constant
     // don't forget to return something of the return type
     return 36;
 }

IMPORTANT POINT A beginner in C/C++ might get confused about the distinction between how pointers are declared in a function prototype vs. how the function is defined. For example, if my_function does not return anything, but accepts one floating point input, its function prototype looks as follows:

 void my_function(float *some_variable);

However, if the variable my_var is defined to be a pointer to a float, for example:

 float *my_var;

Then the manner in which my_function is invoked is as follows:

 my_function(my_var);

Beginners sometimes get confused about the fact that a "*" is used to tell the compiler in a prototype statement that a variable is a pointer, but a "*" is not syntactically used during invocation of a function that accepts a pointer as input.

Scope

edit

Scope is defined by curly braces.

 { // this the beginning of a scope
     // the scope is about to end
 }

Conditional Statements

edit

If and only if A is equal to B assign C to D, otherwise, assign E to F.

 if( A == B )
 {
     D = C;
     // more code can be added here.  It is used if and only if A is equal to B
 }
 else
 {
     F = E;
     // more code can be added here.  It is used if and only if A is not equal to B
 }

or

 if( A == B ) 
     D = C; //more lines of code are not permitted after this statement
 else
     F = E;

Alternatively, a switch statement can be used for multiple choice operations. This sample converts a number input to text.

 switch( number_value )
 {
     case 37:
         text = "thirty-seven";
         break; // this line prevents the program from writing over this value with the
                //   following code
     case 23:
         text = "twenty-three";
         break;
     default: // this is used if none of the previous cases contain the value
         text = "unknown number";
 }

Looping Statements

edit

This code counts from 0 to 9, adding up the contents of the array.

 int i = 0;
 for( int index = 0; index < 10; index = index + 1 )
 {
     i = array[index];
 }

This code repeats until the number 4 is found. If this runs off of the end of the array, there could be a problem.

 int index = 0;
 while( 4 != array[index] )
 {
     index = index + 1;
 }

This code increments the counter before the check is made, so that it starts with element 1.

 int index = 0;
 do
 {
     index = index + 1;
 }
 while( 4 != array[index] );

Output Statements

edit
 printf( "%s","Hello world!\n" );

or

 std::cout << "Hello world!" << std::endl;

Containers

edit

The standard template library has a variety of containers including vector, bit-set, list, map and multi-set.

Algorithms

edit

The standard template library has a variety of algorithms including sort, remove_if, and find_if.

Garbage collection

edit

Garbage collection is manual.

Physical Structure

edit

Generally the interfaces are defined in header files, often *.h. The implementation files are often named *.cpp. Useful collections of classes can be compiled into libraries, often *.dll, *.a, or *.so, which can be compiled into executables (statically linked) or used on the fly (dynamically linked).

Tips

edit

Don't confuse these two:

 =  // assignment
 == // comparison, is equal to

Often using the one you don't want will compile, and will produce results you did not expect.

A good practice is to write; if(CONSTANT == variable) rather than if(variable == CONSTANT) since the compiler will catch; if(CONSTANT = variable) but not if(variable = CONSTANT).

Arrays start with index 0.

Web References

edit

Books and Articles

edit
  • Bruce Eckel, Thinking in C++, Volume 1: Introduction to Standard C++ ISBN 0139798099. It has helped many people make the leap from C to C++. It is available for free online at Bruce Eckel's site.
  • Nicolai M. Josuttis, The C++ Standard Library : A Tutorial and Reference ISBN 0201379260. It contains lengthy and advanced discussions on the standard template library conatiners and algorithms.
  • Stanley B. Lippman, Josée Lajoie & Barbara E. Moo, C++ Primer ISBN 0321714113. This is a very thorough introduction to C++, taking nearly 1000 pages to introduce programmers to C++.
  • Andrew Koenig & Barbara E. Moo, Accelerated C++ ISBN 020170353X. This is another introduction to C++, but stresses the use of the STL. It is a quite compact introduction, with a steeper learning curve better suited for those who already know another programming language.
  • Scott Meyers, Effective C++ ISBN 0321334876, More Effective C++ ISBN 020163371X, Effective STL ISBN 0201749629. Meyers' series of books contain the best C++ practices, from class design to minimising compilation dependencies.
  • Herbert Schildt, C++: The Complete Reference ISBN 0072226803. It contains dictionary-like listings of code syntax and usage. The structure does not make it useful to learn the language from scratch. However, it could be very useful when switching from a similar language.
  • Bjarne Stroustrup, The C++ Programming Language ISBN 0201889544. Written by the creator of C++, it covers everything from the core language to the standard library. The book itself has over 1300 pages.