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.
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#
#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.
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.)
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).
#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.