Software Design Using C++Simple C++ ProgramsInput, Output, and Simple ComputationsOutput and a First C++ ProgramOne of the most basic things that you can do in a program is to write some output to the screen. In C++ this is usually done with cout
as in the following example:
Note that a message string is enclosed in double quotes. The
So, how do you incorporate an output statement like the above into a C++ program? Read the output.cpp example, which is copied here for convenience:
Items enclosed by /* and */ are comments and are ignored by the compiler. The above example shows the type of comments expected at the top of each program. This section should give the filename, author, date, and a brief (but complete) description. The description should explain what the program does overall, its inputs, and its outputs. A short comment that fits on one line can be started with the // symbols. Anything on the same line but coming after the // is taken as a comment and is ignored by the compiler. Consider the following two examples:
Let's return to the sample program that we were discussing.
The
Sometimes we will need to include other headers in order to use a certain
type or class (such as
The program above consists of just a main function. All C++ programs must
have a main function. They can also have other functions.
program begins at the top of the main function. There are other ways
to write a main function (such as using
Execution of the program begins at the top of the main function. Note that
the commands to be carried out by the main function are listed in sequence
inside of curly braces. These commands are carried out in sequence, one
after the other, unless they contain something that alters this standard
order of execution. The last command inside of the main function is
typically Note that the commands inside of a function (and the braces) are indented a uniform 3 spaces. This is a typical indenting style. Although the compiler does not care about indenting, it makes it much easier for those people who must read the program. In this case, it makes it clearer what statements belong to the main function. See proto.cpp for an outline of a typical C++ program file. It shows how the comment section should be laid out, where to include the headers, etc. Compiling and Running a ProgramNow that we have a C++ program, what do we do with it? Before we can run it on a computer, the program has to be compiled (translated into machine code). Then you run the program itself. Exactly how to do all of this depends very much on what compiler you are using. Read Compiler Considerations, especially the section on Using Microsoft Visual C++, if this is your compiler. Note that if you already have a C++ source code file (such as the output.cpp above), you can
get a quick start by just double clicking on the filename in MyComputer and
accepting the suggestion of building a default workspace. If you are creating
a new program from scratch it is better to follow the directions in
Compiler Considerations.
Input of Integer, Float, and Character DataIf we can do output, surely we can also do input. The usual method involves reading data from the cin input stream. This normally reads its
data from what the user types at the keyboard. For example, to read in an
integer (int as it is called), you might use:
Note the use of the variable
Look at iotest.cpp for an example program that uses
both input and output. This program takes the common approach of printing
a prompt before each input operation. That way, the user of the program is
instructed about what to enter at each point. Note the use of two new data
types: Assignment StatementsCan we do something other than input and output? One thing we can do is to assign a value into a variable. This is a second way to place a value into a variable. The simplest form of the assignment statement is as seen in the following example:
A single = sign is used for assignment. It does not mean "equals" as in
algebra. (C++ uses ==, two equals signs, for equals.) The = means assignment.
An assignment statement is always executed from
right to left. First, the value on the right hard side is
figured out (here the literal number 45) and is then copied into the variable
on the left hand side (here the variable The left hand side of an assignment statement needs to be a variable, something that can be assigned a value. The right hand side can be an expression. An expression is anything that evaluates to a reasonable value. For example, if we have an integer variable on the left hand side of the = sign, we could use anything that has an integer value on the right hand side. Here are some examples using integer variable and values:
As before, we place 45 into Assignment statements can also be used to put character data into character variables. A literal character value must be written with single quotes around it, as in 'A', whereas a literal integer or floating point value is written without quotes, as in 3.1416. Here is an example using some assignment statements with character variables:
In this short example, the character T is copied into variable Simple ComputationsAssignment statements can be used to carry out simple computations. At last we can compute something! For example, to find the area of a rectangle we would want to multiply the length by the width and probably then store the answer in some variable. The computation could be carried out by an assignment statement such as:
Look at area1.cpp for an example program that finds
the area of a rectangle. It uses |