C++ for Beginners for Programmers

C++ is one of the most popular programming languages in the world, easily landing a well-deserved space on the top ten list of most used languages. It is in good company alongside heavyweights including Java, Python, C, C#, and JavaScript.

C++ is a member of the C type family of languages, which include C, C#, Objective C, and – depending upon whom you ask – Java. In this developer tutorial, we will be learning the basics of this powerful programming language, starting with its history.

History of C and C++

The C programming language was developed by Dennis Ritchie and Ken Thompson, both working at Bell Laboratories, between 1969 and 1973. It was used to program Unix systems. The book, “C Programming Language”, written by Dennis Ritchie and Brian Kernighan in 1978, is partially responsible for C’s popularity among the developer community.

C++, meanwhile, was first created in 1979 by Bjarne Stroustrup, a researcher at Bell Labs. While doing his PhD, Stroustrup was working on developing a C version that supported the features and class capabilities of Object-Oriented tools. This work was supported by a group including Dennis Ritchie and his team, the creators of the C programming language. This developed version was first called “CFront”. The potential of the resulting product was discovered in a short time and the increment operator (++) was added in 1983, and its name was changed to C++ (C Plus Plus).

C++ has undergone three major evolutions since then. The first in 1985 and the second in 1990. The third occurred during the standardization of C++. Following the evolution that occurred in 1990, several works were initiated to standardize C++. ANSI and ISO combined to form a committee and the first proposed bill was made on January 25, 1994. The features defined by Stroustrup were retained and additional features were added to them.

As a result, there are two versions of C++. The first is the Traditional C++ version, based on Stroustrup’s original design. The second is Standard C++, created by Stroustrup and the ANSI/ISO Standards Committee. These two versions of C++ are basically very similar. However, Standard C++ has several additional features not found in Traditional C++ and is an enhancement of it.

C++ has its own object-oriented library. However, it still supports all of the functions in the C standard library. It contains the control structures and data types included in C.

What’s New in C++

To better understand the changes in C++, we will give two examples of traditional and standard C++ skeleton structures.

The following code structure is an example of a traditional C++ structure:

#include 
int main()
{
 // codes
 return 0;
}

Let’s pay special attention to the #include statement in the above program. With this statement, we include the iostream.h library, which enables C++ to support the I/O (input/output) system. It has the same features as the stdio.h library used in the classical C programming language.

The following code structure is an example of standard C++’s skeleton system:

#include 
using namespace std;
int main()
{
  // codes
  return 0;
}

Before you can use a function in a library, you must include its header file in the program. To do this, you need to use the #include structure.

C++ used the same header style as C within a year or two of its initial creation. While C++ still supports C-style headers for header files you create and for compatibility with older versions, it has introduced a new header style that is used by its own library. These new-style headers specify standard identifiers that can be linked-to files by the compiler, not filenames. Therefore, these styles do not have the .h extension.

C++ includes the entire C function library and supports the C library’s header files as well. For example, header files such as stdio.h and ctype.h are still valid. In C++, however, a “c” prefix has been added to filenames in C’s standard headers, and the “.h” has been dropped. For example, C++’s new style header for math.h is .

Another new feature is namespaces. Namespaces are areas reserved for defining library functions and other elements. The purpose of using Namespace is to properly place identifiers to avoid name conflicts. When we add a new header to our program, the content of that header is placed inside the std namespace and stored there.

Here is an example of this:

#using namespace std;

Once this statement is compiled, it doesn’t matter whether we’re working with old or new titles.

One of the important new features is in C++’s I/O console. Instead of I/O functions like printf() and scanf(), it proposes a different way by using I/O operators like “<<“ and “>>”.

By using the << output operator instead of the printf() function, it is possible to create an output statement or any valid C++ statement.

cout << "Technology Advice" \n";

Using the >> input operator instead of the scanf() function can be inputted from the keyboard; for example, with the following statements, we can assign an integer value to the num variable by entering from the keyboard.

int num; 
cin >> num;  

