Chapter 1: The Fundamentals of C++
In this chapter, we will go through the very basics of the object-oriented programming language, C++. Knowing about the characteristics of a programming language is very important for users to consolidate new information when coding. As such, we will only go through the very important and commonly referred to characteristics of C++. In addition, this chapter will introduce the fundamental steps that are absolutely necessary to create a C++ program. To iterate over these outlined steps without sounding monotonous, we will use examples that will incorporate the information highlighted in these steps allowing the reader to retrace through the things they learned. Lastly, the main goal of this chapter is to make the reader familiar with the fundamental layout of a standard C++ program.
In this chapter, we will discuss three major topics,
  1. The Fundamental Characteristics of C++.
  2. Discussion of Object-Oriented Programming.
  3. Translation and Creation of C++ Programs.
The Fundamental Characteristics of C++
It is important to understand that C++ is not a programming language that is purely focused on object-oriented programming. C++ is derived from the ‘C’ programming language and is a hybrid. Although C++ is different in some aspects from the original ‘C’ programming language, it still features the majority of the important functionalities found in the ‘C’ programming language. In other words, the C++ language also supports the features that are characteristic in the C programming language as well, such as:
Just as how C++ comes with all the important features of the C programming language, similarly, major contents of the code written in the C programming language can be reused in the C++ source code as well.
Another important characteristic of the C++ programming language is that it reinforces the concepts of C’s object-oriented programming into itself. For instance, some object-oriented programming concepts are listed below:
The C++ language features object-oriented characteristics of the C programming language and various elements from other programming languages. For instance, programming elements such as templates and exception handling offer incredible functionality when it comes to implementing your program efficiently. Not only that, these particular elements provide ease in your programming work while also ensuring that you have a clearer understanding of your programs.
Object-Oriented Programming
In this section, we will discuss and build a contrast between traditional procedural programming and object-oriented programming.
Traditional Procedural Programming
The main concept of the traditional procedural programming is based on separating the data which is supposed to be processed from the corresponding sub-routines and procedures (also known as data and functions). This significantly impacts the method through which a program handles data, for instance:
Due to such features, this limits the productivity of the program and hinders the support for low program maintenance requirements. Moreover, the features of traditional procedural programming mentioned above make the program more prone to errors.
Object-Oriented Programming
In object-oriented programming, instead of emphasizing the elements of data and functions, the focus is primarily on the objects itself. In other words, the programmer gives importance to the elements of a program that highlight the main purpose of the program itself. For instance, in object-oriented programming, a program that has been created for the handling and maintenance of bank accounts will feature data objects such as interest rates, balance, credit and debit calculations, transfers, and so on. Such a program built with object-oriented programming will feature objects corresponding to each account in the program. Each of the objects represents properties and capacities that are crucial for the basic function of account management for the program.
In object-oriented programming, the properties and capacities correspond to data and functions, respectively, and these elements are then combined in the program. This is done by using classes. A class used in a program that is based on object-oriented programming will define a type of object by directly defining the properties and capacities of the object. Objects can also communicate with each other through sending messages. In this way, an object can activate another object’s capacities as well that serves multiple advantages along with ease in building the program.
Advantages of Object-Oriented Programming
In terms of software development, there are several advantages offered by object-oriented programming that make it more practical than traditional procedural programming. As traditional procedural programming lacks certain aspects that make it less likely to be used by programmers. Some of these advantages have been listed below:
Translating and Creating a C++ Program
Developing a C++ program is simpler than you might think. In this section, we will discuss how we can translate a C++ program and create a simple C++ program.
Translating a C++ Program
The following diagram depicts the process of C++ translation.
When translating a C++ program, three major steps are very important for the creation and translation process. These steps have been outlined below,
  1. Saving the source code of the C++ program in a text file by using a text editor. The file which contains the source code of the program is known as the source file. The use of one source file is acceptable for short projects. However, for big programming projects, it is recommended to store the source code of the program in several source files so that they can be edited and translated separately with ease. This approach is known as modular programming .
  2. Using a compiler to translate the program. The programmer feeds the compiler, a source file that contains the source code of the program which he wishes to translate. If the compiling process does not encounter any errors, then the output will be an object file that contains machine code . Such an object file is known as a module .
  3. Using a linker to combine different modules to an object file to create an executable file. The modules that are being added to an object file through a linker contain functions that are either part of the program that has been compiled before-hand or contain functions from a standard library.
