Home > Articles > Programming > C/C++

The Anatomy of a C++ Program

  • Print
  • + Share This
The difficulty in learning a complex subject, such as programming, is that so much of what you learn depends on everything else there is to learn. This chapter introduces the basic parts of a simple C++ program.
This chapter is from the book

C++ programs consist of classes, functions, variables, and other component parts. Most of this book is devoted to explaining these parts in depth, but to get a sense of how a program fits together, you must see a complete working program.

In this lesson, you will learn

  • The parts of a C++ program
  • How the parts work together
  • What a function is and what it does

A Simple Program

Even the simple program HELLO.cpp from Lesson 1, “Getting Started,” had many interesting parts. This section reviews this program in more detail. Listing 2.1 reproduces the original version of HELLO.cpp for your convenience.

Listing 2.1. HELLO.cpp Demonstrates the Parts of a C++ Program

 1:  #include <iostream>
 3:  int main()
 4:  {
 5:      std::cout << "Hello World!\n";
 6:      return 0;
 7:  }


Hello World!


On the first line, the file iostream is included into the current file. Here’s how that works: The first character is the # symbol, which is a signal to a program called the preprocessor. Each time you start your compiler, the preprocessor is run first. The preprocessor reads through your source code, looking for lines that begin with the pound symbol (#) and acts on those lines before the compiler runs. The preprocessor is discussed in further detail in Lesson 15, “An Introduction to Macros and Templates,” and in Lesson 29, “Tapping Further into the Preprocessor.”

The command #include is a preprocessor instruction that says, “What follows is a filename. Find that file, read it, and place it right here.” The angle brackets around the filename tell the preprocessor to look in all the usual places for this file. If your compiler is set up correctly, the angle brackets cause the preprocessor to look for the file iostream in the directory that holds all the include files for your compiler. The file iostream (input-output-stream) is used by cout, which assists with writing to the console. The effect of line 1 is to include the file iostream into this program as if you had typed it in yourself.

The actual program starts with the function named main(). Every C++ program has a main() function. A function is a block of code that performs one or more actions. Usually, functions are invoked or called by other functions, but main() is special. When your program starts, main() is called automatically.

main(), like all functions, must state what kind of value it returns. The return value type for main() in HELLO.cpp is int, which means that this function returns an integer to the operating system when it completes. In this case, it returns the integer value 0. A value may be returned to the operating system to indicate success or failure, or using a failure code to describe a cause of failure. This may be of importance in situations where an application is launched by another. The application that launches can use this “exit code” to make decisions pertaining to success or failure in the execution of the application that was launched.

All functions begin with an opening brace ({) and end with a closing brace (}). Everything between the opening and closing braces is considered a part of the function.

The meat and potatoes of this program is in the usage of std::cout. The object cout is used to print a message to the screen. You’ll learn about objects in general in Lesson 10, “Classes and Objects,” and cout and cin in detail in Lesson 27, “Working with Streams.” These two objects, cin and cout, are used in C++ to handle input (for example, from the keyboard) and output (for example, to the console), respectively.

cout is an object provided by the standard library. A library is a collection of classes. The standard library is the standard collection that comes with every ANSI-compliant compiler.

You designate to the compiler that the cout object you want to use is part of the standard library by using the namespace specifier std. Because you might have objects with the same name from more than one vendor, C++ divides the world into namespaces. A namespace is a way to say, “When I say cout, I mean the cout that is part of the standard namespace, not some other namespace.” You say that to the compiler by putting the characters std followed by two colons before the cout.

Here’s how cout is used: Type the word cout, followed by the output redirection operator (<<). Whatever follows the output redirection operator is written to the console. If you want a string of characters written, be certain to enclose them in double quotes ("), as visible in Listing 2.1.

A text string is a series of printable characters. The final two characters, \n, tell cout to put a new line after the words Hello World!

The main() function ends with the closing brace (}).

  • + Share This
  • 🔖 Save To Your Account