Fundamentals of Programming using C++
[Top]
[Topic]
[Task]
[Test]
[TTT index]
Topic 2: Program Structure.
You might know that C++ is often used for Object-Oriented Programming (OOP) but in
this course we won't be creating objects. Instead, we will concentrate on ordinary
(structured) programming.
When you learn to write structured programs for the first time you start with small simple
programs and work towards more complex and powerful programs. There are some things
which remain the same for all programs no matter how trivial or complex. The most
important of these is the overall program structure.
In general, a C++ program has five zones (or sections). If you viewed a complete program
through the wrong end of a pair of binoculars, you would see these sections as shown in
the following list:
- Section 1: Introductory comments
- These contain the name of the author, the date the program was written, the
main purpose of the program and any important assumptions made by the
programmer. These are recommended in all your programs and are part of the culture
of 'good' programming.
- Section 2.: Included header files
- These statements tell the compiler what auxilary files to process before
processing the actual program. Usually the files contain useful functions (sub
programs) that have been pre-written. You will need to include at least one
header file even in your first program. Later you will learn to include several
different header files.
- Section 3: Function prototypes
- These are the first line (only) of the separate functions (sub-programs or
modules) the programmer has designed as part of the solution. They are used
to 'introduce' the names of all the functions the program uses. Your early
programs will not contain any subprograms so you don't have to worry about
this section just yet.
- Section 4: The Main function
- This is the main section or "main block" of the program. It controls the
activities of all the subprograms and usually encapsulates the general logic
used to solve the given problem. All your programs will have a main block.
- Section 5: Function definitions
- This is the actual detailed description of each and every function
(subprogram) used in your program. You won't be using functions at all in
the early stages.
There is a complete C++ program shown below. I suggest you have a look at it now, paying
particular attention to the program's overall structure. Try to identify
each of the five sections outlined above.
Don't be concerned if this sample program appears complex. The ones you will be
expected to write in the early stages of this course will be much simpler. However
you may well be able to figure out what the sample program does. So, as well as
checking for the five sections, try to interpret what the programmer is doing - make
intelligent guesses!
// Section 1: (Introductory Comments)
// A sample program showing the
// overall structure of non-OOP, C++ programs
// Author's Name...
// Date created ..
// Compiler used...
// Section 2: (Included files)
#include <iostream.h>
// Section 3: (Function prototypes)
float calc_average( int n1, int n2, int n3 );
// Section 4: (The main function)
void main( void )
{
int first, second, third;
float result;
cout << "Please enter three whole numbers separated by spaces";
cin >> first >> second >> third;
result = calc_average( first, second, third );
cout << "The average of the numbers is " << result << endl;
}
// Section 5: (Function definitions)
float calc_average( int n1, int n2, int n3 )
{
float answer;
answer = (float) (n1 + n2 + n3 ) / 3;
return answer;
}
[Top]
[Topic]
[Task]
[Test]
[TTT index]
Task 2: Interpeting an existing
program.
- Look firstly at section 4 of the sample program. This is the main
function. The main function in a C++ program encapsulates the overall logic
of the program. Try to recognise the main tasks done by this program.
- The word float is used to describe floating point
numbers. Can you guess what the word int describes?
[Top]
[Topic]
[Task]
[Test]
[TTT index]
Test 2 - Program Structure
Click here to try this topic's test
[Top]
[Topic]
[Task]
[Test]
[TTT index]
- Author:
- Errol Chopping
- Lecturer in Computing
- Charles Sturt University - Mitchell
- Bathurst NSW 2795
- Australia.
- Last Update: Nov 17, 1995