Basic C++
Basic C++ is an intermediate level programming language and is the extension of the C language. This language is not fully object-oriented, but it does have the object-oriented features. This language is highly versatile and used in many different fields. It has applications in nearly every field, ranging from GUI apps to OS development. The famous Windows 98,2000, XP, and MicroSoft Office are mainly coded in C++.
Here we will have an introduction to programming using C++. Before we start programming with C++, let us understand some basic terminologies of C++.
Basic C++ Terms
- Header File: Header files are the files that tell the compiler to call functionalities that are defined in some other files. The header files have no information on how the function is working; it is only used for the declaration purpose. For example, a very well-known header file is the #include<iostream> header file that contains all the declarations regarding the input/output commands like cout, cin, cerr, etc.
- Library: Library is the file that contains all the functionality of an instantiated function. They have a “.lib” or “.dll” extension at the end. There are further two types of libraries
- Static Libraries
- Dynamic Libraries: These library files are neither human-readable as they are in the form of machine code. They are usually added to the program in the last stage using linkers.
- Variables: A variable in C++ is like a box in which you can store certain information that is required by the program. For each kind of data, the variable type (data type) is different for each type of information, and there are three types of basic variables in C++
- INT
- CHAR
- FLOAT
- BOOL
The int variable is used to store integers, and it has a size of 4 bytes. This means that you can store numbers from -2,147,483,648 to 2,147,483,647 in an int type variable. If you want to increase the range, you can use unsigned int, which has the same size, but the range increases from 0 to 4,294,967,295.
The char type variable is used to store a single alphabetic character, and it has the size of 1 byte. Other than that, the float variable type stores any decimal number in it, and it has a size of 4 bytes. Remember that if you enter a number in a char type variable it will be treated as a character and no arithmetic operations can be performed on it.
The bool type variables are used to store the logical states like true and false. It has a size of 1 byte and stores only 0 or 1. 0 represents the false state, and 1 represents the true state. All these basic variables are used to derive further variable types like arrays, pointer, and references. Users can also use these variables to define their own data types like class, structure, and union, etc.
- Operators: The operator is the symbol that tells the compiler to perform a particular mathematical or logical task. In C++ there are four basic types of operators
- Arithmetic Operators
- Logical Operators
- Assignment Operators
- Relational Operators
Arithmetic operators include all the basic mathematical operations like addition, subtraction, multiplication, and division. Other than that, there is a modulus operator in C++ and increment operator as shown in the table below
OPERATOR | EXPLANATION |
+ | Simple mathematical addition operator |
– | Simple mathematical subtraction operator |
* | Simple mathematical multiplication operator |
/ | Simple mathematical division operator |
% | Modulus operator. It returns the remainder after division. Example if a=2 and b=3 then b % a will give you 1 |
++ | Increment operator. Will increment the value of a variable by one. For example, if a=2 then a++ will change “a” to 3 |
— | Decrement operator. Will decrement the value of a variable by one. For example, if a=2 then a– will change “a” to 1 |
The logical operators include three operators that are “&&” (AND), “||” (OR), and “!” (NOT). Bitwise operators are quite similar to logical operators, but they work on binary data. Logical operators work the same way as in gates as shown in the figure below
Assignment operators are used to assigning a value to a certain variable. Equal “=” is the basic assignment operator, but C++ also supports many others like “+=”, “-=”, “*=” etc. All it does is that it performs the operation present in the sign and assigns the value to the variable on the left. For example, C += 2 can be written as C = C + 2.
Relational operators are used to check any condition on a variable. The table below shows some of these operators with explanation
OPERATOR | EXPLANATION |
== | Checks the equality between two variables |
!= | Checks the inequality between two variables |
> | Checks if one number is greater than the other |
< | Checks if one number is smaller than the other |
>= | Checks if one number is greater or equal than the other |
<= | Checks if one number is smaller or equal than the other |
- Loops: Loops are a piece of code that helps the user to run commands until a specific condition is satisfied. Here are a few types of loops that are present in C++
- For Loop
- While Loop
- Do…. While Loop
Here is a structure of these loops
- For loop: for (Initial condition ; terminating condition ; executed at the end of each cycle)
{
// code block to be executed
}
- While loop: while (condition)
{
// code block to be executed
}
- Do while Loop: do
{
// code block to be executed
} while(condition);
The basic flow for “while” and “for” loop is the same, i.e., they check the condition and run the loop if the condition is satisfied. But in case of the “do while” loop, the code block is executed without checking the condition for the first time. After the first execution, the cycle is the same.
- Functions: If a block of code is used repetitively in a program, then it is better to define it once and use it where it is needed. This defining of code is known as functions. These functions are defined outside the main loop and have the following structure
return_type function_name (parameters)
{
// code block to be executed
}
The return type tells us about the kind of value that the function will return. Remember, the function’s name is case sensitive, and it should be unique.
Read more about this