When creating a source file, it is important to note the extension which is being applied to the filename of the source file. Generally, the type of extension that is applied to the source file depends on the compiler being used to create the source file. The most commonly used file extensions are .cpp and .cc .
In some cases, the source file may require an extension file before it can be compiled. These files are known as header files. If a source file has corresponding header files available for the source code, then it becomes necessary to copy the contents of the header file into the source file before compiling it. Header files contain important data for the source file, such as defining the types, declaring the variables, and functions. Header files may or may not have a file extension. In cases where it does have an extension, then it is the .h file extension.
To perform the steps and tasks mentioned previously, programs use software known as a ‘compiler.’ Popular compilers nowadays offer an IDE along as well to get started with programming right away.
Creating a Simple C++ Program
We will now create a standard and simple C++ program. Afterward, we will proceed to discuss the coding elements used in this program.
Here’s the code for the C++ program:
#include <iostream>
using namespace std;
int main()
{
cout << "Have a good day! << endl;
return 0;
}
The output of this program is a text; “Have a good day!”.
Before we delve into understanding the core elements of this program, here’s a visual representation showing the structural arrangement of the program to establish a basic understanding.
For simplification purposes, we can say that major parts of a basic C++ program are the objects and their corresponding ‘member functions and global functions.’ These objects and functions are not part of any class. Apart from completing the task for which it is made, a function also has the capability of calling other functions as well. Experienced programmers have the choice of either creating a function by themselves or use a function that is already available in the C++ standard library. For beginner’s it’s recommended to only use pre-built functions from the standard library as creating a personalized function may cause complications in the code that can be hard to work out. However, every programmer is required to create his own global function main() . This function is one of the main components of a program’s code.
The C++ program demonstrated above contains two of the most crucial elements of any C++ program, i.e., the main­() function and the output string (the message that is displayed as the output of the program).
If you look at the first line of the code block, then you will see a hashtag symbol ‘#.’ This instructs the computer that this line of code is for the preprocessor step. You can think of preprocessor as a preliminary phase where no object is created yet, and code prepares for the upcoming instructions in the program. For instance, if you want the preprocessor to copy a file into the position where it is defined in the code of the source file, then you can use the ‘# ’ symbol to do so. This has also been demonstrated below:
#include <filename>
In this way, we can include header files into the source code. By doing so, the program will be able to access the data in the header files. In the C++ program shown above, we can see in the very first line that the header file by the name of ‘iostream’ has been included in the source code through this method. The data contained by this file corresponds to the conventions that define input and output streams. In other words, the information in the program is considered as a stream of data.
The second line of code in the program refers to a namespace known as std, which contains predefined names in C++. To access the contents of this namespace, we use the using argument before defining the namespace.
The execution of the program itself begins from carrying out the instruction defined by the main() function. Hence, every C++ program needs to have the main() function to execute an instruction set. However, although the main() function is a global function that makes it different from other functions, the structure of how code is implemented with the function is exactly the same as any other typical function in C++.
In the C++ program demonstrated above, the main() function contains two statements,
  1. cout << "Have a good day!" << endl;
  2. return 0;
The first statement has two important components, cout and endl . Cout has been taken from the C++ standard namespace, and it represents ‘console output.’ Cout has been used to designate an object that will be handle outputting the text string defined in the statement. Moreover, the ‘<<’ symbols represent the stream of the data in the program. These symbols tell the program that the characters in the string are supposed to flow to the output stream. In the end, endl indicates that the statement has finished and generates a line feed.
The second statement stops the execution of the main() function, and in this case, the program itself. This is done by returning a value of 0, which is an exit code to the calling program. It is always recommended to use the exit code 0 when highlighting that the program has been successfully terminated.
Creating a C++ Program that has Multiple Functions
Now that we know how a basic C++ program works and what core elements make up a simple program, let’s proceed to understand a program that uses several functions instead of one. An example of such a C++ program has been shown below, also note that this program also has comments which are notes left by the programmer detailing what a specific line of code’s purpose is. The comments are written after two back-slash symbols ‘//’, and the compiler doesn’t consider this as executable code and ignores it.
#include <iostream>
using namespace std;
void line(), message(); // Prototypes
int main()
{
cout << "Hey! The program starts in main()."
<< endl;
line();
message();
line();
cout << "At the end of main()." << endl;
return 0;
}
void line() // To draw a line.
{
cout << "--------------------------------" << endl;
}
void message() // To display a message.
{
cout << "In function message()." << endl;
}
Now let’s execute this program and see what’s the output.
Hey! The program starts in main().
-----------------------------------
In function message().
-----------------------------------
At the end of main().
Through this example, we can understand the structure of a C++ program more intricately. Notice that unlike the simple program shown previously, which only had one function, this C++ program features several functions. However, C++ does not restrict the user to a defined order according to which the functions need to be defined. In other words, when working with a C++ program, you can define functions in any order that you may choose. For instance, you can define the message() function first, then the line() function, and finally, the main() function or in another order that you want.
However, it is recommended that you define the main() function first. This is because this function essentially controls the program flow. To understand this, we need to take a look at what the main() function primarily does. It calls those functions that haven’t been defined yet into the program where it’s being used. The main() function does this by providing the compiler with the prototype function. The prototype function contains all the necessary information for the compiler to help the main() function do its job.
In addition, if you take a look at this program, you’ll find some sentences that start after ‘//’ symbol. These are strings, and the program interprets them as comments. Comments are very useful when working on big projects that involve writing thousands of lines of code. By using comments, the programmer can essentially leave reminders as to what is the purpose of this line of code or an entire block of code, making it easier to debug, or for other users accessing the code to understand it’s structure. Moreover, comments also make it easier for programmers to make changes to the code later on. Hence, programmers practicing this habit often divert some hefty workload later on in the future.