A Simple C++ Program

A C++ program is executed from the main function.

Let us begin with a simple C++ program that displays the message Welcome to C++! on the console. (The word console is an old computer term. It refers to a computer’s text entry and display device. Console input means to receive input from the keyboard and console output means to display output to the monitor.) The program is shown in Listing 1.1.

Listing 1.1 Welcome.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    // Display Welcome to C++ to the console

7    cout << “Welcome to C++!” << endl;

8

9    return 0;

10 }

The line numbers are not part of the program, but are displayed for reference purposes. So, don’t type line numbers in your program.

The first line in the program

#include <iostream>

is a compiler preprocessor directive that tells the compiler to include the iostream library in this program, which is needed to support console input and output. C++ library contains pre­defined code for developing C++ programs. The library like iostream is called a header file in C++, because it is usually included at the head of a program.

The statement in line 2

using namespace std;

tells the compiler to use the standard namespace. std is an abbreviation of standard. Name­space is a mechanism to avoid naming conflicts in a large program. The names cout and endl in line 7 are defined in the iostream library in the standard namespace. For the com­piler to find these names, the statement in line 2 must be used. Namespace is an advanced subject covered in Supplement IV.B. For now, all you need to do is to write line 2 in your program for performing any input and output operations.

Every C++ program is executed from a main function. A function is a construct that con­tains statements. The main function defined in lines 4-10 contains two statements. They are enclosed in a block that starts with a left brace, {, (line 5) and ends with a right brace, } (line 10). Every statement in C++ must end with a semicolon (;), known as the statement terminator.

The statement in line 7 displays a message to the console. cout (pronounced see-out) stands for console output. The << operator, referred to as the stream insertion operator, sends a string to the console. A string must be enclosed in quotation marks. The statement in line 7 first outputs the string “Welcome to C++!” to the console, then outputs endl. Note that endl stands for end line. Sending endl to the console ends a line and flushes the output buffer to ensure that the output is displayed immediately.

The statement (line 9)

return 0;

is placed at the end of every main function to exit the program. The value 0 indicates that the program has terminated with a successful exit. Some compilers will work fine if this statement is omitted; however, other compilers will not. It is a good practice always to include this state­ment for your program to work with all C++ compilers.

Line 6 is a comment that documents what the program is and how it is constructed. Com­ments help programmers to communicate and understand the program. They are not program­ming statements and thus are ignored by the compiler. In C++, a comment is preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment or paragraph comment. When the compiler sees //, it ignores all text after // on the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */.

Here are examples of the two types of comments:

// This application program prints Welcome to C++!

/* This application program prints Welcome to C++! */

/* This application program prints Welcome to C++! */

Keywords, or reserved words, have a specific meaning to the compiler and cannot be used in the program for other purposes. There are four keywords in this program: using, name­space, int, and return.

You have seen several special characters (e.g., #, //, <<) in the program. They are used almost in every program. Table 1.2 summarizes their uses

Most common errors students will encounter in this chapter are syntax errors. Like any other programming language, C++ has its own rules of grammar, called syntax, and you need to write code that obeys the syntax rules. If your program violates these rules, the C++ compiler will report syntax errors. Pay close attention to the punctuation. The redirection symbol << is two consecutive <’s. Every statement in the function ends with a semicolon (;).

The program in Listing 1.1 displays one message. Once you understand the program, it is easy to extend it to display more messages. For example, you can rewrite the program to display three messages, as shown in Listing 1.2.

Listing 1.2 WelcomeWithThreeMessages.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    cout << “Programming is fun!” << endl;

7    cout << “Fundamentals First” << endl;

8    cout << “Problem Driven” << endl;

9

10   return 0;

11 }

Further, you can perform mathematical computations and display the result to the console. Listing 1.3 gives such an example.

Listing 1.3 ComputeExpression.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    cout << “(10.5 + 2 * 3) / (45 – 3.5) = “;

7    cout << (10.5 + 2 * 3) / (45 3.5) << endl;

8

9    return 0;

10 }

The multiplication operator in C++ is *. As you see, it is a straightforward process to trans­late an arithmetic expression to a C++ expression. We will discuss C++ expressions further in Chapter 2.

You can combine multiple outputs in a single statement. For example, the following state­ment performs the same function as lines 6-7.

cout << “(10.5 + 2 * 3) / (45 – 3.5) = “

<< (10.5 + 2 * 3) / (45 – 3.5) << endl;

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *