The Organization of a C++ Program

 

Program Structure

C++ is a procedural language, just as is C. However, it also provides OO extensions which allow you to declare classes. The procedural modules are called functions (or methods, if they are functions belonging to a class). The main function encapsulates the entire program, so far as control flow is concerned.

Other Functions

Any other functions in a C++ program must be hierarchically called from main. That is, main calls other functions and those functions may call other functions, etc. There are a few exceptions to this, which are functions that are called directly from the operating system (See std::set_terminate(), page 363 under Exception Handling, Chapter 22, and also see Application Frameworks in Chapter 18 and function WndProc on page 531)

Program Comments

Use // to begin a comment all on one line.

Use /* this is a

                   Comment */  for comments that span multiple lines.

Function Structure

A function should have a prototype, if needed, of the form:

return_value  function_name (data_type parameter_name, data_type parameter_name, …);

If there is no return value, you must show a return_type of void. If the parameter list is empty, you can leave that list blank. The function definition serves as its own prototype when it isn’t needed for forward reference. In a prototype the parameter list must show the data_type for each parameter, but the parameter_name is optional. The calling function does not use the names or identifiers of the parameters – only the function itself has access to those names. The calling function is responsible for providing variables or expressions that evaluate to the specified data type of the parameter.

Function Return Values

When a function is to return a value, it can be done in either or both of two ways. Usually, the “return value” is understood to be the value of the expression:

function_name();

which can also appear on the right side of the assignment operator, or anywhere else that an expression is allowed:

x = function_name();

The value of x will be replaced by the value returned by the function function_name.

 

The other way in which a function may return values is via the parameter list. If the arguments are passed by reference instead of by value, then the calling function arguments can be modified by the called function.

Expressions

Expressions are legal combinations of variables, function calls, and operators which can be evaluated to a single value. Expressions are the building blocks for building statements.

The Statements

Calling Functions

Calls to functions do not have to be coded as separate statements – they can be used as expressions anywhere that an expression could be used, so long as the function has a return value. It is also not a requirement to use the return value. It can be ignored by coding the function call as a simple statement.

Variables

Variables are entities having two fundamental properties – name, or identifier, and value.  The name or identifier is means of referencing the variable in the program. Whenever a reference to a variable is found in an expression, its value can be substituted into the expression in order to evaluate the expression. Either before or during its first use a variable must be declared.

 

To declare a variable is to give it a name or identifier and to specify its data type. Variables have another property, which is their scope.

 

Global scope means that it can be referenced anywhere in the program. A variable has global scope if it is declared outside of any function. If a variable is defined within a function, its scope is limited to or local to that function. The parameters listed in the parameter list of a function are local to the function.

 

The definition of a variable:

Executable Statements

In arithmetic we see expressions like the following:

X + y = z

Programming assignments may look similar to that but are quite different. In the arithmetic statement above there is equality between the expression on the right and the expression on the left.

 

In programming the expression on the left must be an “l-value”. Expressions that refer to memory locations are called “l-value” expressions. An l-value represents a storage region’s “locator” value, or a “left” value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.

 

The X + y expression above refers to two different variables, each of which would have its own storage location in memory – therefore, X + y could not be a legal l-value.  Under this topic on page 14 there are several examples of executable statements, such as variable declaration, assignment statements, flow control statements, etc.

Classes

Aggregate declarations such as struct’s and unions are found in “C”.

 

C++ extends that concept with the class, which is very similar to the struct but can include methods, or functions that are members of the class. A class encapsulates the implementation and the interface of a user defined data abstract data type, also known as a user-defined data type.

 

A class is just a collection of variables – often of different types – combined with a set of related functions. Any target of the member functions is the object’s data members themselves and the member functions provide the behavior of the object while limiting the visibility to its internals.

Exception Handling

Chapter 22 is devoted to this topic, which is a methodology for organizing the detection of and the structured response to any exceptions that you want to define. This is referred to as try, catch, throw.

Templates

C++ supports parameterized types or generecity – which in plain English is “macros” on steroids. A function template is simply a way of declaring template for handling a certain situation in a consistent way for a number of different classes.

Source Files

We write our C++ source code as an ASCII text file. It doesn’t matter what kind of a text editor, word processor or Integrated Development Environment you use, so long as it is saved as an ASCII text file. NotePad is just as good as the Visual C++ IDE or the Quincy 99 IDE – except that it does not incorporate some of the powerful features of those environments and is not integrated with the compiler, linker, debugger, and help file. Each source file is also known as a translation unit, and is compiled into an object module. Object modules are combined with a link editor to become an executable image. The object files processed by the linker can come from the programmer’s source files and also from libraries of compiled object code.

 

Libraries

The standard class libraries of C++ support data conversions, string manipulations, formatting of output data, etc.

 

The extensibility of C++ comes from the fact that the core language is generic across almost any platform. Any differences that must be incorporated to allow porting to a different machine architecture (instruction set) or different operating system, can be provided in class libraries specific to that platform.

 

It should be noted that by “compilers” we usually mean the entire package of compiler and other utilities related to producing an executable image for a given platform. That development kit can be designed to run on platform “A” and produce code targeted to platform “B” – so remember that there are at least two dimensions to the question of what your development kit can do.

 

Development Kits for this Course

What you will need for this course is a development kit appropriate to the platform on which you will do your homework. If that platform is Win32, then the Quincy 99 system that comes with the textbook will do the job. The Microsoft Visual Studio development system for Visual C++ will also be provided by your instructor for anyone who wants the addition features provided by that environment. If you intend to work in a Unix or Linux or Macintosh environment, you will be expected to provide your own development kit and to deliver the required homework as ASCII text files in a format that can be read in a Win32 environment. The instructor can assist to the extent of providing either Red Hat or Mandrake distributions of Linux, but installation and configuration is the student’s responsibility.