-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperatorFriendFnOverloading.cpp
47 lines (36 loc) · 1.14 KB
/
OperatorFriendFnOverloading.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
class Calc
{
private:
double m_value = 0;
public:
Calc(double x) : m_value { x } {}
double get_value() { return m_value; }
friend Calc operator+(const Calc &calc1, const Calc &calc2) { return calc1.m_value + calc2.m_value; }
friend Calc operator-(const Calc &calc1, const Calc &calc2) { return calc1.m_value - calc2.m_value; }
friend Calc operator*(const Calc &calc1, const Calc &calc2);
friend Calc operator/(const Calc &calc1, const Calc &calc2);
friend Calc operator+(const Calc &calc, int x) { return calc.m_value + x; }
};
Calc operator*(const Calc &calc1, const Calc &calc2)
{
return calc1.m_value * calc2.m_value;
}
Calc operator/(const Calc &calc1, const Calc &calc2)
{
return calc1.m_value / calc2.m_value;
}
int main()
{
Calc calc1(1337);
Calc calc2(3);
Calc sum = calc1 + calc2;
Calc sub = calc1 - calc2;
Calc mlt = calc1 * calc2;
Calc dvd = calc1 / calc2;
std::cout << sum.get_value() << std::endl;
std::cout << sub.get_value() << std::endl;
std::cout << mlt.get_value() << std::endl;
std::cout << dvd.get_value() << std::endl;
return 0;
}