Welcome to the world of C++! If you’re a novice programmer making the leap from C to C++, this guide is tailored just for you. Let’s dive into the key changes and features you’ll encounter as you explore the realm of C++.

As we begin the study of C++ and object oriented programming, a few comments are in order to help you get started. Since the field of object oriented programming is probably new to you, you will find that there is a significant amount of new terminology for you to grasp.

Table of Contents

Comments in C++

In C++, comments start with a double slash (//) and extend to the end of the line. The traditional ANSI-C comment notation (/* */) is still valid in C++. However, it’s recommended to use the double-slash method for better code readability and to avoid accidentally commenting out multiple lines.

The output of the program is:

  • The value of the index is 3
  • The value of the index is 4
  • The value of the index is 5
  • The value of the index is 6
  • The value of the index is 7
  • The value of the index is 8

The careful selection of variable and function names can make your C++ code self documenting and you should strive to achieve this in your code.

Keywords const and volatile

In C++, the const keyword is used to define constants. This keyword ensures that the values of constants cannot be changed. The compiler does not allow you to accidentally or purposefully change the value of START variable because it has been declared a constant. Similarly, the keyword const is used in the function header to indicate that the parameter named data_value is a constant throughout the function. Any attempt to assign a new value to this variable will result in a compile error.

The volatile keyword indicates that a variable may be modified by an external factor, such as a hardware function or an interrupt timer.

You can compile and execute this program with your C++ compiler to see if you get the same result as given in the comments at the end of the listing. One of the primary purposes of compiling it is to prove that your compiler is loaded and executing properly.

The Scope Operator

C++ introduces the scope operator (::) which allows you access to global variables even if a local variable shares the same name.

The output of the C++ Program is:

The scope operator allows access to global variables even though hidden by a local variable.

The iostream Library

C++ introduces the iostream library for flexible input and output operations. The cout and cin operators facilitate output to the console and input from the user.

The output of the C++ Program is:

C++, like the C language itself, has no input or output operations as part of the language itself, but defines the stream library to add input and output functions in a very elegant manner.

The cout Operator: Precision in Output

The cout operator <<, sometimes called the “put to” operator but more properly called the insertion operator, tells the system to output the variable or constant following it, but lets the system decide how to output the data.

Because C++ has several other operators and functions available with streams, you have complete flexibility in the use of the stream output functions. You should refer to your compiler documentation for details of other available formatting commands. The cout and the printf() statements can be mixed in any way you desire.

The cin Operator: Reading Input in C++

In C++, the cin operator is your gateway to receiving input from the user via the standard input device, typically the keyboard. This operator employs the >> symbol, known as the “get from” operator but more accurately termed the extraction operator. It provides similar flexibility to the cout operator for output.

The cerr Operator: Error Output

In addition to cout and cin, there is another essential operator: cerr. This operator is specifically designed for error output and corresponds to the stderr stream pointer in C. Unlike cout, output through cerr cannot be redirected to a file.

File Stream Operations

We use the following C++ program demonstrates file stream operations. This program copies a file and prints its content. Let’s go through the code and understand its functionality:

The output of the C++ Program is:

The standard file I/O library is available with ANSI-C and is as easy to use as the stream library and very portable. For more information on the stream file I/O library refer to this article.

Variable Definitions

Automatic variables in C++ need explicit initialization. Reference variables and a more explicit for loop syntax are also introduced. Review the following C+ program for a few more additions to the C++ language which aid in writing a clear and easy to understand program.

Output of the C++ Program is:

Automatic variables, those declared inside of any function, are not automatically initialized but will contain the value that happens to be in the location where they are defined, which must be considered a garbage value.

Reference Variable

C++ introduces reference variables using the ampersand (&) symbol. They act as synonyms for existing variables, providing an alternative way to access the same data. The reference variable is not quite the same as any other variable because it operates like a self dereferencing pointer. After initialization, the reference variable becomes a synonym for the variable, and changing the value of stuff variable will change the value of another_stuff because they are both actually referring to the same variable.

Declarations and Definitions in C++

In C++, “declaration” and “definition” have distinct meanings. A declaration provides information to the compiler about the characteristics of something (e.g., a type or a function). It does not actually define any code to be used in the executable program. A definition, on the other hand, actually defines something that will exist in the executable program, such as variables or executable code.

If we declare a struct, we are only declaring a pattern to tell the compiler how to store data later when we define one or more variables of that type. But when we define some variables of that type, we are actually declaring their names for use by the compiler, and defining a storage location to store the values of the variables. Therefore, when we define a variable, we are actually declaring it and defining it at the same time.

Variables and Scope:

Variables can be declared and defined near their point of usage in C++. This makes it easier to understand their purpose, as they have a more restricted scope. The scope of a variable is the region of the program where it is valid and accessible. Variables declared within a block (e.g., within a pair of curly braces {}) have a scope limited to that block.

For loop

The for loop in line 20 is highlighted as clearer than the one in ANSI-C. The loop index (count2) is declared within the loop itself, and its scope extends to the end of the enclosing block. This allows for better readability and avoids unintentional usage of the loop variable outside its intended scope.

Finally, as mentioned earlier, the static variable named goofy is declared and automatically initialized to zero in line 26. Its scope is from the point of its declaration to the end of the block in which it is declared, line 29.

Operator Reference

Operator reference in C++ encompasses both the well-defined operator precedence inherited from ANSI-C and the powerful concept of operator overloading. While initial familiarity with these topics is beneficial, a deeper understanding unfolds as you explore advanced scenarios and their practical applications in programming. The tutorial promises a gradual revelation of these intricacies, encouraging learners to embrace the journey of mastering C++ operators.