-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperatoroverloading4.cpp
More file actions
34 lines (30 loc) · 967 Bytes
/
operatoroverloading4.cpp
File metadata and controls
34 lines (30 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Conversion Operator: We can also write conversion operators that can be used to convert one type to another type
#include <iostream>
using namespace std;
class Fraction
{
private:
int num, den;
public:
Fraction(int n, int d)
{
num = n;
den = d;
}
// Conversion operator syntax: operator <target-type>() [cv-qualifiers], target-type is return type
// const promises not to modify the object(i.e. num, den)
operator float() const
{
return float(num) / float(den);
}
};
int main()
{
Fraction f(2, 5);
float val = f; // val is of type float
// since f is Fraction, compiler looks for a way to convert it to float, so conversion operator is invoked
// float val = f.operator float();
cout << val << '\n';
return 0;
}
// Overloaded conversion operators must be a member method. Other operators can either be the member method or the global method.