next up previous contents index
Next: Classes Up: C++ Previous: C++   Contents   Index


A C++ program

Most C compilers available can also compile C++ programs - on Windows, using the Borland compiler, bcc32 is also used for C++ programs, while on Unix, the compiler is usually called g++. A popular extension for C++ programs is .cpp.

C++ includes as a subset the C language, so, often with little modififications, a C program will compile under C++. However, there are a number of enhancements (beyond the object-oriented capabilities, to be discussed shortly) that are available under C++. One is the use of the // notation to denote a single-line comment (many, but not all, C compilers also understand this). Another enhancement is the introduction of a bool basic data type to denote a boolean value - this can be one of two values: true or false.

For out purposes, the biggest difference between C++ and C is the use of a different way to handle standard input and output. This is illustrated in the program below.

#include <iostream.h>

int main(void) {
  int age;
  bool youngness;
  // This is a C++ comment
  cout << "Please enter your age: ";
  cin >> age;
  youngness = (age > 30) ? false : true;
  if (youngness == true) {
    cout << "Being only " << age << ", you sure are young." << endl;
  }
  else {
    cout << "Being already " << age << ", you sure are old." << endl;
  }
  return 0;
}
Note the use of the header file iostream.h, rather than stdio.h, to get the functions necessary to handle standard input and output. Also note the endl at the end of the cout statements to print a newline character.
next up previous contents index
Next: Classes Up: C++ Previous: C++   Contents   Index