设计一个复杂的类,
一个复杂对象包含两个私有数据成员:real(type:int),imag(type:int)。 并且该类必须具有一个带有默认参数的构造函数,该参数的值为零和析构函数。
输出运算符<<的重载,输出格式:(real,imag)。
以友元函数的形式重载运算符-=、+=
以成员函数的形式重载运算符前和后。
我的代码:
int main(){
Complex a(3,5),b(5,3);
cout<<"Complex a:"<<a<<endl;
cout<<"Complex b:"<<b<<endl;
a-=b;
cout<<"After executing a-=b,Complex a:"<<a<<endl;
cout<<"b--:"<<b--<<endl;
cout<<"--b:"<<--b<<endl;
}
当主函数给出并执行时,我希望产生如下结果:
Complex a:(3,5)
Complex b:(5,3)
After executing a-=b,Complex a:(-2,2)
b--:(5,3)
--b:(3,3)
你的结果如果是--b:(3,3)的话,++和--只对实部起作用。
你的结果,最后的--b如果是--b:(3,1),++和--对实部和虚部都起作用。
两种情况的代码都贴出来了,根据你的需要选择吧。
(1)--只对实部起作用的运行结果:
(2)--对实部和虚部都起作用的运行结果:
(1)的代码:(--只对实部起作用)
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) { real = r; imag = i; }
~Complex() {}
friend ostream& operator <<(ostream& os, const Complex& c);
friend Complex& operator +=(Complex& c1, Complex& c2)
{
c1.real += c2.real;
c1.imag += c2.imag;
return c1;
}
friend Complex& operator -=(Complex& c1, Complex& c2)
{
c1.real -= c2.real;
c1.imag -= c2.imag;
return c1;
}
//前置++
Complex operator ++() {
this->real += 1;
//this->imag += 1; //如果++只对实部起作用,就注释掉这一句
return *this;
}
//后置++
Complex operator ++(int) {
Complex c(this->real, this->imag);
this->real += 1;
//this->imag += 1;//如果++只对实部起作用,就注释掉这一句
return c;
}
//前置--
Complex operator --() {
this->real -= 1;
//this->imag -= 1;//如果++只对实部起作用,就注释掉这一句
return *this;
}
//后置--
Complex operator --(int) {
Complex c(this->real, this->imag);
this->real -= 1;
//this->imag -= 1;//如果++只对实部起作用,就注释掉这一句
return c;
}
};
ostream& operator <<(ostream& os, const Complex& c)
{
os << "(" << c.real << "," << c.imag << ")";
return os;
}
int main()
{
Complex a(3, 5), b(5, 3);
cout << "Complex a:" << a << endl;
cout << "Complex b:" << b << endl;
a -= b;
cout << "After executing a-=b,Complex a:" << a << endl;
cout << "b--:" << b-- << endl;
cout << "--b:" << --b << endl;
return 0;
}
(2)的代码:(--对实部和虚部都起作用)
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r=0,int i = 0) { real = r; imag = i; }
~Complex() {}
friend ostream& operator <<(ostream& os, const Complex& c);
friend Complex& operator +=(Complex& c1, Complex& c2)
{
c1.real += c2.real;
c1.imag += c2.imag;
return c1;
}
friend Complex& operator -=(Complex& c1, Complex& c2)
{
c1.real -= c2.real;
c1.imag -= c2.imag;
return c1;
}
//前置++
Complex operator ++() {
this->real += 1;
this->imag += 1; //如果++只对实部起作用,就注释掉这一句
return *this;
}
//后置++
Complex operator ++(int) {
Complex c(this->real, this->imag);
this->real += 1;
this->imag += 1;//如果++只对实部起作用,就注释掉这一句
return c;
}
//前置--
Complex operator --() {
this->real -= 1;
this->imag -= 1;//如果++只对实部起作用,就注释掉这一句
return *this;
}
//后置--
Complex operator --(int) {
Complex c(this->real, this->imag);
this->real -= 1;
this->imag -= 1;//如果++只对实部起作用,就注释掉这一句
return c;
}
};
ostream& operator <<(ostream& os, const Complex& c)
{
os << "(" << c.real << "," << c.imag << ")";
return os;
}
int main()
{
Complex a(3, 5), b(5, 3);
cout << "Complex a:" << a << endl;
cout << "Complex b:" << b << endl;
a -= b;
cout << "After executing a-=b,Complex a:" << a << endl;
cout << "b--:" << b-- << endl;
cout << "--b:" << --b << endl;
return 0;
}
你题目的解答代码如下:
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
~Complex() {}
friend ostream &operator<<(ostream &os, const Complex &data)
{
return os << "(" << data.real << "," << data.imag << ")";
}
Complex operator+=(Complex &b)
{
this->real += b.real;
this->imag += b.imag;
return *this;
}
Complex operator-=(Complex &b)
{
this->real -= b.real;
this->imag -= b.imag;
return *this;
}
Complex operator++() // 前置++
{
++real;
return *this;
}
Complex operator++(int) // 后置++
{
Complex temp(real,imag);
real++;
return temp;
}
Complex operator--() // 前置--
{
--real;
return *this;
}
Complex operator--(int) // 后置--
{
Complex temp(real,imag);
real--;
return temp;
}
};
int main()
{
Complex a(3, 5), b(5, 3);
cout << "Complex a:" << a << endl;
cout << "Complex b:" << b << endl;
a -= b;
cout << "After executing a-=b,Complex a:" << a << endl;
cout << "b--:" << b-- << endl;
cout << "--b:" << --b << endl;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!