DEV Community

Cover image for C++ For Beginners
Fatih Küçükkarakurt
Fatih Küçükkarakurt

Posted on • Edited on

C++ For Beginners

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 to program Unix systems. The book "C Programming Language", written by Dennis Ritchie and Brian Kernighan in 1978, has a great influence on C's popularity. This book; C language is used as the only reference source until it is standardized by ANSI (American National Standards Institute).

C++ 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 is 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 is in 1985 and the second is 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. 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. But 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 functions in the C standard library. It contains the control structures and data types included in C.

In addition, in the continuation of this series, we will act on ASCII.

What's New in C++

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

This code structure is an example of a traditional C++ structure:

#include <iostream.h>
int main()
{
 // codes
 return 0;
}
Enter fullscreen mode Exit fullscreen mode

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. We can think that 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 <iostream>
using namespace std;
int main()
{
  // codes
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

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's 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 <cmath>.

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.

To give 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. C++; 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 output an output statement or any valid C++ statement.

cout << "Dev.to" \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;  
Enter fullscreen mode Exit fullscreen mode

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

C++ BASICS

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++.

table 1

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:

table 2

Structure of C++ Programs

Let's think of a very simple example to better understand the structure of C++ Programs.

Code:

// My first C++ program

#include <iostream>
using namespace std; 

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

Output:

Hello CPlusPlus...
Hello Programming...
Enter fullscreen mode Exit fullscreen mode

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'll see later. Here we are using the std:: construct.

std::cout << "Hello Cpp.." << "\n"; 
std::cout << "Hello Software.."; 
Enter fullscreen mode Exit fullscreen mode

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; It 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, i.e. 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

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 the functions is as follows.

type function_name (parametres)
{
   Statements
   return;
}
Enter fullscreen mode Exit fullscreen mode

Functions are designed to produce a value or a result. Therefore, they must have a data type. e.g; 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.

Code:

#include <iostream>
using namespace std;

// This program displays a message

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

Output:

This is a C++ program.
Enter fullscreen mode Exit fullscreen mode

Code:

#include <iostream>
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; 
} 
Enter fullscreen mode Exit fullscreen mode

Output:

First Number Value: 1
Second Number Value: 2
Enter fullscreen mode Exit fullscreen mode

main() Function

A C++ program can contain many functions; but there must 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.

Code:

#include <iostream>
using namespace std;

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

   cout << "Number: " << num << "\n"; 
   return 0; 
} 
Enter fullscreen mode Exit fullscreen mode

Output:

Number: 100
Enter fullscreen mode Exit fullscreen mode

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 <iostream> header file must be included in the program. Otherwise, the program will not be able to interpret these operators.

Code:

#include <iostream>
using namespace std;

int main()
{
   int volume; 
   volume=99; 

   cout << "Volume= " << volume << " m3" << "\n";
   return 0; 
}
Enter fullscreen mode Exit fullscreen mode

Output:

Volume= 99 m3
Enter fullscreen mode Exit fullscreen mode

#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.

Code:

#include <iostream>
#define LAST 50

using namespace std; 

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

Output:

Value: 50
Enter fullscreen mode Exit fullscreen mode

Comment Lines

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.

Code:

#include <iostream>
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; 
} 
Enter fullscreen mode Exit fullscreen mode

Output:

Enter a number:35
Number:35
Enter fullscreen mode Exit fullscreen mode

Summary

If you've read all this, you've taken the first step into the world of C++. 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++.

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.

Request

I am currently working on a C++ Neural Network Library (iNeural). I would be very grateful if you could give a star to this project on Github. Thanks.


Next Lesson ==> Data Types, Variables and Constants in C++

Top comments (0)