Professional Documents
Culture Documents
UNIT 3AC++
UNIT 3AC++
Syntax:
return type operator symbol (parameters)
{
statement1;
statement2;
}
1. create a class that defines a data type that is to be used in the overloading operation
2. declare the operator function in the public part of class. it may be either Member function
or friend function
3. defined the operator function to implement the required operations
#include<iostream>
using namespace std;
class space
{
private:int x,y,z;
public:void getdata()
{
cout<<"enter the values for x,y and z";
cin>>x>>y>>z;
}
void display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
void operator-()
{
x=-x;
y=-y;
z=-z;
}
};
int main()
{
space s;
s.getdata();
s.display();
-s;
s.display();
}
Output:
enter the values for x,y and z 10 -20 30
x=10
y=-20
z=30
x=-10
y=20
z=-30
Program to overload unary minus using friend function
#include<iostream>
using namespace std;
class space
{
private:int x,y,z;
public:void getdata()
{
cout<<"enter the values for x,y and z";
cin>>x>>y>>z;
}
void display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
friend void operator-(space &sp);
};
void operator-(space &sp)
{
sp.x=-sp.x;
sp.y=-sp.y;
sp.z=-sp.z;
}
int main()
{
space s;
s.getdata();
s.display();
operator-(s);
s.display();
}
Output:
enter the values for x,y and z 10 -20 30
x=10
y=-20
z=30
x=-10
y=20
z=-30
Overloading Binary operator
Just like unary operators, binary operators can overloaded. Binary operators require two operands.
they can be overloaded by using member functions and friend functions.
Syntax:
Object1.operator symbol+(object2)
#include<iostream>
using namespace std;
class complex
{
private:float real,imag;
public:void getdata()
{
cout<<"enter the values for real and imaginary";
cin>>real>>imag;
}
complex operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
void display()
{
cout<<real<<"+i"<<imag;
}
};
int main()
{
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1+c2; // c3=c1.operator+(c2);
c3.display();
}
Output:
enter the values for real and imaginary3.5 4.2
enter the values for real and imaginary2.3 2.1
5.8+i6.3
#include<iostream>
using namespace std;
class complex
{
private:float real,imag;
public:void getdata()
{
cout<<"enter the values for real and imaginary";
cin>>real>>imag;
}
void display()
{
cout<<real<<"+i"<<imag;
}
Implicit overloading
Explicit overloading
#include<iostream>
using namespace std;
class num
{
private:int x;
public:num(int a)
{
x=a;
}
void show()
{
cout<<x;
}
};
int main()
{
num a1(3),a2(8);
cout<<"before overloading assignment operator"<<endl;
cout<<"a1 object value of x is=";
a1.show();
cout<<"\n a2 object value of x is=";
a2.show();
a2=a1;
cout<<"\n after overloading assignment operator"<<endl;
cout<<"a1 object value of x is=";
a1.show();
cout<<"\n a2 object value of x is=";
a2.show();
}
Output:
before overloading assignment operator
a1 object value of x is=3
a2 object value of x is=8
after overloading assignment operator
a1 object value of x is=3
a2 object value of x is=3
#include<iostream>
using namespace std;
class num
{
private:int x,y;
public:num(int a,int b)
{
x=a;
y=b;
}
void operator=(num nm)
{
x=nm.x;
y=nm.y;
}
void show()
{
cout<<"\n x="<<x;
cout<<"\n y="<<y;
}
};
int main()
{
num n1(10,20);
num n2(40,50);
n1.show();
n2.show();
n2.operator=(n1);
n1.show();
n2.show();
}
Output:
x=10
y=20
x=40
y=50
x=10
y=20
x=10
y=20
When an operator (increment/decrement) is used as prefix with object, its value is incremented /
decremented before operation. the postfix use of operator increments/decrements the value of
variable after its use.
++ and -- operators are overloaded for the prefix operation work for postfix operations but
with warning message, but vice versa is not possible. To distinction between prefix and postfix
notations, a new syntax is used to indicate postfix operator overloading function.
Syntax
Operator ++ ( int ) // postfix notation
Operator ++() // prefix notation
#include<iostream>
using namespace std;
class number
{
private:int x,y;
public:number(int k)
{
x=k;
y=0;
}
//postfix
void operator++(int)
{
y=x++;
}
void operator--()
{
y=--x;
}
void show()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y;
}
};
int main()
{
number n(10);
cout<<"\n before incrementing";
n.show();
cout<<"\n after incrementing";
n++;
n.show();
cout<<"\n after decrementing";
--n;
n.show();
}
Output:
before incrementing x=10
y=0
after incrementing x=11
y=10
after decrementing x=10
y=10
Type Conversion
Process of converting from one data type to another. There are three possibilities of data
conversion:
Conversion from basic data type to user-defined data type(class type)
Conversion from class type to basic data type
Conversion from one class type to another class type
#include<iostream>
using namespace std;
class data
{
int x;
float y;
public:data(float m)
{
x=2;
y=m;
}
data()
{
x=y=0;
}
void show()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y;
}
};
int main()
{
data d=3.4;
d.show();
}
Output:
x=2
y=3.4
#include<iostream>
using namespace std;
class data
{
int x;
float y;
public:data()
{
cout<<"\n data default constructor";
x=y=0;
}
operator int()
{
return x;
}
operator float()
{
return y;
}
data(float m)
{
cout<<"\n data parameterized float constructor";
x=2;
y=m;
}
void show()
{
cout<<"\n x="<<x<<endl;
cout<<"y="<<y;
}
};
int main()
{
int i;
float j;
data d;
d.show();
d=2.5;
d.show();
i=d; // operator int() is executed
j=d; // operator float() is executed
d.show();
cout<<"\n value of i="<<i<<endl;
cout<<"value of j="<<j<<endl;
}
Output:
data default constructor
x=0
y=0
data parameterized float constructor
x=2
y=2.5
x=2
y=2.5
value of i=2
value of j=2.5
Program to convert integer to date and vice versa using conversion function in source class.
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class date
{
char d[20];
public:date()
{
strcpy(d,"");
}
date(char *e)
{
strcpy(d,e);
}
void show()
{
cout<<d;
}
};
class dmy
{
int mt,dy,yr;
public:dmy()
{
mt=dy=yr=0;
}
dmy(int m,int d,int y)
{
mt=m;
dy=d;
yr=y;
}
operator date()
{
char temp[60],dt[60];
itoa(dy,dt,10);
strcat(dt,"-");
itoa(mt,temp,10);
strcat(dt,temp);
strcat(dt,"-");
itoa(yr,temp,10);
strcat(dt,temp);
return (date(dt));
}
void show()
{
cout<<dy<<""<<mt<<""<<yr;
}
};
int main()
{
date d1;
dmy d2(1,8,10);
d1=d2;
cout<<"d1=";
d1.show();
cout<<"\n d2=";
d2.show();
}
Output:
d1=8-1-10
d2=8110
Conversion Types
include<iostream>
using namespace std;
class sample
{
int x;
public:sample(int m)
{
x=m;
}
friend void operator << (ostream &put,sample &k)
{
put<<k.x;
}
};
int main()
{
sample s(20);
cout<<s;
}
Output:
20