友元函数的加号运算符重载
时间: 2023-11-19 15:20:39 浏览: 52
友元函数的加号运算符重载可以实现两个对象之间的加法运算。在C++中,加号运算符可以被重载为成员函数或者友元函数。如果将加号运算符重载为成员函数,则操作数只能在左侧,而右侧的操作数必须是函数的参数。如果将加号运算符重载为友元函数,则可以在左右两侧都使用操作数。在实现加号运算符重载时,需要注意参数的类型和返回值的类型,以保证运算的正确性和有效性。
相关问题
友元重载运算符
友元重载运算符是指在C++中,可以使用友元函数或友元类来重载运算符。友元函数是指可以访问类的私有成员的非成员函数,而友元类是指可以访问类的私有成员的另一个类。在重载运算符时,我们可以定义一个友元函数或友元类,使其可以访问类的私有成员,并对运算符进行重载。
例如,我们可以定义一个名为“Point”的类,用来表示二维平面上的一个点。我们可以重载加号运算符来实现两个点的坐标相加。代码如下:
```c++
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
friend Point operator+(const Point& p1, const Point& p2);
void print() {
cout << "(" << x << "," << y << ")" << endl;
}
};
Point operator+(const Point& p1, const Point& p2) {
return Point(p1.x + p2.x, p1.y + p2.y);
}
int main() {
Point p1(1, 2), p2(3, 4);
Point p3 = p1 + p2;
p3.print();
return 0;
}
```
在这个例子中,我们定义了一个类Point,其中包含了x和y两个私有成员变量。我们定义了一个友元函数operator+来重载加号运算符,使其可以实现两个点的坐标相加。在主函数中,我们定义了两个Point类型的对象p1和p2,并将它们相加得到了一个新的Point对象p3,最后输出p3的坐标。
有两个矩阵a 和b ,均为2 行3 列。求两个矩阵之和。用成员函数重载运算符" + " ,使之能用于矩阵相加,如c=a+b。用友元函数重载运算符" - " ,使之能用于矩阵相减,如d=a-b。并以矩阵的形式输出c和d的值。
可以这样实现:
```c++
#include <iostream>
using namespace std;
class Matrix {
private:
int data[2][3];
public:
Matrix() {} // 默认构造函数
Matrix(int d[2][3]) { // 带参数的构造函数
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
data[i][j] = d[i][j];
}
}
}
Matrix operator+(const Matrix& matrix) { // 重载加号运算符
int d[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
d[i][j] = data[i][j] + matrix.data[i][j];
}
}
return Matrix(d);
}
friend Matrix operator-(const Matrix& m1, const Matrix& m2); // 声明友元函数
void print() { // 输出矩阵
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
};
Matrix operator-(const Matrix& m1, const Matrix& m2) { // 实现友元函数
int d[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
d[i][j] = m1.data[i][j] - m2.data[i][j];
}
}
return Matrix(d);
}
int main() {
int a[2][3] = { {1, 2, 3}, {4, 5, 6} };
int b[2][3] = { {7, 8, 9}, {10, 11, 12} };
Matrix m1(a), m2(b);
cout << "a + b = " << endl;
Matrix m3 = m1 + m2;
m3.print();
cout << "a - b = " << endl;
Matrix m4 = m1 - m2;
m4.print();
return 0;
}
```
输出结果为:
```
a + b =
8 10 12
14 16 18
a - b =
-6 -6 -6
-6 -6 -6
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)