Anatomy of a C++ Program

The parts of a C++ program and how they work together

These lecture notes cover the anatomy of a C++ program: the components of a C++ program and how they work together

The hello program

The simple hello.cpp program from our Getting Started notes has several interesting components.

// when executed, will print “Hello World” to the console. 
#include <iostream>
int main(){
	std::cout << Hello World << std::endl;
	return 0;
}

Directives: #include <iostream>

In the first line, the file iostream is included into the current .cpp file. The hashtag (#) signals to a program called the preprocessor which reads through your source code, looking for all the lines that begin the with # symbol. The command #include tells the preprocessor “find the file iostream, read it, and place it here.” The angle brackets (<>) surrounding the file name tells the preprocessor to look in the usual places for this file. If your compiler is set up properly, all the include files are in one repository. For example, I’m using the MinGW C++ compiler which, by default, holds all my include files in ~C:\MinGW\include.

The file iostream is used by cout, which writes to the console. The effect of #include <iostream> is to include the file iostream into this C++ program as if you typed it in yourself.

Function Signatures: int main()

The actual program begins with the function main(). Every C++ program needs to have a main() function because when you execute your program, main() is called automatically.

A function’s signature is important because it defines the inputs and outputs of the function. In int main(), the first part int states that the main() function returns an interger value to the operating system. This function achieves that because, at the bottom, it executes return 0.

Note: The compiler will allow you to return other data types. However, for main(), the only legal return type is int. Generally, it is the informal convention to return 0 to express that the program has ended without errors.

The Function Body & Namespaces

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

std::cout is an important aspect of this hello program. Later in class you will learn about classes and objects which operate similar to their real-world counterparts. Nonetheless, cout and cin are used in C++ to handle input (such as user input from a keyboard) and output (such as printing to the console).

cout and cin objects provided by the standard library (std). A library is a collection of classes. Thus, the standard library is the standard collection of objects that comes with every C++ compiler.

You tell the compiler that the cout object you can to use is part of the standard library by using the namespace specifier std. A real-world analogy for namespaces could be having to distinguish between several persons with the same first name. For example, there are many people with the name ‘Rebecca’, but perhaps only one with the last name ‘Ramnauth’. Within the namespace of the ‘Ramnauth’ family, just ‘Rebecca’ is enough to unambiguously designate me, while within the global namespace (the entire world), my full name used be used.

In our C++ program, std::cout is a way to say, "When I say cout, I actually mean the cout` in the standard namespace.”

When we begin on more complex programs, writing std:: in front of every object we’d like to use from the standard library becomes cumbersome. An alternative is to simple declare that we are using the standard namespace before the program begins:


#include <iostream>

using namespace std;

int main() {
	cout << "Hello World" << endl;
	return 0;
}

Printing to the Console: cout

cout stands for console output which simply means “Print to the console.” To use cout, type the word cout, followed by the *output redirection operator* («`). Whatever comes after the output redirection operator is written to the console.

Character strings (a series of letters and whitespaces) should be enclosed in double quotes ("...") as shown in our program (`“Hello World”);

endl tells cout to put a new line (or end the line) after the words Hello World.

Conclusion

This high-level view of the anatomy C++ program demonstrates directives, namespaces, functions, and how a program can read input and send output.


© 2020. Some rights reserved.