Of course, while doing these, I would like to point out once again that we need to add the header to be able to use I/O operators such as << and >>.

Read: Understanding the Utility of IOStreams in C++.

C++ Basics

Now that we know the history of C++ and how it is similar to the C programming language, we can begin to learn some of the basics oc C++ development.

C++ Character Set

Every programming language has some unique characters. For example, the * character means only multiplication in BASIC languages, while in C++ it is used in many different functions besides multiplication.

This may apply to other characters as well. For example, the ^ sign means exponentiation in BASIC languages, while in C++ this character has no such meaning.

In the table below you see the characters you can use in C++.

C++ Characters

It doesn’t matter to the compiler whether whitespace characters are used in the program or not. However, this is very important for those of us who write programs in C++. In order to facilitate the readability of the programs we write, whitespace characters come at the beginning of the characters that we will use frequently.

In the table below you see the functions of the Whitespace characters and other characters:

C++ Special Characters

Structure of C++ Programs

Let’s think of a very simple example to better understand the structure of C++ Programs. Observe the following C++ code:

Code:
// My first C++ program

#include 
using namespace std; 

int main() 
{
   cout << "Hello CPlusPlus..." << "\n";
   cout << "Hello Programming..."; 
return 0;
}

If you were to run this code in an integrated development environment (IDE) or code editor, you would get the following output:

Hello CPlusPlus...
Hello Programming...

When a program written in C++ is run, lines starting with ‘//’ are skipped, not executed. Lines starting with ‘//’ characters are comment lines that describe what is done in certain lines (code pieces) of the program. The characters ‘/*’ and ‘*/’ are used to mark multiple lines as comments in C++. The ‘/*’ characters indicate where the comment begins, and the ‘*/’ characters indicate where the comment ends.

We need to ask permission from C++ for the ‘cout’ expression we will use in our program. Not just for the ‘cout’ expression, but for the ‘cin’ etc we will see later. Here we are using the std:: construct:

std::cout << "Hello Cpp.." << "\n"; 
std::cout << "Hello Software.."; 

However, the ‘using namespace std;’ statement ensures that the use of the std namespace is included in the program to avoid name conflicts, and thus ‘cout’, ‘cin’ etc. So there is no need to use std:: anymore.

Every C++ program includes a function called ‘main’. Functions, on the other hand, are pieces of code that we call within the program and that do a certain job and return the result to us. A C++ program consists of one or more functions.

The ‘main’ function is a special function where the program first starts running. The ‘int’ statement at the beginning of the ‘main’ statement defines what kind of data the function returns to the calling system after it completes its job.

In addition, the ‘{‘ character in our example indicates the beginning of the ‘main()’ function and the ‘}’ character indicates the end of the said function, and the parts between these characters are called ‘block’. A function consists of one or more blocks placed between the ‘{‘ and ‘}’ signs.

Blocks contain statements. Statements are commands that perform the operations necessary for the program to achieve its purpose. Statements semicolon ‘;’ ends with a sign. In a line, statement breaks ‘;’ more than one statement can be included, provided that they are marked with a sign. ‘;’ at the end of a line if there is no sign, it is understood that the statement in this line continues on the next line.

The ‘return 0’ statement causes the program flow to exit this function and return to the line that called it. Since our ‘main()’ function is not called by any code, the program returns to the system, or ends.

C++ source programs are saved with a name after they are created. In order for the compiler to detect the program, the source file must have the extension ‘.cpp’.

The recorded program is compiled. If no error is encountered as a result of the compilation, the ‘.obj’ file is created.

An application file with the extension ‘.exe’ is created by applying the build process to the compiled program. By running this file, the expected results from the program are obtained.

Functions in C++

Functions are the basic building blocks of the C++ programming language. They are program snippets that have a specific name and are called by these names.

The general structure of a function is as follows:

type function_name (parametres)
{
   Statements
   return;
}

Functions are designed to produce a value or a result. Therefore, they must have a data type. For example, if the function will produce a value, the data type is defined as int. In this case, the return statement is used to return the result from the function. However, if no value is returned, void is defined as the data type of the function.

