qq_45735316 2020-03-25 17:55 采纳率: 94.1%
浏览 294
已采纳

为什么会出错?构造函数应该怎么调用?

图片说明图片说明图片说明
/*6. 更加完整建立一个复数类 Complex,要求
a. 含两个私有数据成员:real, imaginary: float
b. 具有三种构造函数:a.不带参数构造: 0+0i
b.以实部和虚部构造
c.拷贝构造函数
c.公有成员函数包括如下功能:
输出这个复数
与另一复数的加法函数,要求函数原型为 complex add(complex c);
判断与另一复数是否相等 要求函数原型为bool isEqual(complex c);
在main函数中,输入两个复数,判断两个复数是否相等(实部和虚部要分别相等),然后两者相加之和赋值给一个新的复数,并输出

#include <iostream>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

思考:基于上面的Complex类,有如下main函数能否通过运行,有那些是错误的,为什么?
int main()
{
Complex a,b;
Complex c=a+b;
Complex d=a;
Complex e;
e=a;
return 0;
}*/
class Complex{
    public:
        void getRI(float R,float I);     //设置复数的两个私有成员
        void output();                   //输出这个复数
        void add(Complex c);             //与另一复数的加法函数 原型为 add (Complex c);
        void isEqual(Complex c);         //判断与另一复数是否相等
        Complex(){                       //不带参数的构造函数 
            real=0;
            imaginary=0;
        } 
        Complex(float R,float I);        //以实部和虚部构造
        Complex(Complex &c);             //复制构造函数 
    private:
        float real,imaginary;
};
Complex::Complex(float R,float I)      //带参数构造函数的实现 
{
    real=R;
    imaginary=I;
}
Complex::Comeplex(Complex &c){
    real=c.real;
    imaginary=c.imaginary;
    cout<<"calling the copy constructor"<<endl;
} 
void Complex::getRI(float R,float I){
    real=R;
    imaginary=I;
}
void Complex::output(){
    cout<<real<<"+"<<imaginary<<"i"<<endl; 
}
void Complex::add(Complex c){
    cout<<"两复数相加为:";
    cout<<real+c.real<<"+"<<imaginary+c.imaginary<<"i"<<endl;
}
void Complex::isEqual(Complex c){
    if(real==c.real)
    {
        if(imaginary==c.imaginary)
            cout<<"They are equal."<<endl;
    }
    else
        cout<<"They are not equal."<<endl;
}
int main(int argc, char** argv) {
    Complex c1;
    Complex c2(1,2);       //调用带参数的构造函数
    Complex c3(c2);        //用对象c2初始化对象c3,复制构造函数被调用 
        Complex myComplex,a;             //定义两个对象
    float R1,I1,R2,I2;
    cin>>R1>>I1;
    cin>>R2>>I2;
    myComplex.getRI(R1,I1);
    a.getRI(R2,I2);
    cout<<"复数myComplex为:";
    myComplex.output();
    cout<<"复数c为:";
    a.output();
    myComplex.add(a);        
    /*函数调用的时候不用
    也不可以包含两类型名,
    应该写成myComplex.add(c); */ 
    myComplex.isEqual(a);
    return 0;
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2020-03-25 18:03
    关注

    complex拼写错误

    #include <iostream>
    using namespace std; 
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    /*思考:基于上面的Complex类,有如下main函数能否通过运行,有那些是错误的,为什么?
    int main()
    {
    Complex a,b;
    Complex c=a+b;
    Complex d=a;
    Complex e;
    e=a;
    return 0;
    }*/
    class Complex{
        public:
            void getRI(float R,float I);     //设置复数的两个私有成员
            void output();                   //输出这个复数
            void add(Complex c);             //与另一复数的加法函数 原型为 add (Complex c);
            void isEqual(Complex c);         //判断与另一复数是否相等
            Complex(){                       //不带参数的构造函数 
                real=0;
                imaginary=0;
            } 
            Complex(float R,float I);        //以实部和虚部构造
            Complex(Complex &c);             //复制构造函数 
        private:
            float real,imaginary;
    };
    Complex::Complex(float R,float I)      //带参数构造函数的实现 
    {
        real=R;
        imaginary=I;
    }
    Complex::Complex(Complex &c){
        real=c.real;
        imaginary=c.imaginary;
        cout<<"calling the copy constructor"<<endl;
    } 
    void Complex::getRI(float R,float I){
        real=R;
        imaginary=I;
    }
    void Complex::output(){
        cout<<real<<"+"<<imaginary<<"i"<<endl; 
    }
    void Complex::add(Complex c){
        cout<<"两复数相加为:";
        cout<<real+c.real<<"+"<<imaginary+c.imaginary<<"i"<<endl;
    }
    void Complex::isEqual(Complex c){
        if(real==c.real)
        {
            if(imaginary==c.imaginary)
                cout<<"They are equal."<<endl;
        }
        else
            cout<<"They are not equal."<<endl;
    }
    int main(int argc, char** argv) {
        Complex c1;
        Complex c2(1,2);       //调用带参数的构造函数
        Complex c3(c2);        //用对象c2初始化对象c3,复制构造函数被调用 
            Complex myComplex,a;             //定义两个对象
        float R1,I1,R2,I2;
        cin>>R1>>I1;
        cin>>R2>>I2;
        myComplex.getRI(R1,I1);
        a.getRI(R2,I2);
        cout<<"复数myComplex为:";
        myComplex.output();
        cout<<"复数c为:";
        a.output();
        myComplex.add(a);        
        /*函数调用的时候不用
        也不可以包含两类型名,
        应该写成myComplex.add(c); */ 
        myComplex.isEqual(a);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