31
MOHIT DADU

Operator 2

Embed Size (px)

Citation preview

MOHIT DADU

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.

OVERLOADABLE / NON- OVERLOADABLE OPERATORS:

Following is the list of operators which can be overloaded:

+ - * / % ^

& | ~ ! , =

<  >  <= >= ++ --

<<  >>  == != && ||

+= -= /= %= ^= &=

|= *= <<= >>= [] ()

The operators shown in the following table cannot be overloaded.

Operator Name

. Member selection

.* Pointer-to-member selection

:: Scope resolution

? : Conditional

# Preprocessor convert to string

## Preprocessor concatenate

OPERATOR OVERLOADING EXAMPLES:

OPERATORS AND EXAMPLE

Unary operators overloading

Binary operators overloading

Relational operators overloading

Input / Output operators overloading

++ and -- operators overloading

Assignment operators overloading

Function call () operator overloading

Subscripting [] operator overloading

Class member access operator -> overloading

Binary Operators

*= Multiplication/assignment

+= Addition/assignment

–= Subtraction/assignment

–> Member selection

–>* Pointer-to-member selection

/= Division/assignment

Operator Name

!= Inequality

% Modulus

%= Modulus/assignment

&& Logical AND

&= Bitwise AND/assignment

<<  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

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:

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?

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; }

SCOPE OF VARIABLES

SCOPE OF VARIABLES:-

Definition:- Scope of variable is defined as region or part of program in which the variable is visible / accessed / valid .

all the variable have their area of functioning and out of that boundary they don’t hold their value, this boundary is called scope of the variable.

Types of Scope Of Variable :-

1. Global scope.2. Local Scope.

Global variable are those ,which are once declared and can be used throughout the life time of the program by any class or any function.

they can be assigned different values at different time in program lifetime. But even if they are declared and initialize in the same time outside the main function, then also they can be assigned any value at any point into the program.

GLOBAL VARIABLE:

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 :

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; }

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

Local variable are the variables which exist only between the curly braces, in which its declared . outside which they are unavailable and leads to compile time error.

Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:

LOCAL VARIABLE

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

Example : Block/Local Scope#include<stdio.h> void message(); void main(){int num1 = 0 ; // Local to mainprintf("%d",num1);message();} void message(){int num1 = 1 ; // Local to Function message printf("%d",num1);}

Output:

0 1

• 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 :

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 :

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.

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 programwhich means that object of this class type can be declared from anywhere in the program.For instance consider the following code fragment:

#include<iostream.h> class X Global class type X { : : };X obj1; Global object obj1 of type XInt main(){X obj2; Local object obj2 of type X:}Void function(void){ X obj3; Local object obj3 of type X:}

Example:

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 :

Global Object:

An object is said to be global object if it is declared outside all the function bodies and it means that this object is globally available to all function in the program i.e, this object can be used anywhere in the program.

#include<iostream.h>class x{ : public: int a; void fun(); }; x obj1;

int main(){ obj1.a=10;obj1.fun();}void fun2(void){obj.a=20;obj1.fun();}

NOTE : A global object can only be declared using global class type

An object is said to be local object if it is declared within a function, which means that this object is locally available to the function that declares it and it cannot be used outside the function.

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( );}

THANK

YOU