Format of a C++ Program
We start here by presenting a simplistic view of the
general format for C++ programs.
This will help get you started programming.
Later on, you will get into the object-oriented
features of the language.
For now though, here is the basic syntax you need to get going.
Comments
There are two forms of comment in C++.
- 
   /* comment */
  A comment that carries over several lines begins with
   /* and ends with */
 - 
// comment
A comment on just one line - or part of a line - 
begins with //
Typically, you'll see an inline comment at the end of
a processing block such as }    // end program
 
A comment block is simply a collection of comments
contained by these characters: 
   /*     */
The first lines of any C++ function should be a comment block.
There can be many functions in a C++ program
so a comment block is extremely helpful for yourself, or
other programmers using your code.
There are various conventions for the content of these comment blocks, but
a block should minimally consist of:
- the filename
 - the author's name
 - the purpose of the program
 - the parameters passed/to and returned/by the function
 
Here is an example of a comment block that begins a
simple program.
/****************************************************
*
*  FileName:    ~ftp/pub/class/170/ftp/cpp/hello.cpp
*  Author:      Ada Lovelace
*  Purpose:
*               Demonstrate simple I/O by inputting a
*               name and outputting a welcome message.
*
********************************************************/
include Statement
The first lines of the "main"
function
(which, for simple programs, is the only function)
are:
#include <iostream>
using namespace std;
This statement informs the compiler of the location of the
namespace where identifiers such as
cout and endl (used in I/O) are located.
The #include <name> statement is 
referred to as a "preprocessor directive" because
it instructs the C++ compiler where to find the necessary
libraries before the compilation begins.
Function Header
The general format of a function header is 
return_type function_name (parameters).
The simplest example of this is the function header for
the main function.
You'll see both forms of this header - either is acceptable.
i.e.
- void main()
The term void simply means that there is no
return value produced by the function.
Notice that the parentheses are empty - this means that 
no parameters are being passed into the function.
 - or
int main()
If you see this format of the header, you will also see
the line return 0;
in the body of the function.
The value 0 is really not being returned
anywhere - it just shuts the compiler up from reporting
a syntax error.
(It's a history thing.)
 
C++ Statement
A statement is a C++ instruction that must end with
a semi-colon 
";"
e.g. cout << "Hi There" << endl
;
Many C++ statements can be contained in one line, but there
is no reason that you cannot spread the statement over several 
lines for readability - especially useful when you get to
more complex output statements. 
Here is a trivial example:
	cout << "Hi"
	     << "There"
	     << endl;
Any line of text - e.g. cout, or an algebraic calculation - can be split
over a number of lines to improve readability.
As far as the compiler is concerned, the "statement" is officially
concluded only when the semi-colon appears.
Notice that in order to continue a literal string
over more than one line, you need to end each line with
a quotation mark.
Statement Block
The body of the function will contain
a statement block which is 
a group of two or more statements enclosed in braces, begining with
"{"
and ending with
"}"
You'll see these braces used to contain statement blocks
all over C++ programs; wherever you want to group statements,
such as in a IF statement.
The following example shows just one big statement block in the
main function.
Complete Program Example
/********************************************************
*
*  FileName:    ~ftp/pub/class/170/ftp/cpp/hello.cpp
*  Author:      Ada Lovelace
*  Purpose:
*               Demonstrate simple I/O by inputting a
*               name and outputting a welcome message.
*
********************************************************/
#include <iostream>	// preprocessor directive
using namespace std;	// necessary for I/O
void main() {    // statement block enclosed by {  and }
  string user_name;     // variable declaration
    cout << "Welcome to the world of C++" << endl;
    cout << "What is your name? ";
    cin >> user_name;
    cout << endl;
    cout << "Hi " 	// statement continued over several lines
	  << user_name 
          << "! Welcome to CS170." 
          << endl;
} // end main     <- comment used to clarify the "}"
This page has been accessed  
   times.
Last modified: Friday, 21-Aug-2020 15:28:13 CST
Copyright 2002 Department of Computer Science, University of Regina.