xxxx217 2022-05-03 16:11 采纳率: 75%
浏览 55
已结题

较为复杂的类与对象问题

设计一个复杂的类,
一个复杂对象包含两个私有数据成员: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)

  • 写回答

3条回答 默认 最新

  • 关注

    你的结果如果是--b:(3,3)的话,++和--只对实部起作用。
    你的结果,最后的--b如果是--b:(3,1),++和--对实部和虚部都起作用。
    两种情况的代码都贴出来了,根据你的需要选择吧。
    (1)--只对实部起作用的运行结果:

    img

    (2)--对实部和虚部都起作用的运行结果:

    img

    (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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月16日
  • 已采纳回答 12月8日
  • 修改了问题 5月3日
  • 创建了问题 5月3日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效