Basic Building Blocks of C++

Structure of a C++ Program



Atomic Data Types

Atomic data types are the built-in types defined by the C++ language This program will print the number of bytes used by each type - sizes may vary from system to system
 

Variables

Every variable has a: When a program is compiled, a symbol table is created, mapping each variable name to its type, size, and address

Naming Variables

A list of reserved keywords in C and C++ can be found in Deitel, pg. 75.

Declaring Variables

Declaration format: typeName variableName;
 int x;
 float y;
 int a, b, c;      // can also list several variables in one declaration 
                   //  statement, separated by commas
 int a=0, b=0, c;  // can also initialize atomic variables in 
                   //  declaration statements

Constant declaration format: const typename variablename = value;

(must initialize in same statement)

 const double PI = 3.14159;

Declaration Examples
 

Literals


Comments



Operators

Arithmetic Operators

Name Symbol Arity Usage
Add + binary x + y
Subtract - binary x - y
Multiply * binary x * y
Divide / binary x / y
Modulo % binary x % y
Minus - unary -x

Operator precedence


Increment and Decrement

  ++x;	// pre-increment (returns reference to new x)
  x++;	// post-increment (returns value of old x)
		// shortcuts for x = x + 1

  --x;	// pre-decrement
  x--;  // post-decrement
		// shortcuts for x = x - 1

Pre-increment: incrementing is done before the value of x is used in the rest of the expression
Post-increment: incrementing is done after the value of x is used in the rest of the expression
Note - this only matters if the variable is actually used in another expression. These two statements by themselves have the same effect:

 x++;
 ++x;
Examples
  int x = 5, count = 7;
  result = x * ++count;		// result = 40, count = 8

  int x = 5, count = 7;
  result = x * count++;		// result = 35, count = 8

Assignment Operator

Other Special short-cut assignment operators

  v += e;    means    v = v + e; 
  v -= e;    means    v = v - e; 
  v *= e;    means    v = v * e; 
  v /= e;    means    v = v / e; 
  v %= e;    means    v = v % e; 


Automatic Type Conversions

Explicit type conversions (casting)


Input and Output with streams in C++:

In C, we use functions found in stdio.h -- printf, scanf
In C++ we use different standard methods of i/o -- streams.

cout -- output stream object, of type ostream
cin -- input stream object, of type istream

To use these streams, we need to include a library called iostream into our programs

 #include <iostream.h>  // older style
 or
 #include <iostream>    // newer C++ style

cout is most frequently used with the insertion operator <<

 cout << "Hello World";
 cout << 'a';
 cout << x << y << z;

cin is most frequently used with the extraction operator >>

 cin >> x;
 cin >> a >> b >> c;

Note:  You cannot use literals on the right side of the extraction operator.  The cin statement should read data IN to a variable location.  The right side of the >> operator must specify a storage location.

Sample programs:

Note: In the above Hello, World examples, you will see a few differences, illustrating differences between the older and newer C++ styles. C++ was officially standardized in 1998, so some older code will not reflect some of the newer features. A few worth noting:
 

  • "int main()" vs "void main()"
    the newer style is to use int as the return type on the main function, which allows one program to receive a message of success or failure (with a given error code) from another calling program. This is commonly used, for example, in operating systems processes. Note also that the version with "int main()" has a return statement at the end, returning an integer value. In this context, typically 0 is used to report success, and a variety of negative values are used to report errors.

  •  
  • In the #include statement, <iostream.h> vs. <iostream>
    the older style of #include statements uses the exact name of the header file (with the .h extension). The newer style of #includes uses a different naming scheme, which is also extended to the older C libraries. This is described in Deitel starting on page 180.

  •  
  • The "using" statements
    there is a newer feature in the C++ language called namespaces, and the "using" statement helps take advantage of this. In these examples, the "using" statements are there to identify that the "cout" and "cin" words that we are using come from the "standard" namespace. Some older compilers (like Borland C++ 5.0) do not recognize or understand "using" statements and namespaces, as they pre-date the 1998 standardization.


  • While some compilers do not recognize some newer features, most newer compilers can handle the older styles, as well. For example, the g++ compiler will understand all of the above examples without any problem.

    Scope:

    The scope of a variable is the portion of the code where a variable is valid
    A global variable is declared outside of any compound blocks and is usable anywhere in the file from its point of declaration
    A variable declared within a block (i.e. a compound statement) has scope only within that block.
    C++ allows you declare variables anywhere within a program as long as you follow the "declare before use" rule.
    (C requires variable declarations at the beginning of a block).

     Scope Example