CSIT
CSIT
CSIT
Operator Argument:
An overloaded operator has a return type and a parameter list.
Box operator+(const Box&);
declares the addition operator that can be used to add two Box objects and returns final
Box object. Most overloaded operators may be defined as ordinary non-member functions
or as class member functions. In case we define above function as non-member function
of a class then we would have to pass two arguments for each operand as follows −
Box operator+(const Box&, const Box&);
1. Unary operator overloading:
The unary operators operate on a single operand and following are the examples of
Unary operators:-
The increment (++) and decrement (--) operators.
The unary minus (-) operator.
The logical not (!) operator.
//program to show eg of prefix unary operator overloading:
#include <iostream>
using namespace std;
class Rectangle
{
private:
int l,b;
public:
Rectangle()
{
l=0;
b=0;
}
void operator ++()
{
++l;
++ b;
}
void Display()
{
cout<<"Length: "<<l<<endl;
cout<<"breadth"<<b;
}
};
int main()
{
Rectangle R;
cout<<"value of length and breadth before increment:"<<endl;
R.Display();
// this calls "function void operator ++()" function
cout<<endl<<"value of length and breadth After increment:"<<endl;
++R; //R.operator++()
//cout<<"value of count After increment:"<<endl;
R.Display();
return 0;
}
Output:
int main()
{
Rectangle R;
cout<<"value of length and breadth before increment:"<<endl;
R.Display();
// this calls "function void operator ++()" function
cout<<endl<<"value of length and breadth After increment:"<<endl;
R++// R.operator++().
//cout<<"value of count After increment:"<<endl;
R.Display();
return 0;
}
Rectangle R1(2,5),R2(3,4),R3;
//Creating Objects
cout<<endl<<"Rectangle 1 : ";
R1.Display();
cout<<endl<<"Rectangle 2 : ";
R2.Display();
R3 = R1 + R2; //R3=R1.operator+(R2)
cout<<endl<<"Rectangle 3 : ";
R3.Display();
return 0;
}
Note: In statement 1, Left object R1 will invoke operator+() function and right object R2 is
passing as argument.
Output:
Output:
Output:
2. //Another example of Assignment operator overloading
#include<iostream>
using namespace std;
class myclass
{
int x;
int y;
public:
myclass()
{
}
myclass( int x1,int y1)
{
x=x1;
y=y1;
}
void operator=(myclass &m)
{
x=m.x;
y=m.y;
}
void show()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
};
int main()
{
myclass j(10,20);
myclass i;
i=j; //i.operator=(j)
j.show();
i.show();
}
output:
Data conversion:
Those data types which are available as keywords are called primitives data types. for
eg.int,float,char,double etc.already defined to compiler.
user defined is any class type we defined.
float y=3.14;
int x;
x=y;//automatic type conversion
sample s;
int x=5;
s=x;//error
so that there is required type conversion.
Data conversion in C++ includes conversions between basic types and user-defined
types, and conversions between different user-defined types.
The assignments between types, whether they are basic types or user-defined types, are
handled by the compiler with no effort on our part, provided that the same data type is
used on both sides of the equal sign.
};
int main()
{
sample s;
int y=s;//s.operator int() calling casting operator
cout<<"the value of y="<<y<<endl;
return 0;
}
Output:
}
cm(int n)
{
a=n;
}
void show()
{
cout<<"equivalent cm:"<<a<<endl;
}
};
class meter
{
float x;
public:
void getdata()
{
cout<<"Input value in meter"<<endl;
cin>>x;
}
operator cm()
{
return cm(x*100);
}
};
int main()
{
meter m;
m.getdata();
cm c;
c=m;
c.show();
}
Output:
}
alpha(int x)
{
a=x;
}
int getvalue()
{
return a;
}
};
class beta
{
int b;
public:
beta()
{
}
beta(int x)
{
b=x;
}
beta (alpha temp)
{
b=temp.getvalue();
cout<<"constructor method"<<endl;
cout<<"b="<<b<<endl;
}
};
int main()
{
alpha obja(10);
beta objb=obja;
return 0;
}
Output: