Home > Articles > Programming > C/C++

The Anatomy of a C++ Program

  • Print
  • + Share This
Learn the basic parts of a simple C++ program, find out what main() is, get an introduction to namespaces, and learn the basics of console input and output. You will be able to use a lot of these in every program you write.
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 learn

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

Part of the Hello World Program

Your first C++ program in Lesson 1, “Getting Started,” did nothing more than write a simple “Hello World” statement to the screen. Yet this program contains some of the most important and basic building blocks of a C++ program. You use Listing 2.1 as a starting point to analyze components all C++ programs contain.

Listing 2.1. HelloWorldAnalysis.cpp: Analyze a C++ Program

 1: // Preprocessor directive that includes header iostream
 2: #include <iostream>
 4: // Start of your program: function block main()
 5: int main()
 6: {
 7:    /* Write to the screen */
 8:    std::cout << "Hello World" << std::endl;
10:    // Return a value to the OS
11:    return 0;
12: }

This C++ program can be broadly classified into two parts: the preprocessor directives that start with a # and the main body of the program that starts with int main().

Preprocessor Directive #include

As the name suggests, a preprocessor is a tool that runs before the actual compilation starts. Preprocessor directives are commands to the preprocessor and always start with a pound sign #. In Line 2 of Listing 2.1, #include <filename> tells the preprocessor to take the contents of the file (iostream, in this case) and include them at the line where the directive is made. iostream is a standard header file that is included because it contains the definition of std::cout used in Line 8 that prints “Hello World” on the screen. In other words, the compiler was able to compile Line 8 that contains std::cout because we instructed the preprocessor to include the definition of std::cout in Line 2.

The Body of Your Program main()

Following the preprocessor directive(s) is the body of the program characterized by the function main(). The execution of a C++ program always starts here. It is a standardized convention that function main() is declared with an int preceding it. int is the return value type of the function main().

Let’s discuss Line 8 that fulfills the actual purpose of this program!

   std::cout << "Hello World" << std::endl;

cout (“console-out”, also pronounced see-out) is the statement that writes “Hello World” to the screen. cout is a stream defined in the standard namespace (hence, std::cout), and what you are doing in this line is putting the text “Hello World” into this stream by using the stream insertion operator <<. std::endl is used to end a line, and inserting it into a stream is akin to inserting a carriage return. Note that the stream insertion operator is used every time a new entity needs to be inserted into the stream.

The good thing about streams in C++ is that similar stream semantics used with another stream type result in a different operation being performed with the same text—for example, insertion into a file instead of a console. Thus, working with streams gets intuitive, and when you are used to one stream (such as cout that writes text to the console), you find it easy to work with others (such as fstream that helps write text files to the disk).

Streams are discussed in greater detail in Lesson 27, “Using Streams for Input and Output.”

Returning a Value

Functions in C++ need to return a value unless explicitly specified otherwise. main() is a function, too, and always returns an integer. This value is returned to the operating system (OS) and, depending on the nature of your application, can be very useful as most OSes provide for an ability to query on the return value of an application that has terminated naturally. In many cases, one application is launched by another and the parent application (that launches) wants to know if the child application (that was launched) has completed its task successfully. The programmer can use the return value of main() to convey a success or error state to the parent application.

  • + Share This
  • 🔖 Save To Your Account