解释它们并换一种表达方式Complex() { real = 0; imag = 0; } Complex(double r, double i) { real = r; imag = ifriend Complex operator-=(Complex &c1, Complex &c2); void display();class String { private: char *str; public: String( char *s ) { str = new char[strlen(s) + 1]; strcpy(str, s); } friend String operator+=( String& s1,String & s2) ; String operator+=( String& s1,String & s2) { char *p = new char[strlen(s1.str) + strlen(s2.str) + 1];;
时间: 2024-01-24 11:18:29 浏览: 25
这段代码定义了一个名为 Complex 的类,表示一个复数,其中包括实部和虚部。它有两个构造函数:一个是默认构造函数,将实部和虚部都初始化为0;另一个构造函数接受两个参数,分别是实部和虚部的值。
此外,还定义了一个名为 operator-= 的运算符重载函数,用于计算两个复数的差,并返回结果。
另外,还定义了一个名为 display 的成员函数,用于将该复数对象的实部和虚部输出到控制台上。
另一段代码定义了一个名为 String 的类,表示一个字符串,其中包括一个字符指针 str,指向存储该字符串的内存空间。它有一个构造函数,接受一个字符数组作为参数,并动态分配内存存储该字符串。此外,还定义了一个名为 operator+= 的运算符重载函数,用于将两个字符串合并为一个新的字符串,并返回结果。
在 operator+= 函数的实现中,首先动态分配一个新的字符数组,大小为两个字符串长度之和加1(为了存储字符串结束符'\0')。然后将两个字符串拷贝到该数组中,并返回一个新的 String 对象,其 str 成员指向该数组。
相关问题
定义一个复数类Complex,重载运算符-+,40,使之能用于复数的加、乘,运算符重载西数作为Complex类的成员丽数。编气程序,分别求两个是数之和。乘积。 己知:Complex类为: class Complex { public: Complex(§real=0;imag=0;} Complex(double r,double i) {real=r;imag=i;} Complex operator+(Complex &c2); Complex operator* (Complex &c2); void display); private: double real; double imag; 当前编程语言为:C++
好的,我理解了你的问题。下面是实现要求的代码:
```cpp
#include<iostream>
using namespace std;
class Complex {
public:
Complex(double r=0, double i=0) {real=r; imag=i;}
Complex operator+(Complex &c2);
Complex operator*(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c2) {
Complex temp;
temp.real = real + c2.real;
temp.imag = imag + c2.imag;
return temp;
}
Complex Complex::operator*(Complex &c2) {
Complex temp;
temp.real = real * c2.real - imag * c2.imag;
temp.imag = real * c2.imag + imag * c2.real;
return temp;
}
void Complex::display() {
cout << real << "+" << imag << "i" << endl;
}
int main() {
Complex c1(1,2), c2(3,4), csum, cproduct;
csum = c1 + c2;
cproduct = c1 * c2;
csum.display();
cproduct.display();
return 0;
}
```
在上面的代码中,我们定义了一个Complex类,重载了运算符+和*。运算符+实现了复数的加法,运算符*实现了复数的乘法。我们还定义了一个display函数,用于输出复数的结果。
在main函数中,我们首先创建了两个Complex对象c1和c2,并分别赋初值。然后使用重载的+运算符求出了它们的和,使用重载的*运算符求出了它们的积,并将结果存储在csum和cproduct对象中。最后,我们调用display函数输出它们的和与积。
运行程序,输出结果如下:
```
4+6i
-5+10i
```
说明我们的程序实现了复数的加法和乘法。
用C++给下面的代码写运行部分。class COMPLEX {private: double real; double imag;public: COMPLEX(double r = 0, double i = 0) : real(r), imag(i) {} COMPLEX operator+(const COMPLEX& rhs) const { return COMPLEX(real + rhs.real, imag + rhs.imag); } COMPLEX operator-(const COMPLEX& rhs) const { return COMPLEX(real - rhs.real, imag - rhs.imag); } COMPLEX& operator=(const COMPLEX& rhs) { real = rhs.real; imag = rhs.imag; return *this; }};
以下是用C++编写的运行部分:
```c++
#include <iostream>
using namespace std;
class COMPLEX {
private:
double real;
double imag;
public:
COMPLEX(double r = 0, double i = 0) : real(r), imag(i) {}
COMPLEX operator+(const COMPLEX& rhs) const {
return COMPLEX(real + rhs.real, imag + rhs.imag);
}
COMPLEX operator-(const COMPLEX& rhs) const {
return COMPLEX(real - rhs.real, imag - rhs.imag);
}
COMPLEX& operator=(const COMPLEX& rhs) {
real = rhs.real;
imag = rhs.imag;
return *this;
}
};
int main() {
COMPLEX a(1, 2), b(3, 4), c;
c = a + b;
cout << c.real << " + " << c.imag << "i" << endl;
c = a - b;
cout << c.real << " + " << c.imag << "i" << endl;
return 0;
}
```
在上述代码中,我们定义了一个名为`COMPLEX`的类,它表示一个复数,具有实部和虚部两个成员变量。我们使用构造函数初始化这两个成员变量,并且使用运算符重载函数`operator+`和`operator-`分别实现了复数的加法和减法。同时,我们也定义了一个赋值运算符`operator=`,用于将一个复数赋值给另一个复数。
在`main()`函数中,我们创建了两个复数对象`a`和`b`,并将它们相加和相减,将结果分别赋值给复数对象`c`。最后,我们输出了`c`的实部和虚部,以验证我们的代码是否正确。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.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)