C++ is an ISO-standardized programming language. It was developed in 1979 by Bjarne Stroustrup at AT&T as an extension of the C programming language. C++ enables efficient and machine-oriented programming as well as programming at a high level of abstraction. The standard also defines a standard library for which different implementations exist.
Features of C++
C++ is used in both system programming and application programming and is one of the most widely used programming languages in both. Typical applications in system programming include operating systems, embedded systems, virtual machines, drivers, and signal processors. C++ often takes the place that used to be reserved exclusively for assembly languages and the C programming language. In application programming, C++ is mainly used where high demands are placed on efficiency to make the best possible use of performance limits imposed by technical framework conditions. Beginning in 2000, C++ was pushed back from the domain of application programming by the Java and C# languages.
The C++ language uses only about 95 keywords, some of which are used multiple times in different contexts. Similar to the C language, it gets its actual functionality from the C++ standard library, which teaches the language missing important functionalities (arrays, vectors, lists, …) as well as establishes the connection to the operating system (iostream, fopen, exit, …). Depending on the area of application, additional libraries and frameworks are added. C++ focuses on the language tools used to develop libraries. As a result, it favours generalised mechanisms for typical problems and has hardly any individual solutions integrated into the language.
---
One of the strengths of C++ is the ability to combine efficient, machine-oriented programming with powerful language tools that summarize simple to complex implementation details and largely hide them behind abstract sequences of commands. Template metaprogramming comes into play here: a technique that allows an almost uncompromising combination of efficiency and abstraction. However, some design decisions are also often criticized.
C++ does not have garbage collection, but there are efforts to enable garbage collection through libraries or by including it in the language standard. It is possible to use C’s memory management (malloc/realloc/free); to implement low-level functions in libraries such as the C++ standard library, this is necessary to access C library functions. However, this is strongly discouraged in general C++ code. Instead, it is common practice to let the standard C++ library take care of memory management by using the container classes offered. The C++ Standard Library provides:
- Containers (container classes)
- Iterators
- Algorithms
- Function Objects
- Strings
- Input and Output
- Localization
- Numerics
- Exceptions
- RTTI (Runtime Type Information)
- Random Number Generators and Transformers
- Tools for multithreading
- Regular expressions
- Timekeeping tools
Most of the components of the C++ standard library are in the form of template classes. This concept has the great advantage of reusability, for example, containers for arbitrary data types can be created by simple declaration; Algorithms apply to a wide range of data types. Furthermore, templates ensure type safety during compilation, which minimizes runtime errors. One disadvantage is the extremely difficult-to-read error messages that are generated, for example, in the event of type conflicts.
You’ll find the list of all the keywords here:
1 | https://en.cppreference.com/w/cpp/keyword |
How to Compile C++ Program and Run
The easiest way to write code in C++, compile and run (for self-study & learning process) is to use the Unix/Linux Terminal. In the case of Microsoft Windows, you can use Ubuntu Bash (WSL 2).
You need to have C++, g++, and Nano installed, probably all of them are already installed:
1 2 3 | c++ --version g++ --version nano --version |
Open nano:
1 | nano hello.cpp |
Copy and paste the below example and save the file (ctrl+O):
1 2 3 4 5 6 | #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; } |
Next, compile it:
1 | g++ -o hello hello.cpp |
Next, execute it:
1 | ./hello |
You’ll get the output “Hello, World!”.

Of course, later you can use an IDE, such as Microsoft Visual Studio.
How to Write Program in C++
Hello World Program
Let’s begin with the classic “Hello, World!” program, which is often the first program written by beginners in any programming language:
1 2 3 4 5 6 | #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; } |
In this program:
#include : This line includes the input/output stream library, which provides functionality for input and output operations.int main(): Every C++ program must have a main() function, which serves as the entry point of the program.std::cout << "Hello, World!" << std::endl;: This line outputs the "Hello, World!" message to the standard output (usually the console).return 0;: This statement indicates that the program executed successfully and returns an exit status of 0.
Variables and Data Types
C++ supports various data types to represent different kinds of values, such as integers, floating-point numbers, characters, and more. Here's an example of declaring variables and using different data types:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> int main() { // Integer variable int num = 42; // Floating-point variable float pi = 3.14; // Character variable char letter = 'A'; // Output variables std::cout << "Integer: " << num << std::endl; std::cout << "Float: " << pi << std::endl; std::cout << "Character: " << letter << std::endl; return 0; } |
Control Flow Statements
C++ provides various control flow statements, such as if-else, for, while, and switch, to control the flow of program execution. Here's an example using if-else and for loops:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> int main() { int num = 10; // If-else statement if (num > 0) { std::cout << "Positive" << std::endl; } else { std::cout << "Non-positive" << std::endl; } // For loop for (int i = 0; i < 5; ++i) { std::cout << "Iteration " << i + 1 << std::endl; } return 0; } |
Functions
Functions in C++ are reusable blocks of code that perform specific tasks. Here's an example of defining and calling a function to calculate the factorial of a number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> // Function to calculate factorial int factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n - 1); } int main() { int num = 5; // Call factorial function int result = factorial(num); // Output result std::cout << "Factorial of " << num << " is " << result << std::endl; return 0; } |
Basic Calculator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> using namespace std; int main() { char op; float num1, num2; // Display menu and prompt for operator choice cout << "Enter operator (+, -, *, /): "; cin >> op; // Prompt for two numbers cout << "Enter two numbers: "; cin >> num1 >> num2; // Perform operation based on operator choice switch (op) { case '+': cout << "Result: " << num1 << " + " << num2 << " = " << num1 + num2; break; case '-': cout << "Result: " << num1 << " - " << num2 << " = " << num1 - num2; break; case '*': cout << "Result: " << num1 << " * " << num2 << " = " << num1 * num2; break; case '/': // Check for division by zero if (num2 != 0) cout << "Result: " << num1 << " / " << num2 << " = " << num1 / num2; else cout << "Error! Division by zero!"; break; default: // Display error for invalid operator cout << "Error! Invalid operator."; break; } return 0; } |
You'll get these examples from my GitHub repository too. Do not forget to follow me on GitHub.
Conclusion
This article provides a glimpse into the basics of C++ programming, covering essential concepts such as variables, data types, control flow statements, and functions, with illustrative code examples. As you continue your journey in learning C++, remember to practice regularly, explore more advanced topics, and build projects to solidify your understanding. With dedication and perseverance, you'll soon become proficient in C++ and unlock endless possibilities in software development.