What is C++?
C++ is an Object Oriented Programming language developed by Bjarne stroustrup at bell labs. It gives programmers a high level of control over system resources and memory. C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
The major difference C++ is an object oriented language whereas C language is a procedural language.
Data type in C++:
Data type determines the type of data a variable will hold. C++ language supports 2 different type of data types.
- Primitive data types - Under it you will use short, int, long, float, double, boolean and char
- Non-primitive data types - Under it you will use String, Arrays and Classes
- Primitive Data Types: A primitive data type specifies the size and type of variable values, and it has no additional methods. There are eight primitive data types in C++:
Data Type | Size | Description |
---|---|---|
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 2 bytes | Stores whole numbers, without decimals from -32,768 to 32,767 |
long | 4 bytes | Stores whole numbers, without decimals from -2,147,483,648 to 2,147,483,647 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean | 1 bit | Stores true or false values |
char | 1 bytes | Stores a single character/letter or ASCII values |
- Literals in C++:
- Non-Primitive Data Types: Non-primitive data types are called reference types because they refer to objects. Non-primitive types are created by the programmer and is not defined by Java (except for String).
A literal is a fixed value that we assign to a variable in a Program.
int n=10;
Here value 10 is a Integer literal.
Integer literals are assigned to the variables of data type byte, short, int and long.
Float Literals are assigned to the variables of data type float and double.
Char and String Literal Used for char and String type.
C++ Operators:
Operators are used to perform operations on variables and values. C++ divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
C++ Arithmetic Operators
These operators are used to perform mathematical operations.
C++ Assignment Operators
These operators are used to assign values to variables.
C++ Relational Operators
These operators are used to compare two values:
C++ Logical Operators
These operators are used to determine the logic between variables or values:
C++ Bitwise Operators
These operators are used to perform binary logic with the bits of an integer or long integer.
Misc Operators ↦ sizeof & ternary:
void type
void type means no value. This is usually used to specify the type of functions which returns 0 value.
Header file in C++:
A header file is a file with extension .h which contains C++ function declarations and macro. We must include the particular header file in our program when we use its function.
Example
These operators are used to perform mathematical operations.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | ++x |
-- | Decrement | Decreases the value of a variable by 1 | --x |
C++ Assignment Operators
These operators are used to assign values to variables.
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
These operators are used to compare two values:
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
C++ Logical Operators
These operators are used to determine the logic between variables or values:
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
C++ Bitwise Operators
These operators are used to perform binary logic with the bits of an integer or long integer.
Ope- rator | Description | Exa- mple | Result | Dec- imal |
---|---|---|---|---|
& | AND - Sets each bit to 1 if both bits are 1 | 5 & 1 | 0001 | 1 |
| | OR - Sets each bit to 1 if any of the two bits is 1 | 5 | 1 | 0101 | 5 |
~ | NOT - Inverts all the bits | ~ 5 | 1010 | 10 |
^ | XOR - Sets each bit to 1 if only one of the two bits is 1 | 5 ^ 1 | 0100 | 4 |
<< | Zero-fill left shift - Shift left by pushing zeroes in from the right and letting the leftmost bits fall off | 9 << 1 | 0010 | 2 |
>> | Signed right shift - Shift right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off | 9 >> 1 | 1100 | 12 |
>>> | Zero-fill right shift - Shift right by pushing zeroes in from the left and letting the rightmost bits fall off | 9 >>> 1 | 0100 | 4 |
Operator | Description | Example |
---|---|---|
sizeof() | Returns the size of a variable. | sizeof(a), where a is integer, will return 2. |
& | Returns the address of a variable. | &a; returns the actual address of the variable. |
* | Pointer to a variable. | *a; |
? : | Conditional Expression. | If Condition is true ? then value X : otherwise value Y |
void type
void type means no value. This is usually used to specify the type of functions which returns 0 value.
Header file in C++:
A header file is a file with extension .h which contains C++ function declarations and macro. We must include the particular header file in our program when we use its function.
Example
#include<iostream.h>
, #include<conio.h>
, #include<math.h>
etc.
0 Comments