Introduction to C++, Data Types & Operators

Ad Code

Introduction to C++, Data Types & Operators

 

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 TypeSizeDescription
short2 bytesStores whole numbers from -32,768 to 32,767
int2 bytesStores whole numbers, without decimals from -32,768 to 32,767
long4 bytesStores whole numbers, without decimals from -2,147,483,648 to 2,147,483,647
float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
boolean1 bitStores true or false values
char1 bytesStores a single character/letter or ASCII values

    Literals in C++:
    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.
  • 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).
Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.

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.
OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
-SubtractionSubtracts one value from anotherx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x
--DecrementDecreases the value of a variable by 1--x

C++ Assignment Operators

These operators are used to assign values to variables.
OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

C++ Relational Operators

These operators are used to compare two values:
OperatorNameExample
==Equal tox == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

C++ Logical Operators

These operators are used to determine the logic between variables or values:
OperatorNameDescriptionExample
&& Logical andReturns true if both statements are truex < 5 &&  x < 10
|| Logical orReturns true if one of the statements is truex < 5 || x < 4
!Logical notReverse 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
DescriptionExa-
mple
ResultDec-
imal
&AND - Sets each bit to 1 if both bits are 15 & 10001 1
|OR - Sets each bit to 1 if any of the two bits is 15 | 10101 5
~NOT - Inverts all the bits~ 51010 10
^XOR - Sets each bit to 1 if only one of the two bits is 15 ^ 10100 4
<<Zero-fill left shift - Shift left by pushing zeroes in from the right and letting the leftmost bits fall off9 << 100102
>>Signed right shift - Shift right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off9 >> 1110012
>>>Zero-fill right shift - Shift right by pushing zeroes in from the left and letting the rightmost bits fall off9 >>> 101004

Misc Operators ↦ sizeof & ternary:
OperatorDescriptionExample
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.



Post a Comment

0 Comments

Ad Code