C++ tutorial provides basic and advanced concepts of C++. Our C++ tutorial is designed for beginners and professionals.
C++ is an object-oriented programming language. It is an extension to C programming.
Our C++ tutorial includes all topics of C++ such as first example, control statements, objects and classes, inheritance, constructor, destructor, this, static, polymorphism, abstraction, abstract class, interface, namespace, encapsulation, arrays, strings, exception handling, File IO, etc.
C++ supports the object-oriented programming, the four major pillar of object-oriented programming (OOPs) used in C++ are:
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
By the help of C++ programming language, we can develop different types of secured and robust applications:
- Window application
- Client-Server application
- Device drivers
- Embedded firmware etc
As we know both C and C++ are programming languages and used for application development. The main difference between both these languages is C is a procedural programming language and does not support classes and objects, while C++ is a combination of both procedural and object-oriented programming languages.
The following are the important differences between C and C++.
- As C does not support encapsulation so data behave as a free entity and can be manipulated by outside code.
- C++ has support for polymorphism, encapsulation, and inheritance as it is being an object-oriented programming language.
- As mentioned before C is procedural programming.
- On the other hand, C++ supports both procedural and object-oriented programming paradigms.
Before starting the abcd of C++ language, you need to learn how to write, compile and run the first C++ program.
To write the first C++ program, open the C++ console and write the following code:
1 #include "iostream"
2 int main()
3 {
4 std::cout << "Hello World" << std::endl;
5
6 return 0;
7 }
Now that you have created your first Hello World program, we need compile it. We are assuming that you are using GNU C++ MinGW compiler. In order to do that, we need to write the following on the terminal.
// Windows Terminal
g++ -o program.exe program.cpp
// Linux
make program
Now you can run the program just writing .\program and see the output.
Hello World
A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
1 data_type variable_name;
2 int myNumber = 45;
A variable can have alphabets, digits and underscore.
A variable name can start with alphabet and underscore only. It can't start with digit.
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. char, float etc.
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
The basic data types are integer-based and floating-point based. C++ language supports both signed and unsigned literals.
The memory size of basic data types may change according to 32 or 64 bit operating system.
Let's see the basic data types. It size is given according to 32 bit OS.
Data Type | Memory Size (bytes) | Range |
---|---|---|
char | 1 | -128 to 127 |
signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 127 |
short | 2 | -32.768 to 32.767 |
signed short | 2 | -32.768 to 32.767 |
unsigned short | 2 | 0 to 32.767 |
int | 2 | -32.768 to 32.767 |
float | 4 | |
double | 8 | |
long double | 10 |
A keyword is a reserved word. You cannot use it as a variable name, constant name etc. A list of 32 Keywords in C++ Language which are also available in C language are given below.
- auto
- break
- case
- char
- const
- continue
- default
- do
- volatile
- while
An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C language.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operator
- Unary operator
- Ternary or Conditional Operator
- Misc Operator
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operators direction to be evaluated, it may be left to right or right to left.
Let's understand the precedence by the example given below:
1 int a = 3 + 5 * 3
2 std::cout << "The value of a is " << a << std::endl;
The code output will return 18, because it first compute multiplication operator (*) and after the sum operator (+).
The value of a is 18
C++ expression consists of operators, constants, and variables which are arranged according to the rules of the language. It can also contain function calls which return values. An expression can consist of one or more operands, zero or more operators to compute a value. Every expression produces some value which is assigned to the variable with the help of an assignment operator.
Let's see a simple program containing constant expression:
1 #include "iostream"
2 int main()
3 {
4 int x;
5 x = (3 * 3) + 5;
6 std::cout << "The value of x is " << x << std::endl;
7 }
The value of x is 14
Integral Expressions
An integer expression is an expression that produces the integer value as output after performing all the explicit and implicit conversions.
1 #include "iostream"
2 int main()
3 {
4 int x;
5 int y = 12;
6 x = y + int(3.0);
7 std::cout << "The value of x is " << x << std::endl;
8 }
The value of x is 15