![]() |
#2
rjsp2016-05-17 11:58
|

#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int r,int i):real(r),imag(i){}
Complex operator ++(); // 相当于 ++Time ,单操作数运算符的重载
Complex operator ++(int); // 相当于 Time++
Complex (int r); // 此乃转换构造函数
// Complex operator +(Complex &tmp); // 使用类的成员函数进行“+”运算符的重载
friend Complex operator +(Complex &tmp1,Complex &tmp2);
friend ostream& operator<<(ostream& output,Complex& tmp);
friend istream& operator>>(istream& input,Complex& tmp);
private:
int real;
int imag;
};
/*
Complex Complex::operator +(Complex &tmp)
{
return Complex(this->real + tmp.real,this->imag + tmp.imag);
}
*/
Complex::Complex(int r)
{
this->real = r;
this->imag = 0;
}
Complex Complex::operator ++()
{
this->real = this->real + 1;
this->imag = this->imag;
return *this;
}
Complex Complex::operator ++(int)
{
this->real = this->real + 1;
this->imag = this->imag;
return *this;
}
istream& operator>>(istream& input,Complex& tmp) //对输入流的重载
{
input>>tmp.real>>tmp.imag;
return input;
}
Complex operator +(Complex &tmp1,Complex &tmp2) //使用类的友元函数进行“+”运算符的重载 (注意:友元是类的友元)
{
return Complex(tmp1.real + tmp2.real , tmp1.imag + tmp2.imag);
}
ostream& operator<< (ostream& output,Complex& tmp) //对输出流的重载
{
output<<"["<<tmp.real<<"+"<<tmp.imag<<"i"<<"]";
return output;
}
int main()
{
int r = 10;
Complex b(1,2);
Complex a = 10 + b; //本应隐示等价于Complex a = Complex(10) + b;
cout<<a<<endl;
getchar();
return 0;
}
using namespace std;
class Complex
{
public:
Complex(int r,int i):real(r),imag(i){}
Complex operator ++(); // 相当于 ++Time ,单操作数运算符的重载
Complex operator ++(int); // 相当于 Time++
Complex (int r); // 此乃转换构造函数
// Complex operator +(Complex &tmp); // 使用类的成员函数进行“+”运算符的重载
friend Complex operator +(Complex &tmp1,Complex &tmp2);
friend ostream& operator<<(ostream& output,Complex& tmp);
friend istream& operator>>(istream& input,Complex& tmp);
private:
int real;
int imag;
};
/*
Complex Complex::operator +(Complex &tmp)
{
return Complex(this->real + tmp.real,this->imag + tmp.imag);
}
*/
Complex::Complex(int r)
{
this->real = r;
this->imag = 0;
}
Complex Complex::operator ++()
{
this->real = this->real + 1;
this->imag = this->imag;
return *this;
}
Complex Complex::operator ++(int)
{
this->real = this->real + 1;
this->imag = this->imag;
return *this;
}
istream& operator>>(istream& input,Complex& tmp) //对输入流的重载
{
input>>tmp.real>>tmp.imag;
return input;
}
Complex operator +(Complex &tmp1,Complex &tmp2) //使用类的友元函数进行“+”运算符的重载 (注意:友元是类的友元)
{
return Complex(tmp1.real + tmp2.real , tmp1.imag + tmp2.imag);
}
ostream& operator<< (ostream& output,Complex& tmp) //对输出流的重载
{
output<<"["<<tmp.real<<"+"<<tmp.imag<<"i"<<"]";
return output;
}
int main()
{
int r = 10;
Complex b(1,2);
Complex a = 10 + b; //本应隐示等价于Complex a = Complex(10) + b;
cout<<a<<endl;
getchar();
return 0;
}
问程序段一中main函数内:Complex a = 10 + b;总报错~ 难道编译器不知道将10-》10+0i 再利用运算符重载实现复数加法吗?相反我的程序端二却又可以实现隐示转换,请问他们有什么区别呢? 谢谢~下面是程序段二

#include <iostream>
using namespace std;
class Complex
{
public:
~Complex(){}
Complex(int r,int i):real(r),imag(i){}
Complex(int r){this->real = r;this->imag = 0;}
operator int(){return real;}
friend Complex operator + (Complex& tmp1,Complex& tmp2);
private:
int real;
int imag;
};
Complex operator + (Complex& tmp1,Complex& tmp2)
{
return Complex(tmp1.real + tmp2.real , tmp1.imag + tmp2.imag);
}
int main()
{
Complex a(1,2);
Complex c = Complex(10);
Complex b = a + 10;
getchar();
return 0;
}
using namespace std;
class Complex
{
public:
~Complex(){}
Complex(int r,int i):real(r),imag(i){}
Complex(int r){this->real = r;this->imag = 0;}
operator int(){return real;}
friend Complex operator + (Complex& tmp1,Complex& tmp2);
private:
int real;
int imag;
};
Complex operator + (Complex& tmp1,Complex& tmp2)
{
return Complex(tmp1.real + tmp2.real , tmp1.imag + tmp2.imag);
}
int main()
{
Complex a(1,2);
Complex c = Complex(10);
Complex b = a + 10;
getchar();
return 0;
}