Important Concepts: By: Mostafa Ashraf
Important Concepts: By: Mostafa Ashraf
Important Concepts: By: Mostafa Ashraf
#include “” :
Search for the file to be included in the current directory (user defined library) else
search for it in standard libraries.
type of brackets
1- parantheses ( )
2- curly brackets { }
3- square brackets [ ]
Declaration and Initialization
* Variable must be declared before they are used. Usually it is preferred to declare
them at the starting of the program, but in C++ they can be declared in the middle of
program too, but must be done before using them.
Int i ; // declaration i = 5 ; // initialization
If a variable is declared and not initialized by default it will hold a garbage value. Also,
if a variable is once declared and if try to declare it again, we will get a compile time
error.
Int I , j ;
I = 5 ; j = 10 ;
Int j = i+j; // compile time error , cannot redeclare a variable in same scope .
Scope of Variables
All the variables have their area of function, and out of that boundary they don't hold
their value, this boundary is called scope of the variable. For most of the cases its
between the curly braces,in which variable is declared that a variable exists, not outside
it. We will study the storage classes later, but as of now, we can broadly divide variables
into two main types
1 – global variable
2 – local variable
1- Global variable
Global variables are those, which are once declared and can be used throughout the
lifetime of the program by any class or any function. They must be declared outside the
Main() function. If only declared, they can be assigned different values at different time in
program lifetime. But even if they are declared and initialized at the same time outside the
main() function, then also they can be assigned any value at any point in the program.
2- local variable
Local variables are the variables which exist only between the curly braces, in which its
declared. Outside that they are unavailable and leads to compile time error.
Operators in C++
Operators are special type of functions, that takes one or more arguments and produces
a new value. For example : addition (+), subtraction (-), multiplication (*) etc…, are all
operators. Operators are used to perform various operations on variables and constants.
Assignment Operator ( = )
Operates '=' is used for assignment, it takes the right-hand side (called rvalue) and copy it
into the left-hand side (called value). Assignment operator is the only operator which can
be overloaded but cannot be inherited.
Mathematical Operators
There are operators used to perform basic mathematical operations. Addition (+) ,
subtraction (-) , diversion (/) multiplication (*) and modulus (%) are the basic
mathematical operators. Modulus operator cannot be used with floating-point numbers.
Relational Operators
These operators establish a relationship between operands. The relational operators
are : less than (<) , grater than (>) , less than or equal to (<=), greater than equal to
(>=), equivalent (==) and not equivalent (!=).
You must notice that assignment operator is (=) and there is a relational operator, for
equivalent (==). These two are different from each other, the assignment operator
assigns the value to any variable, whereas equivalent operator is used to compare
values .
Logical Operators
The logical operators are AND (&&) and OR (||). They are used to combine two
different expressions together.
If two statement are connected using AND operator, the validity of both statements will
be considered, but if they are connected using OR operator, then either one of them
must be valid. These operators are mostly used in loops (especially while loop) and in
decision making .
Bitwise Operators
There are used to change individual bits into a number. They work with only integral
data types like char , int & long and not with floating point values.
* Bitwise AND operators &
* Bitwise OR operators |
* Bitwise XOR operators ^
* Bitwise NOT operators ~
SIZEOF operator
* sizeof() is also an operator not a function, it is used to get information about the
amount of memory allocated for data types & Objects. It can be used to get size of
user defined data types too, can be used with and without parentheses. If you apply
it to a variable you can use it without parentheses.
Cout << sizeof(float);
Int x = 10 ;
Int I = sizeof (x) ;
Typedef Operator