A SAMPLE C++ PROGRAM: 1. /* This sample C++ program demonstrates basic program structure, 2. as well as console input and output. 3. */ 4. 5. #include 6. using namespace std; 7. 8. int main() { 9. 10. cout << "\nThis program will find the quotient of two numbers\n" 11. << "that you enter.\n\n"; 12. 13. double a,b; // The two numbers entered by the user. 14. 15. cout << "Enter your first number: "; 16. cin >> a; 17. 18. cout << "Enter your second number: "; 19. cin >> b; 20. 21. if (b) { 22. double quotient = a/b; // The quotient of the user's numbers; 23. cout << "The quotient is " << quotient << "\n\n"; 24. } 25. else { 26. cerr << "Sorry. Division by zero is not allowed.\n\n"; 27. } 28. 29. } Commentary: Line 1: C++ uses the same types of comments as Java. // starts a comment that ends at the end of the line. /* starts a comment that ends with a */. (This is NOT an example of a good comment for a real program! It's just an example of a /* comment.) Line 5: Almost every C++ program uses header files. To use a header file in a program, you have to #include it. Standard header file names are enclosed between < and >. The iostream standard header file defines cin, cout, and cerr (among other things). These are objects that are used for standard console-style I/O. As another example, the standard mathematical functions are defined in a header file named cmath. If you want to use them, you should "#include " at the beginning of the program. The functions declared in this header file include things like sin(x), abs(x), sqrt(x), and pow(x,y). (In Java, the same functions are static members of the Math class and have names such as Math.sin and Math.sqrt. In C++, they are not members of a class, but you have to include the cmath header if you want to use them.) Line 6: This line (or something equivalent) is required by the C++ standard, but it is NOT required by the compiler that we will be using. According to the standard, the proper names for standard identifiers such as cout and sqrt are std::cout and std::sqrt. The directive "using namespace std;" makes it possible to use these identifiers without the "std::" prefix. Line 8: A C++ program must have a main() function. (All subroutines in C++ are called "functions", whether or not they return a value.) This function cannot be a member of a class. It should be declared as "int main()" or as "int main(int argc, char* argv[])". In the second form, argv is an array of command-line parameters, and argc is the number of items in the array. A return value of 0 from main() tells the operating system that the program "succeeded", while any non-zero value indicates "failure". If no return statement is included, as in this example, then a return value of 0 is provided automatically. Line 10: Output to the console is done with the console output object, cout. The << operator is used with cout to specify the output. Note that you can string together several output items by using several << operators. A \n in a string indicates a new line. Line 13: Simple variable declarations are similar in C++ and Java. The most common types in C++ are int, double, bool, and char. Note that the true/false type is named "bool" rather than "boolean". The char type represents 8-bit ASCII characters, not 16-bit Unicode characters as in Java. Line 16: Input from the console is done with the console input object, cin. The >> operator is used to put an input value into a variable. You can string together several input operations, as in "cin >> a >> b;". Note that the direction of the arrows indicates the direction of the flow of information: In "cout << a", a value is being sent from a to cout. In "cin >> a", a value is being sent from cin into the variable a. Line 21: Control structures (if, while, do, for, switch) are almost identical in C++ and Java. One significant difference is that in C++, a numerical value can be used wherever a Boolean value would ordinarily be used. A numerical value of zero is considered to be false; any non-zero value is considered to be true. Thus, in this sample program, "if (b)" is testing whether b is non-zero. It might be clearer to say "if (b != 0)", but the effect is the same. Line 22: Just as in Java, a variable can be defined inside a block. The variable is then local to that block. Line 26: There is an object called cerr which is supposed to be used for output about errors that occur when the program is running. Error output might be sent to a different location than the output from cout, such as to an error log file. In practice, cerr is mostly used in non-interactive programs, and you will rarely encounter it.