C++ 开发之实现操作符重载的实例

2025-05-27 0 44

C++操作符重载

实现效果图:

C++ 开发之实现操作符重载的实例

实例代码:

Matrix.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
#pragma once

#include "vector"

#include "iostream"

#define rep(i,n) for(int i=1;i<=n;i++) //宏定义for循环,精简代码

using namespace std;

class Matrix

{

public:

//基本构造函数

Matrix(int Row=0, int Column=0);

//拷贝构造函数或者复制构造函数

Matrix(const Matrix& matrix);

//赋值操作符重载,必须为成员函数,不然会报错

Matrix& operator=(const Matrix& matrix);

//复合赋值操作符重载,建议重载为成员函数

Matrix& operator+=(const Matrix& matrix);

Matrix& operator*=(const Matrix& matrix);

Matrix& operator*=(const float& number);

Matrix& operator*=(const int& number);

Matrix& operator-=(const Matrix& matrix);

Matrix& operator/=(const float& number);

float& operator[](const size_t& index);

Matrix& operator++();//前缀式自增

Matrix& operator--();//前缀式自减

Matrix operator++(int); //后缀式自增

Matrix operator--(int); //后缀式自减

//算术和关系操作符一般为非成员函数,声明为友元

friend Matrix operator+(const Matrix& matrix1, const Matrix& matrix2);

friend Matrix operator-(const Matrix& matrix1, const Matrix& matrix2);

friend Matrix operator*(const Matrix& matrix1, const Matrix& matrix2);

friend Matrix operator*(const Matrix& matrix1, const float& number);

friend Matrix operator*(const Matrix& matrix1, const int& number);

friend bool operator==(const Matrix& matrix1, const Matrix& matrix2);

friend bool operator!=(const Matrix& matrix1, const Matrix& matrix2);

//输出操作符<<的重载,必须声明为友元

friend ostream& operator<<(ostream& os, const Matrix&object);

//输入操作符>>重载,必须声明为友元

friend istream& operator >>(istream& in,Matrix&object);

void Display();

~Matrix();

public:

int Row;

int Column;

vector<vector<float> > data; //二维vector,用于存放矩阵类数据

};

Matrix.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

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

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326
#include "stdafx.h"

#include "Matrix.h"

#include "iomanip"

//构造函数

Matrix::Matrix(int Row/* =0 */, int Column/* =0 */){

this->Row = Row;

this->Column = Column;

data.resize(Row + 1); //申请行数为row+1,0号位不用

rep(i, Row)

data[i].resize(Column + 1); //申请各行的列数

rep(i, Row)

rep(j, Column)

data[i][j] = 0; //每个元素初始化为0,方便后面计算

}

//打印函数

void Matrix::Display(){

rep(i, Row)

{

rep(j, Column)

cout << setw(8) << data[i][j] << ' ';

cout <<endl;

}

}

//拷贝构造函数

Matrix::Matrix(const Matrix& matrix){

Row = matrix.Row;

Column = matrix.Column;

data.resize(Row+1); //申请行数为row,0号位不用

rep(i, Row)

data[i].resize(Column+1); //申请各行的列数

rep(i, Row)

rep(j, Column)

data[i][j] = matrix.data[i][j];

}

//赋值操作符重载

Matrix& Matrix::operator=(const Matrix& matrix){

if (this==&matrix)

{

return *this;

}

Row = matrix.Row;

Column = matrix.Column;

//分配资源

data.resize(Row + 1); //申请行数为row+1,0号位不用

rep(i, Row)

data[i].resize(Column + 1); //申请各行的列数

rep(i, Row)

rep(j, Column)

data[i][j] = matrix.data[i][j];

//返回本对象的引用

return *this;

}

//复合赋值操作符重载

Matrix& Matrix::operator+=(const Matrix& matrix){

if (Row == matrix.Row&&Column == matrix.Column)

{

rep(i, Row)

{

rep(j,Column)

{

data[i][j] += matrix.data[i][j];

}

}

}

return *this;

}

