Operator Overloading
Operator overloading:-
The process of making an operator to different behavior in different instances is
known as the operator overloading.
Ex: Consider the operation of addition for two nos. The operation will generate
sum. If you consider operands are strings then it would produce third string by
concatenation.
The mechanism of giving a special meaning to an operator is known as operator
overloading. lost
When operator is overloaded, its original meaning is not.
Operator function must be either member function or friend function.
If we declare operator function is member function of class then member function
has no argument for unary operators and binary operators.
Defining an operator overloading:-
Syntax:
Return type class name:: operator op(argument list)
{
Function body
}
Operator:-is a keyword to precede by any operator.
Op: - the op is the operator being overloaded.
Steps for process of operator overloading:-
1. Create a class that defines the data type that is to be used in the overloading
operation.
2. Declare the operator function :- operator op () in public part of the class
3. Implementation of the operator function
Outside the class
Inside the class
op x or x op
Unary operator overloading:-
Write a C++ program for unary – operator overloading.
#include<iostream.h>
#include<conio.h>
class space
{
, int x, y, z;
public:
void getdata (int a, int b, int c);
void display ();
void operator – ();
}
void space :: getdata (int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void space :: display()
{
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
}
void space ::operator –()
{
x= - x;
y= - y;
z= - z;
}
void main()
{
Space s;
s.getdata (5,-10, 15);
s.display();
-s;
s.display();
getch ();
}
Write a C++ program for unary ++ operator overloading.
#include<iostream.h>
#include<conio.h>
class increment
{
public:
int data;
increment()
{
data=1;
Operator overloading:-
The process of making an operator to different behavior in different instances is
known as the operator overloading.
Ex: Consider the operation of addition for two nos. The operation will generate
sum. If you consider operands are strings then it would produce third string by
concatenation.
The mechanism of giving a special meaning to an operator is known as operator
overloading. lost
When operator is overloaded, its original meaning is not.
Operator function must be either member function or friend function.
If we declare operator function is member function of class then member function
has no argument for unary operators and binary operators.
Defining an operator overloading:-
Syntax:
Return type class name:: operator op(argument list)
{
Function body
}
Operator:-is a keyword to precede by any operator.
Op: - the op is the operator being overloaded.
Steps for process of operator overloading:-
1. Create a class that defines the data type that is to be used in the overloading
operation.
2. Declare the operator function :- operator op () in public part of the class
3. Implementation of the operator function
Outside the class
Inside the class
op x or x op
Unary operator overloading:-
Write a C++ program for unary – operator overloading.
#include<iostream.h>
#include<conio.h>
class space
{
, int x, y, z;
public:
void getdata (int a, int b, int c);
void display ();
void operator – ();
}
void space :: getdata (int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void space :: display()
{
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
}
void space ::operator –()
{
x= - x;
y= - y;
z= - z;
}
void main()
{
Space s;
s.getdata (5,-10, 15);
s.display();
-s;
s.display();
getch ();
}
Write a C++ program for unary ++ operator overloading.
#include<iostream.h>
#include<conio.h>
class increment
{
public:
int data;
increment()
{
data=1;