This slide is completely based on the Operator Overloading and the Scope of Variable. The example given to explain are based on C/C++ programming language.
3. Definition –
Operator overloading is a technique by which operators used in a
programming language are implemented in user-defined types with
customized logic that is based on the types of arguments passed.
What does Operator Overloading mean?
Operator overloading is an important concept in c++. It is a type of
polymorphism in which an operator is overloaded to give user
defined meaning to it.
10. << Left shift
<<= Left shift/assignment
<= Less than or equal to
== Equality
>= Greater than or equal to
>> Right shift
>>= Right shift/assignment
^= Exclusive OR/assignment
|= Bitwise inclusive OR/assignment
|| Logical OR
11. The operators :: (scope resolution), . (member access), .* (member access
through pointer to member), and ?:(ternary conditional) cannot be overloaded
New operators such as **, < >, or & | cannot be created.
The overloads of operators &&, ||, and , (comma) lose their special
properties: short-circuit evaluation and sequencing.
The overload of operator -> must either return a raw pointer or return an
object (by reference or by value), for which operator -> is in turn overloaded.
Precedence and Associativity of an operator cannot be changed.
Cannot redefine the meaning of a procedure.
Restrictions:
12. To overload a operator, a operator
function is defined inside a class as:
The return type comes first which is
followed by keyword operator, followed
by operator sign,i.e., the operator you
want to overload like: +, <, ++ etc. and
finally the arguments is passed. Then,
inside the body of you want perform the
task you want when this operator function
is called.
How to overload operators in C++ programming?
13. Example of operator overloading in C++ Programming
#include <iostream.h>
class temp {
private:
int count;
public:
temp(): count(5){ }
void operator ++()
{ count=count+1; }
void Display()
{ cout<<"Count: "<<count;
}
};
OUTPUT
Count: 6
int main()
{ temp t;
++t; //operator function void
operator ++() is called
Display();
return 0;
}
14.
15. SCOPE OFVARIABLES:-
Scopeof variableis definedas region or part of program in which
the variableis visible/ accessed/ valid .
all the variablehavetheir area of functioningand out of that boundarythey don’t hold
their value, this boundaryis calledscopeof the variable.
Types of Scope OfVariable :-
1. Global scope.
2. Local Scope.
17. Variable is said to have global scope / file scope if it is defined outside
the function and whose visibility is entire program.
File Scope is also called Global Scope.
It can be used outside the function or a block.
It is visible in entire program.
Variables defined within Global scope is called as Global variables.
Variable declared globally is said to having program scope.
A variable declared globally with static keyword is said to have file
scope.
.
File Scope of Variable :
18. For example:
#include<iostream.h>
int x = 0; // **program scope**
static int y = 0; // **file scope**
static float z = 0.0; // **file scope**
int main()
{
int i; /* block scope */
/*
.
.
.
*/
return 0;
}
19. Advantages / Disadvantages of File scope / Global
Variable
Advantages of Global Variables :
If some common data needed to all functions can be declared as global to avoid
the parameter passing
Any changes made in any function can be used / accessed by other
Disadvantages of Global Variables :
Too many variables , if declared as global , then they remain in the memory till
program execution is over
Unprotected data : Data can be modified by any function
21. Block Scope of Variable :
Block Scope i.e Local Scope of variable is used to evaluate expression at block
level. Variable is said to have local scope / block scope if it is defined within
function or local block. In short we can say that local variables are in block scope..
Important Points About Block Scope :
Block Scope is also called Local Scope
It can be used only within a function or a block
It is not visible outside the block
Variables defined within local scope is called as Local variables
22. Example : Block/Local Scope
#include<stdio.h>
void message();
void main()
{
int num1 = 0 ; // Local to main
printf("%d",num1);
message();
}
void message()
{
int num1 = 1 ; // Local to Function message
printf("%d",num1);
}
Output:
0 1
23. • In both the functions main() and message() we have declared same
variable.Since these two functions are having different block/local scope,
compiler will not throw compile error.
• Whenever our control is in main() function we have access to variable from
main() function only. Thus 0 will be printed inside main() function.
• As soon as control goes inside message() function , Local copy of main is no
longer accessible. Since we have re-declared same variable inside message()
function, we can access variable local to message(). “1” will be printed on the
screen.
Explanation Of Code :
24. Example 2:
#include<iostream.h>
void message();
void main()
{
int num1 = 6 ;
Cout<<num1;
message();
}
void message()
{
cout<<num1;
}
Compile error:
Variable num1 is visible only with
in main function, it can not be
accessed by other function.
Output of Above Program :
25. Advantages / Disadvantages of Local scope / Block
Variable
Advantages of Local Variables :
Since data cannot be accessed from other functions , Data Integrity is preserved.
Only required data can be passed to function , thus protecting the remaining data.
Disadvantages of Local Variables :
Common data required to pass again and again .
They have Limited scope.
26. Class scope:
The scope of the class either global or local.
Global Class:
A class is said to be global class if its definition occur outside the
class if the definition occur outside the bodies of all function in a
program
which means that object of this class type can be declared from
anywhere in the program.
For instance consider the following code fragment:
27. #include<iostream.h>
class X Global class type X
{ :
:
};
X obj1; Global object obj1 of type X
Int main()
{
X obj2; Local object obj2 of type X
:
}
Void function(void)
{
X obj3; Local object obj3 of type X
:
}
Example:
28. A class is said to be local class if its definition occur inside a function body, which
means that the object of this class type can be declared only within the function that
define this class type.
#include<iostream,h>
Int main()
{
Class Y Local class type Y
{ :
};
Y obj1; Local object obj1 of type X
}
Void function(void)
{
Y obj2; invalid. Y type is not available in function().
:
}
Local Class :
30. NOTE:
A local object can be created from
both class types: global as well as local.
Local Object:
#include<iostream.h>
class x
{
public:
int a;
void fun( );
};
void main( )
{
x obj1;
obj1.a=10;
obj1.fun( );
}