Software Engineers Handbook/Language Dictionary/Multi-paradigmed/C++

< Software Engineers Handbook < Language Dictionary < Multi-paradigmed

C++

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

Type

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

Execution Entry Point

An executable starts with the main() method.

General Syntax

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

 a = b;

Comments

 // 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

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

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 statment that a variable is a pointer, but a "*" is not syntactically used during invocation of a function that accepts a pointer as input.

Scope

Scope is defined by curly braces.

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

Conditional Statements

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

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

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

or

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

Containers

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

Algorithms

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

Garbage collection

Garbage collection is manual.

Physical Structure

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

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

Books and Articles

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.