CSCI 1300
Lecture Notes


9/4/97

Input/Output Expressions
  • I/O Streams
  • Input Expressions (using cin)
  • Output Expressions (using cout)
  • Functions
  • Defining a Function
  • Scope Rules
  • Classes
  • Classes


  • Input/Output Expressions

    Getting information into and out of the computer.

    Increases flexibility of the program because you don't have to rewrite it for each set of data - instead, you get it from the user each time.


    I/O Streams

  • Abstract away the details of the hardware
  • istream gets inputs from keyboard
  • ostream puts outputs to display


    Input Expressions (using cin)

  • Gets input from the user and places it in the specified variable.
  • Example:
    cin >> radius;
    cin >> rows >> cols;
  • Inputs are delineated by white space (spaces, tabs, and newlines).
  • cin waits for all inputs before allowing the program to proceed.
  • Evaluated left to right
  • Types must match
  • Example:
    Given the statement:
    cin >> rows >> cols;

    If the user types: 25 37
    Then rows will be 25 and cols will be 37.

    If the user types: 2,3
    What will happen?


    Output Expressions (using cout)

  • Displays values on the screen
  • Example
    cout << "This is a test\n";
  • Evaluated left to right
  • Types are automatically displayed properly.
  • Example:
    char name[] = "Scott A. Brandt";
    int age = 32;
    float weight = 230.5;

    cout << name << ", Age: " << age << ", Weight: " << weight << "\n";

    Prints out:
    Scott A. Brandt, Age: 32, Weight: 230.5



    Functions

  • List of expressions that are executed by calling the function.
  • Implements some piece of the overall program.
  • May be called from several places in the program.
  • Reusability, Readability, Modularity, Modifiability, ...
  • Example: Computing the volume of a sphere (with functions)
  • Describe flow of control


    Defining a function

  • Functions take parameters (comma seperated list)
  • Functions return one value
  • Functions have a type
  • This is the type of the value returned
  • May be void if no value is to be returned
  • Defaults to int
  • Functions should end with "return(whatever);"
  • Functions can appear anywhere any other expression can appear.
  • Example: x = (pow(y, 2) * 2) + (3/sqrt(y));

    Example: FahrToCelsius.cc


    Scope rules

  • Scope rules define where a variable is visible and usable
  • Global variables (outside any function)
  • Visible everywhere
  • Value remains until changed or program exits
  • Local variables
  • Visible only in the function in which they are defined
  • Value remains until function returns
  • Next time function is called, the value is no longer there
  • Local variables eclipse globals with the same name



    Classes


    Classes

  • Classes are complex data types
  • Complex data types represent some unit of data
  • Classes have:
  • Data Members or Fields that contain individual pieces of data within the class
  • Member Functions that operate on the data
  • Public and private sections defining what is visible where
  • Public data members and member functions can be accessed by any part of the program.
  • Private data members and member functions can only be accessed by member functions of the class itself.
  • Classes are used like fundamental data types
  • Class members are accessed with id.member
  • Examples: Computing the volume of a sphere (with classes)