CS 2005 - Notes for students transitioning from CS 1101 to CS 2005

Background

In CS 1101 you wrote programs using DrScheme. Scheme is a functional programming language, that is, it views every task in terms of functions. A function transforms one or more arguments into a single value. In Scheme, the transformation is specified with a (define...), and combinations of transformations are allowed by nesting defined and primitive operators.

In CS 2005 we will be using the programming language C++. We will start by studying the C-language subset of C++ that is defined using the procedural or imperative paradigm. In a procedural language, a program consists of a sequence of statements that transform data by changing the contents of memory cells. In Scheme, the (set!...) operator was used only in specific circumstances. In C, changing the contents of memory by setting variables to new values is at the heart of every programming problem.

We will later expand the C subset to include C++'s object-oriented capabilities. The fundamental idea behind an object-oriented language is to define both data and the functions that operate on that data as a single unit called a class. An instance of a class is called an object. An object-oriented program typically consists of objects that communicate with each other by calling each other's member functions.

Examples: converting Scheme functions to C++ functions

Here are examples of tasks similar to ones you wrote in Scheme for CS 1101, and the corresponding tasks written in C++. We will cover additional examples in the first few class meetings in B-term.

Executing a C++ program

Let's look at how functions are executed in Scheme and in C++. In Scheme's Interactions Window the function is-a-vowel? is applied to actual arguments and the result is immediately calculated and displayed:
> (is-a-vowel? 'g)
false
Executing a function in C++ is a more complicated process. That is because C++ is a compiled language rather than an interpreted language. In a compiled language, a source file containing both the function definition and the function call is translated and the resulting "object code" is stored in an "object file". This object file can then be loaded into memory and executed. Here is a complete C++ program that defines two functions, is_a_vowel (described above) and main, whose purpose is to call is_a_vowel on user-specified input, and then display a message based on the result of the function call. (The mechanics of entering, compiling, and running a C++ program on the Unix system will be covered in the first lab in CS 2005.)
// Program reads in a character entered at the keyboard, and displays a
// message indicating whether or not the character is a vowel

#include <iostream>              //the cin and cout statements used below are
                                 //defined in the library iostream
using namespace std;             

bool is_a_vowel (char letter);   //this is the function prototype
                                 //it serves a purpose similar to that
                                 //of the contracts you wrote in Scheme

int main()                       //every C++ program begins execution
{                                //in the function main()

  char letterToTest;             //creates a variable to hold the user's input

  // prompt the user for input, and read the character typed by the user into letterToTest
  cout << "Which letter do you want to test?  ";
  cin >> letterToTest;

  // call the function to determine if letterToTest is a vowel or not,
  // and print an appropriate message
  if (is_a_vowel(letterToTest))
     cout << "That letter is a vowel" << endl;
  else
     cout << "That letter is not a vowel" << endl;

  // terminate the main function
  return 0;
}

//PRE:  letter is a char
//POST: the function returns true if letter is one of 'a', 'e', 'i', 'o', 'u',
//      and returns false otherwise
bool is_a_vowel(char letter)
{
  return (letter == 'a' || 
          letter == 'e' || 
          letter == 'i' || 
          letter == 'o' ||
          letter == 'u');
}
If we were to compile and execute this program from a Unix terminal, we would see this interaction:
Which letter do you want to test?  g
That letter is not a vowel

Further reading

I have placed on reserve in the library several copies of the textbook Applications Programming in C++, by Richard Johnsonbaugh and Martin Kalin. These sections in the textbook cover the material you should know about C++ as you begin CS 2005: