Operator Overloading part 1(Chapter 12 of Thinking in C++)

Operator Overloading part 1(Chapter 12 of Thinking in C++)Operator Overloading part 1(Chapter 12 of Thinking in C++)Integer1#include<iostream>

2usingnamespacestd;

3

4classInteger

5{

6longi;

7IntegerThis() {returnthis; }

8public:

9Integer(longll=0) : i(ll) {}

10friendconstInteger&operator+(constInteger&a);

11friendconstIntegeroperator-(constInteger&a);

12friendconstIntegeroperator~(constInteger&a);

13friend Integer
operator&(Integer&a);

14friendintoperator!(constInteger&a);

15friendconstInteger&operator++(Integer&a);

16friendconstIntegeroperator++(Integer&a,int);

17friendconstInteger&operator--(Integer&a);

18friendconstIntegeroperator--(Integer&a,int);

19};

20

21constInteger&operator+(constInteger&a)

22{

23cout<<"+Integern";

24returna;

25}

26

27constIntegeroperator-(constInteger&a)

28{

29cout<<"-Integern";

30returnInteger(-a.i);

31}

32

33constIntegeroperator~(constInteger&a)

34{

35cout<<"~Integern";

36returnInteger(~a.i);

37}

38

39Integeroperator&(Integer&a)

40{

41cout<<"&Integern";

42returna.This();

43}

44

45intoperator!(constInteger&a)

46{

47cout<<"!Integern";

48return!a.i;

49}

50

51constInteger&operator++(Integer&a)

52{

53cout<<"++Integern";

54a.i++;

55returna;

56}

57

58constIntegeroperator++(Integer&a,int)

59{

60cout<<"Integer++n";

61Integer before(a.i);

62a.i++;

63returnbefore;

64}

65

66constInteger&operator--(Integer&a)

67{

68cout<<"--Integern";

69a.i--;

70returna;

71}

72

73constIntegeroperator--(Integer&a,int)

74{

75cout<<"Integer--n";

76Integer before(a.i);

77a.i--;

78returnbefore;

79}

80

81voidf(Integer a)

82{

83+a;

84-a;

85~a;

86Integer
ip=&a;

87!a;

88++a;

89a++;

90--a;

91a--;

92}

93

94classByte

95{

96unsignedcharb;

97public:

98Byte(unsignedcharbb=0) : b(bb) {}

99constByte&operator+()const

100{

101cout<<"+Byten";

102returnthis;

103}

104

105constByteoperator-()const

106{

107cout<<"-Byten";

108returnByte(-b);

109}

110

111constByteoperator~()const

112{

113cout<<"~Byten";

114returnByte(~b);

115}

116

117Byteoperator!()const

118{

119cout<<"!Byten";

120returnByte(!b);

121}

122

123Byte
operator&()

124{

125cout<<"&Byten";

126returnthis;

127}

128

129constByte&operator++()

130{

131cout<<"++Byten";

132b++;

133returnthis;

134}

135

136constByteoperator++(int)

137{

138cout<<"Byte++n";

139Byte before(b);

140b++;

141returnbefore;

142}

143

144constByte&operator--()

145{

146cout<<"--Byten";

147--b;

148return
this;

149}

150

151constByteoperator--(int)

152{

153cout<<"Byte--n";

154Byte before(b);

155--b;

156returnbefore;

157}

158};

159

160voidg(Byte b)

161{

162+b;

163-b;

164~b;

165Byte*bp=&b;

166!b;

167++b;

168b++;

169--b;

170b--;

171}

172

173intmain()

174{

175Integer a;

176f(a);

177Byte b;

178g(b);

179}

原文链接: https://www.cnblogs.com/zhtf2014/archive/2010/11/26/1889269.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/17900

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午6:36
下一篇 2023年2月7日 下午6:37

相关推荐