Here is some example code showcasing the above:

#include 
using namespace std;

// This program displays a message

int main() 
{ 
   cout << "This is a C++ "; 
   cout << "program. " << "\n"; 
   return 0; 
} 

This results in the following output:

This is a C++ program.

Here is another example:

 

#include 
using namespace std;

int func1(); 
int i=1; int j=2;

int main() 
{ 
   cout <<"First Number Value: " << i <<"\n"; 
   func1(); 
   return 0; 
}

int func1() 
{ 
   cout <<"Second Number Value: " << j <<"\n"; 
   return 0; 
} 

This is the result of running the above code in your code editor:

First Number Value: 1
Second Number Value: 2

The C++ main() Function

A C++ program can contain many functions, but there must always be a main() function. When the program starts executing, first a startup routine linked inside the program runs, which in turn calls the main() function. Here is how that looks in code:

#include 
using namespace std;

int main() 
{
   int num; 
   num=100; 

   cout << "Number: " << num << "\n"; 
   return 0; 
} 

This code outputs:

Number: 100

C++ Preprocessor

The relationship of C++ programs with their compilers is provided with the help of the C++ preprocessor. Preprocessors, consisting of various orders, are used to control the source code of the C++ compiler.

Preprocessor orders begin with the (#) sign in the program. C++’s most used preprocessor commands are defined by #include and #define. Preprocessor orders are not terminated with a (;) sign.

#include

This command ensures that a resource file is included in the program. Files included in C++ are called Header Files. Header files contain information about standard library functions, and in order to use these functions, it is necessary to include these files in the program.

In the following program, the “<<“ operator is used to print a message inside the main() function. To use this operator, the header file must be included in the program. If not, the program will not be able to interpret these operators.

#include 
using namespace std;

int main()
{
   int volume; 
   volume=99; 
   
   cout << "Volume= " << volume << " m3" << "\n";
   return 0; 
}

The output of this code is:

Volume= 99 m3

#define

This command allows symbolic literals to be defined. The preprocessor in question is identified with a name and a value. Although it is not a necessity to write the name used in capital letters, it is accepted as a general habit.

#include 
#define LAST 50

using namespace std; 

int main() 
{
   cout << "Value: " << LAST<< "\n"; 
   return 0; 
}  

The following is the result of running this code in your code editor:

Value: 50

Comment Lines in C++

Especially in long C++ programs, in order to know what operation the program parts perform, it is useful to record statements between the lines that indicate the purpose for which the code was written.

To add comment lines anywhere in a C++ program, add (//) to the beginning of the comment. Statements starting with this sign are ignored by the C++ compiler and are not processed. Here is an example showing C++ comments at work:

#include 
using namespace std; 

// This program reads a value 
// entered from the keyboard 
// and displays it on the screen.

int main()
{ 
   int number;
 
   cout << "Enter a number:"; cin >> number;
   cout << "Number: " << number << "\n"; 
   return 0; 
} 


Here is the output:

Enter a number:35
Number:35

C++ for Beginners

If you have read all of this, you have taken the first step into the world of C++ programming. Of course, this is a very long and arduous journey. However, I think this article is a good step for beginners to learn about C++ development.

C++ language is an intermediate-level language. It can be said that it is more efficient when necessary optimization is made from high-level programming languages.

Many artificial intelligence tools, game engines, driver software, Operating Systems,, and even many software languages are created with C++. It would not be wrong to say that C++ promises you an endless universe. If you love to read and research, if you want to tinker with a lot of things, and really want to dig deep into software, then exploring C++ would be a great journey. I wish you success with this powerful language.

Read: How to Operate on Strings in C++

Fatih Kucukkarakurt
Fatih is an engineer who has taken great strides in the fields of Mathematics, Technology and Engineering. He is the author of a C programming book as well as some C/C++ courses. A developer who has found himself in the world of mathematics and computers since an early age. Besides Game Development, Data Science and Machine Learning, he is now trying to specialize in Cyber Security.

More by Author

Must Read