完成一个复数类Complex(复数a+bi,a为实部,b为虚部两部),已经给出main函数
发布网友
发布时间:2022-05-01 20:50
我来回答
共1个回答
热心网友
时间:2023-10-25 06:35
#include<iostream>
using std::cout;
using std::cin;
class Complex {
double m_r, m_v;
public:
Complex(double r=0, double v=0);
friend Complex operator+(const Complex&, const Complex&);
friend std::ostream& operator<<(std::ostream&, const Complex&);
};
Complex::Complex(double r, double v)
{
m_r = r;
m_v = v;
}
Complex operator+(const Complex &c1, const Complex &c2)
{
return Complex(c1.m_r+c2.m_r, c1.m_v+c2.m_v);
}
std::ostream& operator<<(std::ostream &os, const Complex &c)
{
os << c.m_r << '+' << c.m_v << 'i' << std::endl;
return os;
}
int main()
{
Complex c1(2,3),c2(5,6),c3;
c3=c1+c2;
cout<<c1<<c2<<c3;
return 0;
}
热心网友
时间:2023-10-25 06:35
#include<iostream>
using std::cout;
using std::cin;
class Complex {
double m_r, m_v;
public:
Complex(double r=0, double v=0);
friend Complex operator+(const Complex&, const Complex&);
friend std::ostream& operator<<(std::ostream&, const Complex&);
};
Complex::Complex(double r, double v)
{
m_r = r;
m_v = v;
}
Complex operator+(const Complex &c1, const Complex &c2)
{
return Complex(c1.m_r+c2.m_r, c1.m_v+c2.m_v);
}
std::ostream& operator<<(std::ostream &os, const Complex &c)
{
os << c.m_r << '+' << c.m_v << 'i' << std::endl;
return os;
}
int main()
{
Complex c1(2,3),c2(5,6),c3;
c3=c1+c2;
cout<<c1<<c2<<c3;
return 0;
}