创建类A和类B分别拥有私有成员,通过一个友元函数让他们实现相加
时间: 2024-05-05 19:22:23 浏览: 12
下面是一个简单的示例代码:
```c++
#include <iostream>
using namespace std;
class B; // 提前声明类B
class A {
private:
int numA;
public:
A(int n = 0) : numA(n) { }
friend int add(A a, B b); // 友元函数声明
};
class B {
private:
int numB;
public:
B(int n = 0) : numB(n) { }
friend int add(A a, B b); // 友元函数声明
};
int add(A a, B b) { // 友元函数定义
return a.numA + b.numB;
}
int main() {
A objA(10);
B objB(20);
cout << "相加结果为:" << add(objA, objB) << endl;
return 0;
}
```
在上面的代码中,类A和类B分别拥有私有成员numA和numB。为了让它们实现相加,我们定义了一个友元函数add,并在类A和类B中分别声明了该函数为友元函数。在友元函数中,我们可以访问类A和类B的私有成员,从而实现相加的功能。最后,在main函数中,我们创建了一个类A对象和一个类B对象,然后调用了add函数,输出了相加结果。
相关问题
用c++分别采用成员函数和友元函数重载实现复数的加法运算。
好的,下面是用成员函数和友元函数重载实现复数加法的示例代码:
```C++
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double real = 0, double imag = 0) {
this->real = real;
this->imag = imag;
}
Complex operator+(const Complex& c) const {
Complex res;
res.real = this->real + c.real;
res.imag = this->imag + c.imag;
return res;
}
void display() {
cout << "(" << real << ", " << imag << ")" << endl;
}
friend Complex operator+(double r, const Complex& c);
};
Complex operator+(double r, const Complex& c) {
Complex res;
res.real = r + c.real;
res.imag = c.imag;
return res;
}
int main() {
Complex a(2, 3);
Complex b(3, 4);
Complex c = a + b;
c.display();
Complex d = 2 + b;
d.display();
return 0;
}
```
上面的代码实现了一个复数类 `Complex`,其中重载了 `+` 运算符,用于实现复数的加法运算。同时,代码中用了两种不同的实现方式,一种是用成员函数重载 `+` 运算符,另一种是用友元函数重载 `+` 运算符。其中,成员函数重载的代码如下:
```C++
Complex operator+(const Complex& c) const {
Complex res;
res.real = this->real + c.real;
res.imag = this->imag + c.imag;
return res;
}
```
这个函数的作用是将当前的复数对象和另一个复数对象相加,返回一个新的复数对象,实现了复数的加法运算。而友元函数重载的代码如下:
```C++
friend Complex operator+(double r, const Complex& c);
```
这个函数的作用是将一个实数和一个复数对象相加,返回一个新的复数对象,实现了复数的加法运算。需要注意的是,这个函数是一个友元函数,因此可以直接访问 `Complex` 类的私有成员变量。
最后,在 `main` 函数中,我们创建了两个复数对象 `a` 和 `b`,并将它们相加得到一个新的复数对象 `c`,然后将 `c` 输出。接着,我们创建了一个实数 `2`,并将它和复数对象 `b` 相加得到一个新的复数对象 `d`,然后将 `d` 输出。
派生类的成员和友元函数使用派生类向基类的类型转换的具体例子
假设有如下的基类和派生类定义:
```cpp
class Base {
protected:
int x;
public:
Base(int a): x(a) {}
void display() {
cout << "Base class x = " << x << endl;
}
};
class Derived : public Base {
private:
int y;
public:
Derived(int a, int b): Base(a), y(b) {}
void display() {
cout << "Derived class x = " << x << ", y = " << y << endl;
}
friend int sum(Derived d); // 声明友元函数
};
```
现在我们来看一个具体的例子,假设有一个函数 `sum()`,它需要访问派生类 `Derived` 的私有成员 `y`,同时也需要访问基类 `Base` 的保护成员 `x`。我们可以将 `sum()` 声明为 `Derived` 的友元函数,这样 `sum()` 就可以直接访问 `Derived` 中的私有成员 `y`。但是,由于 `Base` 中的成员 `x` 是被保护的,无法直接访问。此时,我们可以通过使用派生类向基类的类型转换来解决这个问题。
具体来说,我们可以在 `sum()` 函数中将 `Derived` 类型的对象转换成 `Base` 类型的对象,然后就可以访问 `Base` 中的保护成员 `x` 了。代码如下:
```cpp
int sum(Derived d) {
int s = d.y;
s += static_cast<Base>(d).x;
return s;
}
```
在 `sum()` 函数中,我们首先访问了 `Derived` 中的私有成员 `y`,然后使用 `static_cast` 进行类型转换,将 `Derived` 类型的对象 `d` 强制转换成 `Base` 类型的对象。这样就可以访问 `Base` 类中的保护成员 `x` 了。最后,将 `y` 和 `x` 的值相加,得到了结果。
需要注意的是,在使用派生类向基类的类型转换时,需要保证这个转换是合法的。也就是说,被转换的对象必须是派生类的对象,而不能是基类的对象或其他类型的对象。否则,这个转换将是不合法的,可能会导致程序出错。