White Bar

A Review of C++
Procedural Paradigm

Identifiers | Data Types | ASCII | Constants | Variables | Operators | Expressions | Statements | Arrays | Simple I/O | Std Lib | Böhm & Jacopini | Functions | Strings | Recursion

Identifiers - Names given to functions and variables not predefined in C++.
Rules to Identifiers:
  • Sequence of letters, digits, and underscores
  • One or more characters in length
  • A digit cannot be used as the beginning character of an identifier.
C++ reserves certain identifiers that may not be used (Reserved List). Also, all double underscore (__myFunction) and an underscore followed by a uppercase letter (_MyArray) are reserved. Identifiers are case sensitive.

There are two types of identifiers, external and internal.
  • External identifiers are names given to functions because they are accessed by other files externally.
  • Internal identifiers are names given to local variables. These are only accessed from inside the function in which they are declared.


Data Types -


Specifies the type of content to be stored in a constant or variable.
Such types are:
char : 'B'
int : 72
float : 3.1415926
bool : (true/false)
double : 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482
void : no value


ASCII -


The character data type, char, is represented with its ASCII value. ASCII is a standard correlating characters to an interger value. ASCII Table


Constants -


Identifier whose value does not change throughout the execution of the program.
Declaration is as such:

const int MY_INT = 1000;


Variables -


Identifier whose value may change repeatedly during the execution of the program.
Declaration is as such:

int myInt = 934;


Operators -


Operators are broken into three groups; Arithmetic, Logical and Relational, and Bitwise Operators. The operators are in order from highest to lowest precedence, starting at the top.
  • Arithmetic:
  • ++
    --
    *
    /
    %
    +
    -
    Increment
    Decrement
    Multiplication
    Division
    Modulus (returns the remainder)
    Addition
    Subtraction

  • Logical and
    Relational:


  • !
    >
    >=
    <
    <=
    ==
    !=
    &&
    ||


    Not
    Greater than
    Greater than or equal to
    Less than
    Less than or equal to
    Equal to
    Not equal to
    And
    Or

  • Bitwise:

  • ~
    >>
    <<
    &
    ^
    |

    One's complement
    Right shift
    Left shift
    And
    Xor
    Or


    Expressions -


    Expressions take variables, literals, and operators and combine them into pieces of a program.
    Below are examples of expressions:

    (x + y)/ 100

    z <= w


    Statements -


    Statements are complete units that can be executed. They can be broken up into three types:
  • Expression:
  • Expressions that make up statements.
    myInt = 794;
    ++counter;
  • Declaration:
  • A declaration of a variable or constant.
    const float myFloat = 99.3;
  • Control flow:
  • Control which way the program will proceed. If statements and for loops are good examples of control flow statements.


    Arrays -


    Example declaration:

    int myArray[100];

    Arrays can be declared using any type. The above declaration allows for an array of 100 elements, in this case integers, which are accessed by using the subscript operator, [ ]. The indexing of an array starts at 0, therefore, myArray[67], would access the 68th element in the array. Arrays can be initialized by using the curly braces in the declartion.

    char myArray[5] = {'a', 'b', 'c', 'd', 'e'}


    Simple I/O -


    The << and >> operators have been overloaded to handle input and output in C++. The << operator ouputs data to the I/O stream while the >> operator inputs from the I/O stream.
    Examples:

    cin >> myFloat;
    cout << "This is a float: " << myFloat;


    Standard
    Libraries -


    These libraries contain many of the predefined function of C++, like the I/O functions and math functions.
    This is a reference to some of the libraries found in C++:

    http://www.cppreference.com/


    Böhm and Jacopini
    structures -


    Böhm and Jacopini proved that there is a need for only three control structures when writing a program.
  • Sequence:
  • One step is executed followed by another.
  • Selection:
  • According to the status of the expression, the program will follow a certain path.
  • Repetiton:
  • Iterative loops such as a while.

    Structured Programming: Along with the control structures above, a program must have good structure. Producing human understanding is the key to good structure.

    Single-Entry, Single-Exit: Becuase humans read linearly, the program should execute as such. This is accomplished with a single-entry and a single-exit program.


    Functions -


    Functions make up the program. Each having there separate actions to preform, combining them produces a "functional" program. Each function has a parameter list, the elements being passed into the function from outside, and a return type, or the data type of the element to be returned from the function.

    Function Prototypes: This declares the function at the beginning of the program. The prototype looks exactly like the first line of the function, followed by a semi-colon.
    Example:

    int myFunction (int theNum, float theFloat);

    Scope: Every function and variable has a scope or bounds which it can work within. If a variable is declared locally to a function, it can only be used inside that function.

    Pass By Value and Reference: Every function and variable has a scope or bounds which it can work within. If a variable is declared locally to a function, it can only be used inside that function.

    Function Overloading: overloaded functions have the same name, but a different parameter list and/or return type. When this type of function is called, the complier decides which function is to be called out of the overloaded ones.


    Strings -


    C-strings: C-strings are character arrays with a NULL character at the end to signal the end of the string. There is a library in C++ that contains functions that can be executed on C-strings.
    ( C-Strings Library References)

    String Objects: A string class has also been introduced into C++. This class adds different functions to the C-string library.
    ( String Class Library References)


    Recursion -


    A resursive function is a function that calls itself. The programmer specifies a basis case and the function will continue to run, calling itself, until that basis case is found. Everytime the function is called a new set of parameters and variables are allocated memory, thus recursion keeps track of values at different levels of the recursive loop.

     

    Back