Matrix& Matrix::operator-=(const Matrix& matrix){

if (Row == matrix.Row&&Column == matrix.Column)

{

rep(i, Row)

{

rep(j, Column)

{

data[i][j] -= matrix.data[i][j];

}

}

}

return *this;

}

Matrix& Matrix::operator*=(const float& number){

rep(i, Row)

{

rep(j, Column)

{

data[i][j] = data[i][j] * number;

}

}

return *this;

}

Matrix& Matrix::operator*=(const int& number){

rep(i, Row)

{

rep(j, Column)

{

data[i][j] = data[i][j] * number;

}

}

return *this;

}

Matrix& Matrix::operator*=(const Matrix& matrix){

//先保存矩阵的值

Matrix temp(Row, Column);

rep(i, temp.Row)

{

rep(j, temp.Column)

{

temp.data[i][j] = data[i][j];

}

}

//改变矩阵的大小和值

Column = matrix.Column;

data.clear(); //清除数据

data.resize(Row+1); //申请行数为row+1,0号位不用

rep(i, Row)

data[i].resize(Column+1); //申请各行的列数

//重新给矩阵赋值

rep(i, temp.Row)

{

rep(j, matrix.Column)

{

double sum = 0;

rep(k, temp.Column)

sum += temp.data[i][k] * matrix.data[k][j];

data[i][j] = sum; //改变矩阵的值

}

}

return *this;

}

Matrix& Matrix::operator/=(const float& number){

rep(i, Row)

{

rep(j, Column)

{

data[i][j] = data[i][j] / number;

}

}

return *this;

}

//前缀式自增

Matrix& Matrix::operator++(){

//对每个元素都加1

rep(i, Row)

{

rep(j, Column)

{

data[i][j] +=1;

}

}

return *this;

}

//前缀式自减

Matrix& Matrix::operator--(){

//对每个元素都减1

rep(i, Row)

{

rep(j, Column)

{

data[i][j] -= 1;

}

}

return *this;

}

//后缀式自增

Matrix Matrix::operator++(int){

//拷贝构造函数

Matrix ret(*this);

//对每个元素都加1

rep(i, Row)

{

rep(j, Column)

{

data[i][j] += 1;

}

}

return ret;

}

//后缀式自减

Matrix Matrix::operator--(int){

//拷贝构造函数

Matrix ret(*this);

//对每个元素都减1

rep(i, Row)

{

rep(j, Column)

{

data[i][j] -= 1;

}

}

return ret;

}

//析构函数

Matrix::~Matrix()

{

data.clear();

}

//加法操作符重载

Matrix operator+(const Matrix& matrix1, const Matrix& matrix2){

Matrix temp(matrix1.Row, matrix1.Column);

if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column)

{

rep(i, matrix1.Row)

{

rep(j, matrix2.Column)

{

temp.data[i][j] = matrix1.data[i][j]+matrix2.data[i][j];

}

}

}

return temp;

}

//减法操作符重载

Matrix operator-(const Matrix& matrix1, const Matrix& matrix2){

Matrix temp(matrix1.Row, matrix1.Column);

if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column)

{

rep(i, matrix1.Row)

{

rep(j, matrix2.Column)

{

temp.data[i][j] = matrix1.data[i][j] - matrix2.data[i][j];

}

}

}

return temp;

}

//乘法操作符重载

Matrix operator*(const Matrix& matrix1, const Matrix& matrix2){

Matrix temp(matrix1.Row, matrix2.Column);

rep(i, temp.Row)

{

rep(j, temp.Column)

{

double sum = 0;

rep(k, matrix1.Column)

sum += matrix1.data[i][k] * matrix2.data[k][j];

temp.data[i][j] = sum;

}

}

return temp;

}

//乘法操作符重载

Matrix operator*(const Matrix& matrix1, const float& number){

Matrix temp(matrix1.Row, matrix1.Column);

rep(i, temp.Row)

{

rep(j, temp.Column)

{

temp.data[i][j] = matrix1.data[i][j]* number;

}

}

return temp;

}

