![]() |
#2
rjsp2017-06-07 14:54
|

#include<iostream>
using namespace std;
class CCopmlex
{
private:
double m_real, m_unreal;//实数,虚数
public:
CCopmlex();
CCopmlex(double r = 0, double u = 0);
CCopmlex operator+(const CCopmlex &t);
friend void print(const CCopmlex &t);
};
CCopmlex::CCopmlex()
{
m_real = 0;
m_unreal = 0;
}
CCopmlex::CCopmlex(double r, double u)
{
m_real = r;
m_unreal = u;
}
inline CCopmlex CCopmlex::operator+(const CCopmlex &t)//重载运算符
{
return CCopmlex(m_real + t.m_real, m_unreal + t.m_unreal);
}
void print(const CCopmlex &t)
{
if (t.m_unreal < 0)
cout << t.m_real << t.m_unreal << "i" << endl;
cout << t.m_real << "+" << t.m_unreal << "i" << endl;
}
int main()
{
CCopmlex p1(2.0, 3.0), p2(4.0, 2.3);
CCopmlex p3;
p3 = p1 + p2;
cout << "the answer is";
print(p3);
return 0;
}
using namespace std;
class CCopmlex
{
private:
double m_real, m_unreal;//实数,虚数
public:
CCopmlex();
CCopmlex(double r = 0, double u = 0);
CCopmlex operator+(const CCopmlex &t);
friend void print(const CCopmlex &t);
};
CCopmlex::CCopmlex()
{
m_real = 0;
m_unreal = 0;
}
CCopmlex::CCopmlex(double r, double u)
{
m_real = r;
m_unreal = u;
}
inline CCopmlex CCopmlex::operator+(const CCopmlex &t)//重载运算符
{
return CCopmlex(m_real + t.m_real, m_unreal + t.m_unreal);
}
void print(const CCopmlex &t)
{
if (t.m_unreal < 0)
cout << t.m_real << t.m_unreal << "i" << endl;
cout << t.m_real << "+" << t.m_unreal << "i" << endl;
}
int main()
{
CCopmlex p1(2.0, 3.0), p2(4.0, 2.3);
CCopmlex p3;
p3 = p1 + p2;
cout << "the answer is";
print(p3);
return 0;
}
总是提示类包含多个默认构造函数;对重载函数调用不明确