Operator Overloading SASTRA University C++ First Year

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Unary operator overloading

Prefix-Increment (++x) Prefix Decrement (--x)


int x = 8; x = 8;
//Prefix increment operator //Prefix decrement operator
Int a = ++x; Int b = --x;
// a is 9 // b is 7
Postfix-Increment (x++) Postfix Decrement (x--)
int x = 8; x = 8;
// Postfix Increment operator // Postfix Decrement operator
Int a = x++; Int b = x--;
// a is 8 and x = 9 // b is 8 and x = 7

#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a=10,b=20; int a=10,b=20;
++a; a++;
++b; b++;
cout<<a<<"\t"<<b; cout<<a<<"\t"<<b;
return 0; return 0;
} }
11 21 11 21
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a=10,b=20,x,y; int a=10,b=20,x,y;
x=++a; x=a++;
y=++b; y=b++;
cout<<x<<"\t"<<y<<endl; cout<<x<<"\t"<<y<<endl;
cout<<a<<"\t"<<b; cout<<a<<"\t"<<b;
return 0; return 0;
} }
11 21 10 20
11 21 11 21

There are three important things in operator overloading

1. Operator keyword
2. Operator arguments
3. Operator return values

Syntax of Operator Overloading

return_type operator op(argument_list)

// body of the function.

}
Prefix increment (++) operator overloading

#include<iostream> void display() {


using namespace std; cout << a << "+\t" << b << "i"
class complex { << endl;
public: }
int a, b; };

void getvalue() int main() {


{ complex obj;
cout << "Enter the Two obj.getvalue();
Numbers:"; ++obj; //obj.operator ++()
cin >> a>>b; cout << "prefix Increment
} Complex Number\n";
obj.display();
void operator ++() return(0);
{ }
++a; Enter the Two Numbers:
++b; 10 20
} prefix Increment Complex
Number
11+ 21i
Prefix decrement (--) operator overloading

#include<iostream> void display() {


using namespace std; cout << a << "+\t" << b << "i"
class complex { << endl;
public: }
int a, b; };

void getvalue() { int main() {


cout << "Enter the Two complex obj;
Numbers:"; obj.getvalue();
cin >> a>>b; --obj; // obj.operator --()
} cout << "prefix Increment
Complex Number\n";
void operator --() obj.display();
{ return(0);
--a; }
--b; Enter the Two Numbers:
} 10 20
prefix Increment Complex
Number
9+ 19i
Postfix Notation

#include<iostream> void display() {


using namespace std; cout << a << "+\t" << b << "i"
class complex { << endl;
public: }
int a, b; };

void getvalue() { int main() {


cout << "Enter the Two complex obj;
Numbers:"; obj.getvalue();
cin >> a>>b; obj++; // obj.operator ++()
} cout << "postfix Increment
Complex Number\n";
void operator ++(int) obj.display();
{ return(0);
a++; }
b++; Enter the Two Numbers:
} 12 14
postfix Increment Complex
Number
13+ 15i

#include<iostream> void display() {


using namespace std; cout << x << "+\t" << y << "i" <<
class complex { endl;
public: cout << a << "+\t" << b << "i"
int a, b,x,y; << endl;
void getvalue() { }
cout << "Enter the Two };
Numbers:"; int main() {
cin >> a>>b; complex obj;
} obj.getvalue();
void operator ++(int) obj++; //++obj(error)
{ cout << "postfix Increment
x=a++; Complex Number\n";
y=b++; obj.display();
} return(0);
}
Enter the Two Numbers:
25 45
postfix Increment Complex
Number
25+ 45i
26+ 46i

Operator Return Values

#include<iostream> void display() {


using namespace std; cout << x << "+\t" << y << "i" <<
class complex { endl;
public: }
int a, b,x,y; };

void getvalue() { int main() {


cout << "Enter the Two complex obj,obj1;
Numbers:"; obj.getvalue();
cin >> a>>b; obj1= ++obj;
} cout << "prefix Increment return
Complex \n";
complex operator ++() obj1.display();
{ return(0);
complex c; }
c.x=++a; Enter the Two Numbers:
c.y=++b; 20 35
return c; prefix Increment return Complex
} 21+ 36i
Nameless Temporary Objects

#include<iostream> void display() {


using namespace std; cout << a << "+\t" << b << "i"
class complex { << endl;
public: }
int a, b; };

void getvalue() { int main() {


cout << "Enter the Two complex obj,obj1;
Numbers:"; obj.getvalue();
cin >> a>>b; obj1= ++obj;
} cout << "prefix Increment return
complex number\n";
complex operator ++() obj1.display();
{ return(0);
++a; }
++b; Enter the Two Numbers:
return *this; 20 35
} prefix Increment return Complex
Number
21+ 36i
Overloading unary – operator

#include<iostream> void operator - ()