//乘法操作符重载

Matrix operator*(const Matrix& matrix1, const int& number){

Matrix temp(matrix1.Row, matrix1.Column);

rep(i, temp.Row)

{

rep(j, temp.Column)

{

temp.data[i][j] = matrix1.data[i][j] * number;

}

}

return temp;

}

//等于关系操作符重载

bool operator==(const Matrix& matrix1, const Matrix& matrix2){

//只有维数相等才有可比性

if (matrix1.Row==matrix2.Row&&matrix1.Column==matrix2.Column)

{

rep(i, matrix1.Row)

{

rep(j, matrix1.Column)

{

if (matrix1.data[i][j]!=matrix2.data[i][j])

{

return false;

}

}

}

return true;

}

else

{

return false;

}

}

//不等于关系操作符重载

bool operator!=(const Matrix& matrix1, const Matrix& matrix2){

//只有维数相等才有可比性

if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column)

{

rep(i, matrix1.Row)

{

rep(j, matrix1.Column)

{

if (matrix1.data[i][j] != matrix2.data[i][j])

{

return true;

}

}

}

return false;

}

else

{

return false;

}

}

//输出操作符重载

ostream& operator<<(ostream& os, const Matrix&object){

rep(i, object.Row)

{

rep(j, object.Column)

os << setw(8) << object.data[i][j] << ' ';

os <<endl;

}

return os;

}

//输入操作符重载

istream& operator >>(istream& in, Matrix&object){

rep(i, object.Row)

rep(j, object.Column)

in >> object.data[i][j];

return in;

}

main.c

?

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

76

77

78

79

80

81
#include "iostream"

#include "Matrix.h"

using namespace std;

int main(){

int row1, row2, col1, col2;

cout << "请输入第一个矩阵的行和列:\\n";

cin >> row1 >> col1;

Matrix m1(row1, col1);

cout << "请输入" << row1 << '*' << col1 << "个数:\\n";

cin >> m1;

cout << "输出矩阵的值:\\n";

cout << m1;

cout << "请输入第二个矩阵的行和列:\\n";

cin >> row2 >> col2;

Matrix m2(row2, col2);

cout << "请输入" << row2 << '*' << col2 << "个数:\\n ";

cin >> m2;

cout << "输出矩阵的值:\\n";

cout << m2;

if (col1 != row2)

cout << "这两个矩阵无法相乘\\n";

else

{

cout << "判断矩阵m1与m2是否相等:\\n";

if (m1==m2)

{

cout << "m1和m2相等:\\n";

}

else

{

cout << "m1和m2不相等:\\n";

}

cout << "m1拷贝构造m3矩阵结果输出:\\n";

Matrix m3(m1);

cout << m3;

cout << "m1赋值重载m4矩阵结果输出:\\n";

Matrix m4(m1.Row,m1.Column);

m4 = m1;

cout << m4;

cout << "m1*m2矩阵相乘输出m5:\\n";

Matrix m5(m1.Row, m2.Column);

m5 = m1*m2;

cout << m5;

cout << "矩阵m1*2输出m6:\\n";

Matrix m6(m1.Row, m1.Column);

m6 = m1*2;

cout << m6;

cout << "矩阵m1*0.5输出m7:\\n";

Matrix m7(m1.Row, m1.Column);

m7 = m1 * 0.5;

cout << m7;

cout << "m1*m2矩阵相乘输出m1:\\n";

m1 *= m2;

cout << m1;

cout << "m1矩阵前自增输出\\n";

cout << ++m1;

cout << "m1矩阵后自增输出\\n";

cout << m1++;

cout << "m1矩阵输出\\n";

cout << m1;

cout << "m1矩阵前自减输出\\n";

cout << --m1;

cout << "m1矩阵后自减输出\\n";

cout << m1--;

cout << "m1矩阵输出\\n";

cout << m1;

}

return 0;

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C++ 开发之实现操作符重载的实例 https://www.kuaiidc.com/73710.html

相关文章

发表评论
暂无评论