CSCI 1300
Lecture Notes


9/2/97

C++
  • C++ Program
  • Parts of a Program
  • Declarations
  • What is Data?
  • C++ Data Types
  • Identifiers
  • Declaration
  • Named Constants
  • Variables
  • Variable Initialization
  • Numerical Expressions
  • Basic Mathematical Operations
  • Precedence and Associativity
  • Unary Operators
  • Other Operators
  • Type Conversion
  • Math Library
  • Assignment Expressions
  • Shortcuts
  • NOTES


  • C++


    C++ Program

  • A list of C++ statements.
  • Statements executed in order.
  • Statements terminated with a semicolon (;).
  • May contain classes, functions, variables, etc.
  • Simplest correct program (does nothing, but is correct).

    main()
    {

    }


    Parts of a program

  • Comments
  • Information for programmers about the program
  • Not compiled
  • Include files
  • Additional files to include in the program
  • Usually contain only definitions and prototypes, not code
  • Often system interface files
  • Prototypes
  • Short definitions of functions defined elsewhere.
  • Class definitions
  • Define classes used in the program
  • Do not allocate storage - must be done separately
  • Global variable declarations
  • Variables accessible to all functions in program
  • Considered bad practice
  • Can be used for constants used all over the program
  • Functions
  • The parts that do something
  • Can be called by other functions
  • The main() function
  • The main part of the program
  • Executed first
  • Calls other functions as necessary
  • Each C++ program must contain exactly one main() function.
  • Example: Calculating volume of a sphere.



    Declarations: Types of Data Objects


    What is data?

  • Characters
  • Letters ('a', 'b', 'c')
  • Digits ('0', '1', '2')
  • Symbols (';', '$', '&')
  • Punctuation ('.', ',', '!')
  • Numbers
  • Whole numbers (1, 3000, 102456789)
  • Negative whole numbers (-1, -3000, -128y234)
  • Numbers with a decimal point (.01, 3.7, 12357.213456)
  • Combinations of the above
  • Booleans (represented as a numeric value, 0=FALSE, 1=TRUE)
  • Imaginary numbers (two parts: real and imaginary)
  • Bank accounts
  • Database records
  • ...


    C++ Data Types

  • Character
  • Denoted with "char"
  • Single character
  • Used to represent a-z, A-Z, numbers, and symbols
  • Uses 8 bits to represent 127 or 256 unique characters
  • ASCII standard (see appendix A)
  • Special characters '\'', '\n', '\010', etc. (see table 3.2)
  • '3' = '\063' and 'A' = '\101'
  • Example:
    char middle_initial;
    middle_initial = 'A';
  • String
  • Null terminated list of chars
  • Denoted with "char []"
  • Not really a fundamental data type, but used so often that people think of it as one
  • Uses 8 bits * number of chars.
  • Example: char name[] = "Scott Brandt";
    or
    char name[80];
    strcpy(name, "Scott Brandt");
  • Integer
  • Whole positive or negative number
  • Denoted with "int", "short" or "short int", "long" or "long int"
  • Positive-only denoted with "unsigned" before "int" or "short" or "long"
  • Ints are usually 16 bits long
  • Shorts are usually 16 bits long, <= size of int
  • Longs are usually 32 bits long, >= size of int
  • Exact sizes are machine dependent
  • Example:
    int age;
    age = 32;
  • Real
  • Denoted with "float", "double", or "long double"
  • 32, 64, and 96 or 128 bit real values
  • Example:
    float pi;
    pi = 3.14159;


  • Identifiers

  • An identifier is a name for a data object.
  • Should begin with a letter (a-z, A-Z)
  • May contain letters, digits and underscores
  • Should relate to the intended use of the data object
  • Examples:
    float radius;
    char first_name[5];
    int time1, time2;
  • Case matters - radius and Radius are not the same identifier


    Declarations

  • Tell the compiler the type and identifier for a data object
  • Must occur before the first use of the object.
  • Examples: See above


    Named constants

  • Named data objects whose values do not (and should not)change
  • Declared with "const"
  • Used for
  • Mathematical constants
    const float pi = 3.14159;
    const double e = 2.71828182846;
  • Things that don't change during the execution of the program
    const char errorstring[] = "Fatal error";
  • Can be used with any other data type
  • Improves readability
  • Everyone either knows what pi is, or can find out
  • Fewer people know that 3.141592653589793 is pi
  • Improves reliability
  • Only defined in one place
  • Especially when the same constant is reused in many places throughout the program.
  • Improves modifiability
  • One change rather than many.
  • Don't need to search to make sure you have found all of the places that the constant occurs.

  • Variables

  • Variable = Simple Data Object
  • Variable names are identifiers
  • Before using a variable, it must be declared.
  • This tells the compiler what type of data it will/can contain.
  • The type is enforced by the compiler.
  • Variables are associated with the memory location that stores the actual data.
  • Examples: All of the above


    Variable Initialization

  • Initial values may be assigned to variables.
  • Examples
    int i = 0, j = 0;
    double foo = 23456.1235;



    Numerical Expressions


    Basic mathematical operations (+, -, *, /, %)

    Addition (+)

  • Adds two numbers

    Subtraction (-)

  • Subtracts two numbers

    Multiplication (*)

  • Multiplies two numbers

    Division (/)

  • Divides two numbers

    Modulus (%)

  • Remainder of integer division
  • Example: 5 % 2 = 1

    Type of result will be that of the greater of the two numerical types.
    Examples:

    3 / 2 = 1
    3.0/2 = 1.5


    Precedence and Associativity

  • Order of mathematical operations are important
  • Example: (3+2)*4 = 5*4 = 20 but 3+(2*4) = 3 + 8 = 11
  • * and / evaluated before + and -
  • Example: 3+2*4 evaluated as 3+(2*4)
  • Precendences equal => evaluate from left to right
  • Example: 3+2+5 = 5 + 5 = 10 and 3*2/5 = 6/5 = 1
  • Parentheses enforce evaluation order
  • Example: (3+2)*4 = 5*4 = 20


    Unary operators (+, -)

  • -3, +17 allowed
  • Higher precedence than binary operators (above)
  • Example: 4*-3 = -12


    Other operators (~, &, |, ^, <<, >>, &&, ||)

  • ~ = negation
  • & = bitwise and
  • | = bitwise or
  • ^ = bitwise exclusive or
  • << = bitshift left
  • >> = bitshift right
  • && = logical and
  • || = logical or

    See appendix C for a list of all operators and their precedences


    Type conversion

  • A variable or value of one numeric type may be converted to another type by casting the type.
  • Some information may be lost if converting to a smaller type.
  • Example
    double value = 123.456;
    int int_value;
    int_value = int(value); // int_value = 123
  • May also be done implicitly
    int_value = value;
  • It's clearer to make it explicit


    Math library

    sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), sinh(x), cosh(x), tanh(x), exp(x), log(x), log10(x), pow(x,y), sqrt(x), ceil(x), floor(x)

  • Parameters are doubles, returns doubles
  • Example:
    double x, sqrt_of_x;
    x = 4.0;
    sqrt_of_x = sqrt(x); // sqrt_of_x = 2


    Assignment Expressions

  • Assigns a value to a variable
  • variable = expression
  • The value can be a numeric constant, another variable, or the result of an expression.
  • Types must be compatible (e.g. you can't assign a string to a float)
  • Puts a value in the location associated with the variable
  • Examples
    x = 4;
    y = 3 + 7.5;
    z = x * y / 4;
    c = 't';
  • Errors
    5 = x;
    x + 7 = 3;
    c = "ABC";
    x = "123" + "456";
  • Returns value assigned to variable
  • Example
    x = 5 + 4;
    x = y = 5 + 4; // what is x? why?
  • Variable can be on both sides of "="
  • Example
    x = 5;
    x = x + 1;


    Shortcuts

    ++

  • ++x is equivalent to x = x+1;
  • ++x evaluated before it is used
  • Example
    x = 7;
    y = ++x; // x = 8, y = 8
  • x++ is evaluated after it is used
  • Example
    x = 7;
    y = x++; // x = 8, y = 7

    --

  • --x is equivalent to x = x-1
  • Works just like ++, but with subtraction instead of addition

    +=, -=, *=, /=, %=

  • x ?= y is the same as x = x ? y
  • Example
    x = 4;
    x *= 5; // x = 20


    Notes

  • All expressions must be terminated with a semicolon (;)
  • The value of a variable is undefined until is has been assigned.
  • A common error is to use a variable before a value has been assigned.