C++中的复数类是一种用于表示和操作复数的自定义数据类型。复数由实部和虚部组成,可以表示为a + bi的形式,其中a是实部,b是虚部,i是虚数单位。
下面是一个简单的复数类的实现示例:
#include <iostream>
class Complex {
private:
double real; // 实部
double imaginary; // 虚部
public:
// 默认构造函数
Complex() {
real = 0.0;
imaginary = 0.0;
}
// 带参构造函数
Complex(double r, double i) {
real = r;
imaginary = i;
}
// 获取实部
double getReal() const {
return real;
}
// 获取虚部
double getImaginary() const {
return imaginary;
}
// 重载加法运算符
Complex operator+(const Complex& other) const {
Complex result;
result.real = real + other.real;
result.imaginary = imaginary + other.imaginary;
return result;
}
// 重载减法运算符
Complex operator-(const Complex& other) const {
Complex result;
result.real = real - other.real;
result.imaginary = imaginary - other.imaginary;
return result;
}
// 重载乘法运算符
Complex operator*(const Complex& other) const {
Complex result;
result.real = real * other.real - imaginary * other.imaginary;
result.imaginary = real * other.imaginary + imaginary * other.real;
return result;
}
// 重载输出运算符
friend std::ostream& operator<<(std::ostream& os, const Complex& c) {
os << c.real << " + " << c.imaginary << "i";
return os;
}
};
int main() {
Complex c1(2.0, 3.0);
Complex c2(1.0, 2.0);
Complex sum = c1 + c2;
Complex difference = c1 - c2;
Complex product = c1 * c2;
std::cout << "c1: " << c1 << std::endl;
std::cout << "c2: " << c2 << std::endl;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
return 0;
}
在上面的示例中,Complex类封装了两个私有成员变量real和imaginary,分别表示复数的实部和虚部。它提供了默认构造函数和带参构造函数来初始化复数对象。
Complex类还重载了加法运算符、减法运算符和乘法运算符,使得可以方便地进行复数的加减乘操作。此外,还重载了输出运算符<<,以便能够直接输出复数对象的内容。
在main函数中,我们创建了两个Complex对象c1和c2,并使用重载的加法运算符、减法运算符和乘法运算符进行复数的加减乘操作。最后,我们输出了这些操作的结果。
需要注意的是,这只是一个简单的复数类的实现示例,实际的复数类可以根据需求进行扩展和优化
实例——complex的实现
#include <iostream>
using namespace std;
class complex{
public:
complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
public:
friend complex operator+(const complex & A, const complex & B);
friend complex operator-(const complex & A, const complex & B);
friend complex operator*(const complex & A, const complex & B);
friend complex operator/(const complex & A, const complex & B);
friend istream & operator>>(istream & in, complex & A);
friend ostream & operator<<(ostream & out, complex & A);
private:
double m_real; //实部
double m_imag; //虚部
};
//重载加法运算符
complex operator+(const complex & A, const complex &B){
complex C;
C.m_real = A.m_real + B.m_real;
C.m_imag = A.m_imag + B.m_imag;
return C;
}
//重载减法运算符
complex operator-(const complex & A, const complex &B){
complex C;
C.m_real = A.m_real - B.m_real;
C.m_imag = A.m_imag - B.m_imag;
return C;
}
//重载乘法运算符
complex operator*(const complex & A, const complex &B){
complex C;
C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag;
C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag;
return C;
}
//重载除法运算符
complex operator/(const complex & A, const complex & B){
complex C;
double square = A.m_real * A.m_real + A.m_imag * A.m_imag;
C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square;
C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square;
return C;
}
//重载输入运算符
istream & operator>>(istream & in, complex & A){
in >> A.m_real >> A.m_imag;
return in;
}
//重载输出运算符
ostream & operator<<(ostream & out, complex & A){
out << A.m_real <<" + "<< A.m_imag <<" i ";;
return out;
}
int main(){
complex c1, c2, c3;
cin>>c1>>c2;
c3 = c1 + c2;
cout<<"c1 + c2 = "<<c3<<endl;
c3 = c1 - c2;
cout<<"c1 - c2 = "<<c3<<endl;
c3 = c1 * c2;
cout<<"c1 * c2 = "<<c3<<endl;
c3 = c1 / c2;
cout<<"c1 / c2 = "<<c3<<endl;
return 0;
}