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