The C++ programming language is widely used to create various applications. Therefore, for those of you who are still beginners, learn C++ can be the right step to build a career as a reliable developer.
Of course there are many reasons to learn C++, whether to become a system engineer, a Game Developer, or just to learn it because of its practicality. C++ is one of the most popular programming languages in the world.
This programming language is used for everything from building operating systems to creating video games and creating 3D movies. While it may have a steeper learning curve than others, C++ has tremendous potential.
Why C++ is Hard to Learn?
C++ is known to be a more difficult language to learn than other languages like Java and Python. Mostly because C++ has a more complex syntax.
This can be a challenge for those starting out with programming. Therefore, investing time in understanding C++ learning tips can provide a number of benefits for you and your career.
What is C++ Programming Language?
The C++ programming language is a language that can be used to create various applications. For example, image processing applications, gadget software, games, to new operating systems.
This language was developed from the C programming language. No wonder that these two languages have the same syntax and code structure.
The difference is, C++ is Object Oriented Programming (OOP) while C is a procedural programming language. That is, the C + + programming language has data and functions that are united in classes and objects to work together to solve a problem.
So when you want to change the function, there is no need to change the whole program. That way, changing the code will be more flexible.
This cannot be done in the C programming language which is procedural, where data and functions are separate and must be read one by one by the compiler.
So when you want to change a function, you need to change the whole program.
Since its initial design in 1983, the C++ programming language has continued to evolve until the latest version of C++17 which was released in 2017.
Although there are several versions, the basic concept is the same so you don’t have to be confused about which one to use.
Basic Concepts of C++ Programming Language
If you want to learn C++, it is important to understand the following basic concepts of the C++ programming language:
1. C++ Variable
A variable is an identity marker that is used to hold a value. That is, the variable will indicate a location that is in the computer’s memory or RAM. So, when you create a variable, there will be one memory slot to hold that value.
C++ is a strongly typed programming language. This means that when you make a declaration, you have to give each variable a data type.
The following are the data types in C++ variables:
- bool: the data type is a boolean value, i.e. True or False
- char: data type letters from A to Z
- int: data type is a number
- float and double: data types are fractional numbers, for example 1.33
- string: data type in the form of a set of characters such as “learn c++ language”
How to Write Variables in C++ Language?
There are two steps to do this, namely declaration and initialization.
The declaration is made before the program starts. However, you can also make declarations in the middle of the program. However, the declaration must be complete before you can use the program. The following is an example declaration:
int i; // example declaration
char c;
int i, j, k; // multiple declarations
While initialization means filling in the value in the declared variable. Examples like this:
int i; // declaration
i = 10; // initialization
If done simultaneously, the result is like this:
int i=10; // example declaration and initialization
int i=10, j=11;
If you declare without initialization, or re-declare a variable that is in one scope, an error will appear. Examples like this:
int i,j;
i=10;
j=20;
int j=i+j; // compile time error
All the variables you write have their own scope of function. This means that the variable won’t work if you write it out of scope. The scope of a variable is usually curly braces.
2. Global Variables and Local Variables.
Global variables are variables that, when you declare them, can be used again in any class and function as long as the program is running. This variable is declared outside the main() function. Examples like this:
#include<iostream>
using namespace std;
int x; // Global variable declaration
int main()
{
x=10; // first initialization
cout <<"first value of x = "<< x;
x=20; // second initialization
cout <<"initialized again with value = "<< x;
}
While local variables are variables that can only function if they are declared in the main() function. That is, if it is declared outside of main() then you will encounter an error.
#include<iostream>
using namespace std;
int main()
{
int i=10;
if(i<20) // initial limit of scope
{
int n=100; // local variables declared and initialized
} // end of scope
cout << n; // Compile time error
}
Control Structure
Control structure is a basic concept for determining decisions in each line of code reading or code flow.
How does it work?
As the program reads lines of code one by one using the compiler, the compiler will find a decision point. The decision point is in the form of selection and code repetition.
The control structure will help the compiler determine whether the selection or loop will be broken or skipped.
The following is the control structure for selecting the C++ programming language:
1. If statement
The if statement is used to find the truth of a conditional expression in the form of a boolean value with a value of True or False. Here’s an example:
include< iostream.h>
int main()
{
int x,y;
x=37;
y=25;
if (x > y )
{
cout << "x is greater than y";
}
}
2. While
While is a statement that is used to repeat a number of commands as long as the conditional expression evaluates to True. Below is an example:
int x=0 ;
while (x<=10)
{
statements;
x++ or x-- or x=x+2;
}
3. Else if statement
When the compiler encounters a selection with an if statement, the CPU checks the correctness of the conditional expression. If the result is True, then the code command will be executed. However, if the result is False, the compiler will check the code with the else if statement.
void main()
{
int x,y;
x=15;
y=19;
if (x > y )
{
cout << "x is greater than y";
}
else
{
cout << "y is greater than x";
}
}
The check will be repeated in each line of code until it finds the value True. If there is no True value, the compiler will execute an else statement. However, if the else statement also does not have a value of True, then the if statement will be ignored entirely.
4. For
For is a statement to execute multiple commands repeatedly. That is, this statement is useful for creating dynamic programs. Here is an example of its use:
for(initialization; condition; increment/decrement)
{
statement-block;
}
5. Do While
Do while is a statement to execute a command, then check the conditional expression in the form of a boolean number. The do statement will execute the command, then the while will check the conditional expression.
Example of a do while statement:
do{
statement;
…
}while(conditional expression);
So when the compiler encounters a do while statement, the compiler will immediately execute the command in the do while body. To repeat the command, the compiler will check the conditional expression first.
If the conditional expression is 1 or true, the compiler will execute the command again. The loop will continue until the conditional expression evaluates to 0 or false.
Data Structure
Data structure is a feature that serves to make declarations containing groups of variables with different data types. The data structure has a struct statement. With this feature, you can create structure types that construct an object.
The following is the data structure of the C++ programming language:
- Struct: the identity of the structure to be created
- Variables: a pair of curly braces that group all variables
- Object: a declaration that takes a structure to be a data type
Below is the data structure of the C++ programming language:
structname_struct{
/*various variable declarations
*…
*/
}object_name;
You can use it like this:
struct Student {
char NIM[11];
charName[27];
char Address[41];
GPA floats;
};
C++ Syntax
Syntax is the rules for writing code to create a program or application. It includes a layout, expressions, and symbols. You need to make sure that the syntax you use is correct to prevent errors from occurring in the program you are creating.
The following is an example of the basic syntax of the C++ programming language:
#include <iostream.h> → include
using namespace std; → namespace
int main() → function main
{
cout << "learn C++ programming language";
}
Judging from the example above, the C++ programming language has three basic syntaxes, namely:
1. Main() Function
The main() function is the function that will be executed the first time you open the program. The other functions can only be executed after the main function. This means that you must use this function. The following is an example of writing it:
int main() → function main
{
cout << "learn C++ programming language";
2. Include
Include is a syntax for defining libraries to be used in a program. That is, include you can call a program that will be used in the program.
There are several libraries that can be used in the C++ language. First, iostream which contains input and output functions. Second, the libraries included in the extension such as .h, .cpp, .hpp, .c to import file headers. A header file is a file containing functions that have already been declared.
The following is an example of writing it:
#include <iostream.h>
3. Using namespace std
Using namespace std is an instruction to the compiler to use all declared functions. For example, classes, objects, and variables. The following is an example of writing it:
using namespace std;
C++ Tools
In writing C++ code, you need an editor as a tool to write programming code and a compiler to translate commands into a computer.
However, with the Integrated Development Environment (IDE) you can work more effectively because there is an editor and compiler in one application.
Some IDE applications that you can try are Eclipse and Netbeans which are quite friendly for beginners.
Conclusion
Learning the C++ programming language well allows you to easily develop a wide variety of programs. Starting from applications, games, databases to media players.
Learning the C++ programming language is not too difficult if you already know the basic concepts, including variables, control structures, data structures, syntax. Oh yes, don’t forget to master the tools needed to learn C++.
With various advantages such as portability, OOP, dynamic memory, and fast execution, C++ can make your dreams of creating useful applications come true.
This will certainly make you more enthusiastic about learning to become a reliable developer.