-
Notifications
You must be signed in to change notification settings - Fork 24
/
MyInt.h
75 lines (63 loc) · 1.59 KB
/
MyInt.h
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef MYINT_H
#define MYINT_H
#include <iostream>
class MyInt
{
private:
int m_num;
public:
// constructor
MyInt(int num): m_num(num){}
// copy constructor
MyInt(const MyInt &num)
{
m_num = num.m_num;
}
// copy assignment operator
MyInt& operator=(const MyInt &num)
{
m_num = num.m_num;
return *this;
}
MyInt operator+(const MyInt& r)
{
int temp = 0;
temp = m_num + r.m_num;
MyInt result(temp);
return result;
}
MyInt operator-(const MyInt& r)
{
int temp = 0;
temp = m_num - r.m_num;
MyInt result(temp);
return result;
}
MyInt operator/(const MyInt& r)
{
if (r.m_num == 0)
{
// illegal division, you may want to throw an exception here
return MyInt(0);
}
int temp = 0;
temp = m_num / r.m_num;
MyInt result(temp);
return result;
}
MyInt operator*(const MyInt& r)
{
int temp = 0;
temp = m_num * r.m_num;
MyInt result(temp);
return result;
}
friend std::ostream& operator<<(std::ostream& os,const MyInt& num);
};
// overload the global operator << so that we can print our object the same way we print an int
std::ostream& operator<<(std::ostream& os,const MyInt& num)
{
os << num.m_num;
return os;
}
#endif