Notes N2

Math 10 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 10 Homepage | Return to Notes Listing Page]

Contents


Introduction To C++

C++ is a recent, popular, scientific, applicative language.

It is immediately descended from C, associated with the Unix operating system, but has been influenced by developments since about 1980 in computer languages which have focused on data encapsulation and object-oriented programming.

Some people have even recently suggested that C is not a good language to uses as a first programming language -- it can lead to too many errors and thus too much frustration. C++ may not be as disastrous for new programmers, but one still has to be careful.

Many modern languages focus on "structured programming" and on a disciplined programming style. Some other languages have been re-written in a "structured" versions, e.g., "structured COBOL." (cf. One New Jersey alumnus of St. Peter's College (Jersey City) got a job programming in structured COBOL, while everyone else used an older style. Because he consistently got his programs running correctly faster, he eventually received a two week vacation while the rest of his division was retrained in structured programming techniques.) In the spirit started by the language Algol, C/C++ is said to be format-free. In other words, it doesn't make any difference to the compiler how much or how little you put on a line or where on the line you put anything. It may make a lot of difference to the person reading the program, however. C/C++ is "structured." This help to avoid problems like the so-called "spaghetti code" of BASIC and early versions of FORTRAN where the "flow" of the program is far from evident.

Identifiers

Certain concepts need names. We call these names identifiers. They vaguely correspond to variables in algebra. In C/C++, they can be any reasonable length, but in some cases only the first 31 letters are analyzed by the compiler.

C/C++ is case sensitive, in other words, UPPERCASE and lowercase letters are NOT considered to be the same.

By convention, identifiers referring to variable concepts are usually all lowercase, except if the identifier is actually several words together, in which case the internal words are capitalized. For example numberOfDays.

By convention, identifiers referring to constant concepts are usually all uppercase.

It should be noted that long identifiers are usually harder to type correctly (consistently). The first character MUST be a letter (of the alphabet). The rest can be letters, numbers, or the underline. Underlines do help readability.

NOTE: you cannot have blanks in identifiers or other special characters.

        GOOD IDENTIFIERS            BAD IDENTIFIERS
            ABCD                        BAD NUM
            car                         3A
            dollars                	dol#

Reserved Words

Some words have a special meaning in C/C++. They CANNOT be used as identifiers. In some books, they are printed in bold, or in italics, or underlined. They are always lower-case.

C++ Program Skeleton

	#include <iostream.h>

	int main()
	   {

	   initial declarations of variables 

	   statements  
	   }
NOTES:

(1) statements indicates one or more valid C/C++ statements each ended by a semi-colon.

(2) { and } can be used to enclose "blocks," like opening and closing parentheses. Every time you have a { (opening brace), you must have a corresponding } (closing brace).

(3) Semicolons are extremely important. Forgotten or misplaced semicolons can cause a lot of errors. They can also lead to "phony" error messages. One NEEDS a semicolon after every statement.

(4) Re: Debugging. Carefully look at error messages, especially the first one. "Band-aid" solutions are bad, i.e., fixing symptoms rather than curing causes. Don't fix what isn't broken! The most time in getting a program running is spent on debugging, i.e., fixing both compiler and logical errors.

(5) In C/C++, new lines are meaningless. I.e., carriage returns are interpreted as blank spaces. The semi-colon is the important separator.

(6) Some style books suggest/recommend that the last statement of main be the line

		return 0;
This return statement is of more use in other subprograms in which a specific value needs to be returned (i.e., what many languages designate as function). Nevertheless, it can be a good practice to consistently use the return statement as the last statement in any program segment.

(7) The int before the keyword main indicates that the "main" program segment returns an integer value. One can also use void before main and omit the return statement. Some books omit any qualifier before the keyword main.

Comments

Comments are helps to programmers (i.e., humans). They are ignored by the compiler (i.e., the machine). It is hard to over-comment, and good (internal) comments and (external) documentation for programs are an ever increasing concern for software companies.

In C++, comments are indicated in one of two ways:

In general, programs should have three types of comments. (See Introduction to Math 10 for specifics.)

Output

There are two sets of input/output ("i/o") functions in C++, the older file i/o functions (from C) and the newer stream i/o functions. To use the older file functions, one must begin the C++ program file with an include statement referencing the appropriate header file in this way:

	#include <stdio.h>
(where "stdio.h" is the "header" file for "standard input-output" functions). More information about the standard i/o is found at this link.

To use the newer stream functions, one must begin the C++ program file with an include statement referencing the appropriate header file in this way:

	#include <iostream.h>
(where "iostream.h" is the "header" file for "input-output stream" functions).

Sample Program

	#include <iostream.h>
	// sample program
	int main ()
	   {
	     cout << "Hello" << endl;
	     cout << "How";
	     cout << " are you?" << endl;
             return 0;
           }

[output]
		|	|	|
		v	v	v

		Hello
		How are you?


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1997, 1998 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 31 December 1998.