Home arrow Practices arrow Page 3 - Basic Ideas

A Simple C++ Program - Practices

This article will give you a good understanding of the basic concepts and practices of the C++ language, so that you will have the foundation to eventually learn these ideas in detail as you continue working with the language. It is excerpted from Ivor Horton's Beginning ANSI C++ The Complete Language (Apress, 2004; ISBN 1590592271).

  1. Basic Ideas
  2. Interpreted vs. Compiled Program Execution
  3. A Simple C Program
  4. Names Using Extended Character Sets
  5. C Statements and Statement Blocks
  6. Creating an Executable from Your Source Files
  7. C Source Characters
  8. Whitespace in Statements
  9. Procedural and Object-Oriented Programming
By: Apress Publishing
Rating: starstarstarstarstar / 18
March 23, 2005

print this article



Letís take a look at a very simple C++ program and find out what its constituents are. You donít need to enter this code right now; itís just here so that you can get a feel for what goes into making up a program. I donít go into all the details at the moment either, as everything that appears here will be explored at length in later chapters. Figure 1-2 illustrates a simple C++ program.

Figure 1-2.  A simple C++ program

The program shown in Figure 1-2 displays the following message:

The best place to start is at the beginning

This isnít a very useful program, but it serves to demonstrate a few points. The program consists of a single function, main(). A function is a self-contained block of code thatís referenced by a name, main in this case. There may be a lot of other code in a program, but every C++ application consists of at least the function main(). There can be only one function called main() within a program, and execution of a C++ program always starts with the first statement in main().

The first line of the function is

int main()

which identifies that this is the start of a function with the name main. The int at the beginning indicates that this function will return an integer value when it finishes executing. Because itís the function main(), the value will be received by the operating system that calls it in the first place.

This function main() contains two executable statements, each on a separate line:

 cout << "The best place to start is at the beginning";
 return 0;

These two statements are executed in sequence. In general, the statements in a function are always executed sequentially, unless thereís a statement that specifically alters the sequence of execution. Youíll see what sorts of statements can do that in Chapter 4.

In C++, input and output are preferably performed using streams. If you want to output something from a program, you put it into an output stream, and when you want something to be input, you get it from an input stream. A stream is thus an abstract representation of a source of data, or a data sink. When your program executes, each stream is tied to a specific device that is the source of data in the case of an input stream and the destination for data in the case of an output stream. The advantage of having an abstract representation of a source or sink for data is that the programming is the same regardless of what the stream actually represents. You can read data from a disk file in essentially the same way as you read from the keyboard, for instance. The standard output and input streams in C++ are called cout and cin, and by default they correspond to your computerís screen and keyboard, respectively.

The first line of code in main() outputs the character string ďThe best place to start is at the beginningĒ to your screen by placing it in the output stream, cout, using the insertion operator,<<. When we come to write programs that involve input, youíll see its partner, the extraction operator, >>.

A header contains code defining a set of standard facilities that you can include in a program source file when required. The facilities provided by the C++ standard library are stored in headers, but headers arenít exclusively for that. Youíll create your own header files containing your own code. The name cout referred to in this program is defined in the header iostream. This is a standard header that provides the definitions necessary for you to use the standard input and output facilities in C++. If your program didnít include the following line:

#include <iostream>

then it wouldnít compile, because the <iostream> header contains the definition of cout, and without it the compiler canít know what cout is. This line is an example of what is called a preprocessing directive, which youíll investigate in depth later in the book. The effect of the #include is to insert the contents of the <iostream> header into your program source file at the point where the directive appears. This is done before your program is compiled.

TIP  Note that there are no spaces between the angled brackets and the standard header name.With many compilers, spaces are significant between the two angled brackets, < and >; if you insert any spaces here, the program may not compile.

The second and final statement in the body of the function name is

 return 0;

This ends the program and returns control to your operating system. It also returns the value zero to the operating system. Other values can be returned to indicate different end conditions for the program and can be used by the operating system to determine if the program executed successfully. Typically, zero indicates a normal end to a program, and any nonzero value indicates an abnormal end. However, whether or not a nonzero return value can be acted upon will depend on the operating system concerned.


Lots of things in a C++ program have names that are used to refer to them. Such names are also referred to as identifiers. There are five kinds of things that youíll give names to in your C++ programs:

  • Functions are self-contained, named blocks of executable code. Chapter 8 goes into detail on how to define these.

  • Variables are named areas in memory that you use to store items of data. Youíll start with these in Chapter 2.

  • Types are names for the kinds of data that you can store. The type int, for example, is used for integers (whole numbers). Youíll see something on these in Chapter 2 and more in subsequent chapters, particularly Chapter 11.

  • Labels provide a means of referring to a particular statement. These are rarely used, but youíll look at them in action in Chapter 4.

  • Namespaces are a way of gathering a set of named items in your program under a single name. If that sounds confusing, donít worryóIíll say more about them shortly, and youíll look at them again in Chapter 10.

In C++, you can construct a name using the upper- and lowercase Latin letters a to z and A to Z, the underscore character (_), and the digits 0 to 9. The ANSI standard for C++ also permits Universal Character Set (UCS) characters to be included in a name for reasons I cover in a moment.

The ANSI standard allows names to be of any length, but typically a particular compiler will impose some sort of length limit. However, this is normally sufficiently large (several thousand characters) that it doesnít represent a serious constraint.

Whitespace is the term used in C++ to refer to spaces, vertical and horizontal tabs, and newline and form-feed characters. You must not put whitespace characters in the middle of a name. If you do, the single name wonít be seen by the compiler as such; it will be seen as two or more names, and therefore it wonít be processed correctly. Another restriction is that names may not begin with a digit.

Here are some examples of legal names:

value2  Mephistopheles  BettyMay  Earth_Weight  PI

Here are some names that arenít legal:

8Ball   Mary-Ann   Betty+May    Earth-Weight   2PI

Note that names that contain a double underscore (_ _) or start with an underscore followed by an uppercase letter are reserved for use by the C++ standard library, so you shouldnít choose such names for use in your programs.Your compiler probably wonít check for this, so youíll only find out that you have a conflicting name when things go wrong!

This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN  1590592271). Check it out at your favorite bookstore today. Buy this book now.

>>> More Practices Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Calculating Development Project Costs
- More Techniques for Finding Things
- Finding Things
- Finishing the System`s Outlines
- The System in So Many Words
- Basic Data Types and Calculations
- What`s the Address? Pointers
- Design with ArgoUML
- Pragmatic Guidelines: Diagrams That Work
- Five-Step UML: OOAD for Short Attention Span...
- Five-Step UML: OOAD for Short Attention Span...
- Introducing UML: Object-Oriented Analysis an...
- Class and Object Diagrams
- Class Relationships
- Classes

Developer Shed Affiliates


Dev Shed Tutorial Topics: