0% found this document useful (0 votes)
7 views

Overloading Operator

Uploaded by

sathiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Overloading Operator

Uploaded by

sathiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Operator Overloading

• Friend version of • Friend version of


Operator function is not Operator function is only
available for the following available [operator
operators: function not as member
function ] for the
1. () following operators:
2. []
3. -> 1. <<
4. = 2. >>
SATHIYA R.R
Overloading Relational Operator
#include <iostream> // overloaded < operator
using namespace std; bool operator <( Distance d)
class Distance { if(feet < d.feet) { return true; }
{ if(feet == d.feet && inches < d.inches)
private: int feet,inches; { return true; }
public: return false;
Distance(){ feet = 0; inches = 0; } } };
Distance(int f, int i){ main()
feet = f; inches= i; } { Distance D1(11, 10), D2(5, 11);
void display() if( D1 < D2 )
{ { cout << "D1 is less than D2 " << endl; }
cout <<feet << inches <<endl; else { cout << "D2 is less than D1 " <<
} endl; }
}
SATHIYA R.R
Overloading Relational Operator
#include <iostream> // overloaded < operator
using namespace std; int operator <( Distance d)
class Distance { if(feet < d.feet) { return 1; }
{ if(feet == d.feet && inches < d.inches)
private: int feet,inches; { return 1; }
public: if(feet == d.feet && inches ==d.inches)
Distance(){ feet = 0; inches = 0; } { return 2; }
Distance(int f, int i){ return 0;
feet = f; inches= i; } } }; main() { Distance D1(11, 10), D2(5, 11);
void display() int i=D1<D2;
{ if(i==1 )
cout <<feet << inches <<endl; { cout << "D1 is less than D2 " << endl; }
} else if(i==2){ cout << “equal”; }
else {cout << “D2 is less than D1”; }}
SATHIYA R.R
Overloading Assignment Operator
#include <iostream> main() {
using namespace std;
Distance D1(11, 10), D2(5, 11);
class Distance
{ int feet, inches;
cout << "First Distance : ";
public: D1.displayDistance();
Distance() cout << "Second Distance :";
{ feet = 0; inches = 0; } D2.displayDistance();
// use assignment operator
Distance(int f, int i)
{ feet = f; inches = i; } D1 = D2;
cout << "First Distance :";
void operator=( Distance D ) D1.displayDistance();
{ feet = D.feet; inches = D.inches; }
}
void displayDistance() Different versions:
{ cout << feet << inches << endl; } }; /* Distance operator =(Distance d);
Distance&
SATHIYA R.R operator =(Distance &d);*/
Overloading input and output operators
#include <iostream> friend istream &operator>>( istream &input,
using namespace std; Distance &D )
class Distance
{ int feet,inches; {input >> D.feet >> D.inches;
public:
return input;} };
Distance(){feet = 0; inches = 0;}
main()
Distance(int f, int i)
{ Distance D1(11, 10), D2(5, 11), D3;
{feet = f; inches = i; }
cout << "Enter the obj value : " << endl;
friend ostream & operator<< cin >> D3; //no cascading
(ostream &output, Distance &D ) cout << “I Dis : " << D1 ;//cascading
cout << "Second Dis :" << D2 ;
cout << "Third Dis:" << D3 << endl;}
{ /* 1.ostream/istream objects /return
type must
SATHIYA R.R be reference.2.return type can
outpu
Overloading () operator
#include <iostream> // method to display distance
using namespace std; void displayDistance()
class Distance { cout << feet<< inches << endl; }
{ int feet,inches; };
public: Distance(){ feet = 0; inches = 0; } main()
Distance(int f, int i){ feet = f; inches = i; } { Distance D1(11, 10), D2;
// overload function call cout << "First Dis : ";
Distance operator ()(int a, int b, int c) {
D1.displayDistance();
Distance D; // just put random calc
D2 = D1(10, 10, 10); // invoke ()
D.feet = a + c + 10;
cout << "Second Dis :";
D.inches = b + c + 100 ;
return D; } D2.displayDistance(); }

SATHIYA R.R
Overloading for strings
#include<iostream>
#include<bits/stdc++.h> //str3=str1+str2
using namespace std; friend MyString operator+(MyString
class MyString obj1,MyString obj2)
{ { MyString obj3;
char *value; int len; obj3.len=obj1.len+obj2.len;
Public: obj3.value=new char[obj3.len+1];
MyString(){len=0;value=0;} strcpy(obj3.value,obj1.value);
MyString(char *s) strcat(obj3.value,obj2.value);
{len=strlen(s); return obj3;
value=new char[len+1]; }
strcpy(value,s);} void display()
MyString(MyString & s) { if(len==0)
{len=s.len; {cout<<"\n String is Empty \n“;}
value=new char[len+1]; else
strcpy(value,s.value);} {cout<<"\nThe result is:"<<value<<"\n“;}}
SATHIYA R.R
Overloading for strings
//if(str1<str2) main()
friend int operator<(MyString {
obj1,MyString obj2) MyString str1("xxxyyyzzz");
MyString str2("abc");
{
str1.display();
int rel=0;int result=0; str2.display();
rel=strcmp(obj1.value,obj2.value) MyString str3;
; str3=str1+str2;
if(rel<0) str3.display();
{result=1;} if(str1<str2)
{cout<<"\n Condition is True\n“;}
return result;
else
} {cout<<"\n Condition is False \n“;}
}; }
SATHIYA R.R
Overloading [] operator
#include <iostream> int operator[](int i) //return type –int & also
using namespace std; { if( i > SIZE ){
const int SIZE = 10; cout << "Index out of bounds" <<endl;
class safearay return arr[0];
{ }
int arr[SIZE]; return arr[i]; }
public: };
safearay() main()
{ int i; {
for(i = 0; i < SIZE; i++) safearay A;
{ arr[i] = i; } cout << "Value of A[2] : " << A[2] <<endl;
} cout << "Value of A[5] : " << A[5]<<endl;
cout << "Value of A[12] : " << A[12]<<endl;
}SATHIYA R.R
Overloading ->operator
#include <iostream> Marks *operator->(){
using namespace std; return this;
class Marks{ }
int mark;
public: };
Marks(int m){ int main()
mark = m; {
}
void whatsYourMark(){ Marks anilsmark(65);
cout << "hey i got "<< mark <<" anilsmark.whatsYourMark();
marks" << endl; anilsmark->whatsYourMark();
} return 0;
}
SATHIYA R.R
Overloading , operator
#include <iostream>
using namespace std;
main()
{
int i, j; j = 10;
i = (j++, j+100, 999+j);
cout << I;
}
o/p:1010
SATHIYA R.R
Overloading , operator
#include <iostream> location location::operator,(location op2)
{ location temp;
using namespace std;
temp.longitude = op2.longitude;
class location {
temp.latitude = op2.latitude;
int longitude, latitude; cout << op2.longitude << " " << op2.latitude <<
public: endl;
location() {} return temp; }
location(int lg, int lt) { location location::operator+(location op2)
longitude = lg;latitude = lt; } { location temp;
temp.longitude = op2.longitude + longitude;
void show() {
temp.latitude = op2.latitude + latitude;
cout << longitude << " ";
return temp; }
cout << latitude << endl; } main(){
location operator+(location op2); location ob1(10, 20),ob2( 5, 30),ob3(1, 1);
location operator,(location op2); ob1.show(); ob2.show(); ob3.show();
}; ob1 = (ob1, ob2+ob2, ob3);
SATHIYA R.R
ob1.show();}
Overloading new and delete operators
#include <iostream> // Overloaded delete
using namespace std; void operator delete(void* ptr)
{ cout << "Object Destroyed" << endl;
class MyClass // Invoke the default delete operator
{ ::delete ptr;
public: }
// Overloaded new };
void* operator new (size_t sz) main()
{ cout << "Object Created" << endl; {
MyClass* obj = new
// Invoke the default new operator
MyClass();
return ::new MyClass();
delete obj;
}
}

SATHIYA R.R
Extra-Overloading new and delete
operators
#include<iostream>
//if(str1==str2)
#include<bits/stdc++.h>
friend int operator==(MyString obj1,MyString obj2)
using namespace std;
{int rel=0;
class MyString
if(strcmp(obj1.value,obj2.value)==0)
{
{rel=1;}
char *value;
return rel;}
int len;
//if(str1!=str2)
Public:MyString(){len=0;value=0;}
friend int operator!=(MyString obj1,MyString obj2)
~MyString(){ }
{int rel=0;
//String From Array
if(strcmp(obj1.value,obj2.value)!=0)
MyString(char *s)
{rel=1;}return rel;}
{len=strlen(s);
value=new char[len+1];
//if(str1<str2)
strcpy(value,s);}
friend int operator<(MyString obj1,MyString obj2)
//String using Copy Constructor
{int rel=0;int result=0;rel=strcmp(obj1.value,obj2.value);if(rel<0){result=1;}
MyString(MyString & s)
return result;}
{
friend int operator>=(MyString obj1,MyString obj2)
len=s.len;
{int rel=0;int result=0;rel=strcmp(obj1.value,obj2.value);if(rel>0 || rel==0)
value=new char[len+1];
{result=1;} return result;}
strcpy(value,s.value);
void display(){if(len==0){cout<<"\n String is Empty \n“;}
}
else{cout<<"\nThe result is:"<<value<<"\n“;}}};
//str3=str1+str2
main(){MyString str1("xxxyyyzzz"); MyString str2("abc");
friend MyString operator+(MyString obj1,MyString obj2)
str1.display(); str2.display();
{MyString obj3;
MyString str3;
obj3.len=obj1.len+obj2.len;
str3=str1+str2;str3.display();
obj3.value=new char[obj3.len+1];
if(str1<str2)
strcpy(obj3.value,obj1.value);
{cout<<"\n Condition is True\n“;}
strcat(obj3.value,obj2.value);
else{cout<<"\n Condition is False \n“;}}
return obj3;
} SATHIYA R.R
Extra-Overloading new and delete
operators
#include <iostream> main( )
#include <bits/stdc++.h> {
using namespace std; int *p = new int;
void* operator new(size_t n) *p=20;
{cout<<endl<<"overloaded new"; cout<<"\n The value is : "<<*p; //value of 20
void *ptr; delete p;
ptr = malloc(n); cout<<"\n The value after deleting is : "<<*p; //some
return ptr; } invalid value
void operator delete(void *p)
{cout<<endl<<"overloaded delete"; }
free(p);
}

SATHIYA R.R

You might also like