C++ Chapter4
C++ Chapter4
Operator overloading is done with the help of special function called “operator” function.
1
}
void space :: display (void)
{
cout <<x<< " " ;
cout << y << " " ;
cout<< z << "\n" ;
}
void space :: operator - () // here operator is a keyword
{
x = -x;
y = -y;
z = -z;
}
int main ()
{
space s;
s.getdata (10, -20, 30);
cout << "S:";
s.display();
-s; // activates operator-() function
cout << "S:";
s.display ();
}
Output:
S: 10 -20 30
S: -10 20 -30
2
};
complex complex :: operator + (complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return (temp);
}
void complex :: display (void)
{
cout<< x << "+j" <<y<<"\n";
}
int main ()
{
complex C1, C2, C3; // invokes constructor1
C1 = complex (2.5,3.5); // invokes constructor 2
C2 = complex (1.6, 2.7);
C3 = C1 + C2; // activates operator + () function
cout<<"C1= " ; C1.display();
cout<<"C2= " ; C2.display();
cout<<"C3= "; C3.display ();
}
Output:
C1 = 2.5 + j3.5
C2 = 1.6 + j2.7
C3 = 4.1 + j6.2
Example 2
#include<iostream>
using namespace std;
class overloading
{
int value;
public:
void setValue(int temp)
{ value = temp; }
overloading operator+(overloading ob)
{
overloading t;
t.value=value+ob.value;
return(t);
}
void display()
{
cout<<"\n Value is :"<<value<<endl;
3
}
};
int main()
{
overloading obj1,obj2,result;
int n;
cout<<"Enter the no of terms";
cin>>n;
for(int i=1;i<=n;i++)
{ cout<<i<<" Run\n";
obj1.setValue(i);
obj2.setValue(i);
result = obj1 + obj2;
result.display();
}
return 0;
}
Output
Data Conversion
Type conversion refers to changing an entity of one data type, expression, function agreement or
return value into another.
1) Basic to Basic
#include<iostream>
4
using namespace std;
int main()
{
float b = 4.4;
int c;
c = (int)b;
cout<<c;
return 0;
}
2) Basic to user-defined
#include<iostream>
using namespace std;
class X
{
int z;
char y;
public:
X() { }
X (char p)
{
z = (int)p;
y = p;
}
void show()
{
cout<<z<<y;
}
};
int main ()
{
char s = 'a';
X x1;
x1 = s; // calls parameterized constructor. 's' is basic type and x1 is class type.
x1.show();
return 0;
}
3) User-defined to Basic
#include<iostream>
5
#include<math.h>
using namespace std;
class Hour
{
int hr;
public:
Hour() { }
operator int()
{
int minute;
minute= hr * 60;
return (minute);
}
void getdata()
{
cout<<"Enter Hours";
cin>>hr;
}
};
int main()
{
Hour h1;
float min;
h1.getdata();
min = h1; //user defined type to basic
cout<<"Minutes = "<<min;
}
4) User -defined to User-defined
a) Class type to Class type conversion using constructor in the destination class: Rectangle to Polar
#include<iostream>
#include<math.h>
class rectangle
{ float x,y;
public:
rectangle(float a, float b)
{ x=a;
6
y=b;
float get_x()
{ return(x);
float get_y()
{ return(y);
};
class polar
{ float radius,thita;
public:
void show();
polar(){ }
polar(rectangle r)
{ float tempx=r.get_x();
float tempy=r.get_y();
thita = atan(tempy/tempx);
};
{ cout<<"radius is:"<<radius<<endl;
cout<<"thita is:"<<thita*(180/3.14);
int main()
7
rectangle r(6,9);
polar p(r);
p.show();
return 0;
};
8
}
operator rectangle() {