using namespace std; {
n=-n;
class sign }
{ };
int n;
public: int main()
void get(int x) {
{ sign s;
n=x; s.get(10);
} -s;
void display() s.display();
{ return 0;
cout << "value of n is: " << n; }
} value of n is: -10

Overloading Binary Operators

Arithmetic Operators (overloading + operator) (Method 1)

#include<iostream> int main()


using namespace std; {
class sum { sum ob1, ob2;
public: ob1.getdata(10,20);
int x, y; ob2.getdata(20,30);
void getdata(int a, int b) ob1+ob2; // ob1.operator +(ob2)
{ return 0;
x=a; }
y=b;
} Sum of X:30
void operator+(sum &ob) Sum of Y:50
{
x=x+ob.x; // 10+20
y=y+ob.y; // 20+30
cout<<"\nSum of X:"<<x;
cout<<"\nSum of Y:"<<y;
}
};
Arithmetic Operators (overloading * operator)

#include<iostream> int main()


using namespace std; {
class sum { sum ob1, ob2;
public: ob1.getdata(10,20);
int x, y; ob2.getdata(20,30);
void getdata(int a, int b) ob1*ob2; // ob1.operator *(ob2)
{ return 0;
x=a; }
y=b;
} Sum of X:200
void operator * (sum &ob) Sum of Y:600
{
x=x*ob.x; // 10*20
y=y*ob.y; // 20*30
cout<<"\nSum of X:"<<x;
cout<<"\nSum of Y:"<<y;
}
};

Arithmetic Operators (overloading + operator) (Method 2)

#include<iostream> void display()


using namespace std; {
class sum { cout<<"\nSum of X:"<<x;
public: cout<<"\nSum of Y:"<<y;
int x, y; }
void getdata(int a, int b) };
{ int main()
x=a; {
y=b; sum ob1, ob2,obj3;
} ob1.getdata(10,20);
sum operator+(sum &ob) ob2.getdata(40,35);
{ obj3=ob1+ob2; // ob1.operator +(ob2)
sum c; obj3.display();
c.x=x+ob.x; // 10+40 return 0;
c.y=y+ob.y; // 20+35 }
return c;
Sum of X:50
} Sum of Y:55

Arithmetic Operators (overloading + operator) (Method 3)

#include<iostream> void display()


using namespace std; {
class sum { cout<<"\nSum of X:"<<x;
public: cout<<"\nSum of Y:"<<y;
int x, y; }
void getdata(int a, int b) }; int main()
{ {
x=a; sum ob1, ob2,obj3;
y=b; ob1.getdata(10,20);
} ob2.getdata(40,35);
sum operator+(sum &ob) obj3=ob1+ob2; // ob1.operator
{ +(ob2)
x=x+ob.x; // 10+40 obj3.display();
y=y+ob.y; // 20+35 return 0;
return *this; }
Sum of X:50
} Sum of Y:55
Concatenating Strings (Method1) (overloading + operator)

#include<iostream> void operator+(app x)


#include<cstring> //Concatenating String
//#include<string.h> {
using namespace std; strcat(str,x.str);
class app }
{ };
public: int main()
char str[20]; {
public: app str1, str2;
void getstring() str1.getstring();
{ str2.getstring();
cout<<"\n Enter String:"; str1+str2;
cin>>str; str1.putstring();
} return 0;
void putstring() }
{ Enter String: Viswanathan
cout<<str; Enter String: Anand
} ViswanathanAnand
Concatenating Strings (Method 2)

(Overloading + operator)

#include<iostream> int main()


#include<cstring> // {
#include<string.h> app str1, str2, str3;
using namespace std; str1.getstring(); // MARY
class app str2.getstring(); // KOM
{ str3=str1+str2;
public: str3.putstring();
char str[20]; return 0;
public: }
void getstring()
{ Enter String:Mary
cout<<"\n Enter String:"; Enter String:Kom
cin>>str; MaryKom
}
void putstring()
{
cout<<str;
}
app operator +(app x)
{
app a;
strcat(str,x.str); //MaryKom
strcpy(a.str,str); // Copying MaryKom
return a;
}
};
Concatenating Strings (Method 3)

(Overloading + operator)

#include<iostream> int main()


#include<cstring> // {
#include<string.h> app str1, str2, str3;
using namespace std; str1.getstring();
class app str2.getstring();
{ str3=str1+str2; //String is
public: concatenated.
char str[20]; cout<<"\nConcatenated String is:";
public: str3.putstring();
void getstring() return 0;
{ }
cout<<"\n Enter String:";
cin>>str; Enter String:Sachin
} Enter String:Tendulkar
void putstring() Concatenated String is :
{ SachinTendulkar
cout<<str;
}
app operator +(app x)
{
strcat(str,x.str);
return *this;
}
};
Overloading (<) comparison operator (Method 1)

#include<iostream> else
using namespace std; return(a);
class comp }
{ };
public: int main()
int a; {
comp(int x) int s;
{ comp obj1(10);
a=x; comp obj2(20);
} s=obj1<obj2;
int operator <(comp c) cout<<s;
{ return 0;
if(a<c.a) }
return(c.a); 20
Overloading (<) comparison operator (Method 2)

#include<iostream> int main()


using namespace std; {
class comp int s;
{ comp obj1(10);
public: comp obj2(20);
int a; s=obj1<obj2;
comp(int x) cout<<s;
{ return 0;
a=x; }
} 20
int operator <(comp c)
{
return(a<c.a)?c.a:a;
}
};
Overloading (<) comparison operator (Method 3)

#include<iostream> int main()


using namespace std; {
class comp comp obj1(30);
{ comp obj2(20);
public: if(obj1<obj2)
int a; cout<<obj2.a<<"is big";
comp(int x) else
{ cout<<obj1.a<<"is big";
a=x; return 0;
} }
bool operator <(comp c) 30 is big
{
return(a<c.a)?true:false;
}
};

Comparing Strings

Return Value from strcmp()

Return Value Remarks

0 if both strings are identical (equal)

if the ASCII value of first unmatched character is less than


negative
second.

positive if the ASCII value of first unmatched character is greater


integer than second
Normal string comparison program

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;

result = strcmp(str1, str2);


cout<<"strcmp(str1, str2) = "<< result<<endl;

result = strcmp(str2, str1);


cout<<"strcmp(str2, str1) = "<< result<<endl;

result = strcmp(str1, str3);


cout<<"strcmp(str1, str3) ="<<result;

return 0;
}
strcmp(str1, str2) = 32
strcmp(str2, str1) = -32
strcmp(str1, str3) =0
Overload string comparison (Method 1)

#include <iostream>
#include<string.h>
using namespace std;

class comp
{
public:
char str[20];
comp(char ss[])
{
strcpy(str,ss);
}
bool operator ==(comp y)
{
return(strcmp(str,y.str)==0)?true:false;
}
};

int main()
{
comp obj1("abcd");
comp obj2("abcd");
if(obj1==obj2)
cout<<"Strings are equal";
else
cout<<"Strings are not equal";
return 0;
}
Strings are equal
Overload string comparison (Method 2)

#include <iostream>
#include<string.h>
using namespace std;

class comp
{
public:
char str[20];
void getstring()
{
cout<<"Enter a string:";
cin>>str;
}
bool operator ==(comp y)
{
return(strcmp(str,y.str)==0)?true:false;
}
};

int main()
{
comp obj1,obj2;
obj1.getstring();
obj2.getstring();
if(obj1==obj2)
cout<<"Strings are equal";
else
cout<<"Strings are not equal";
return 0;
}
Enter a string: software
Enter a string: software
Strings are equal
Overload string comparison (Method 3)

#include <iostream>
#include<string.h>
#include <string>
using namespace std;

class comp
{
public:
string str;
comp(string h)
{
str=h;
}
bool operator ==(comp y)
{
return(strcmp(str.c_str(),y.str.c_str())==0)?true:false;
// strcmp is for C strings (null-terminated char *). string::compare is for C++ strings.
}
};

int main()
{
comp obj1("abcd");
comp obj2("abcd");
if(obj1==obj2)
cout<<"Strings are equal";
else
cout<<"Strings are not equal";

return 0;
}
Strings are equal
Overloading Arithmetic Assignment Operator

using namespace std; cout<<"Feet:"<<feet<<endl;


#include<iostream> cout<<"Inch:"<<inch;
class arith }
{ };
int feet,inch; int main()
public: {
arith(int a,int b) arith a1(10,20);
{ arith a2(30,40);
feet=a; a1+=a2;
inch=b; return 0;
} }
void operator +=(arith ss) Feet:40
{ Inch:60
feet+=ss.feet;
inch+=ss.inch;
Overloading the Subscript Operator []

#include <iostream> void operator[](int i)


using namespace std; {
class sub { cout<<a[i];
int a[3]; }
public: };
sub(int i, int j, int k) { int main()
a[0] = i; {
a[1] = j; sub ob(10, 25, 38);
a[2] = k; ob[1];
} return 0;
}
25
Overloaded [] Operator Returning by Reference

#include <iostream> int& operator[](int i)


using namespace std; {
class sub return a[i];
{ }
int a[3]; };
public: int main()
sub(int i, int j, int k) {
{ sub ob(10, 25, 38);
a[0] = i; cout<<ob[2]<<endl; // displays 38
a[1] = j; ob[2] = 85; // [] on left of =
a[2] = k; cout<<ob[2]; // now displays 85
} return 0;
}
38
85

In C++, following operators cannot be overloaded:

. (Member Access or Dot operator)

?: (Ternary or Conditional Operator )

:: (Scope Resolution Operator)

.* (Pointer-to-member Operator )

sizeof (Object size Operator)

You might also like