| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 731 人关注过本帖
标题:为什么构造函数在外面不行?高手帮忙啊!
只看楼主 加入收藏
xuru1990
Rank: 2
等 级:论坛游民
帖 子:36
专家分:23
注 册:2009-10-28
结帖率:50%
收藏
已结贴  问题点数:0 回复次数:4 
为什么构造函数在外面不行?高手帮忙啊!
class Complex
{
private:
    double real;
    double imag;
public:
    Complex (double r,double i);
    Complex operator+(Complex &s);
    Complex operator-(Complex &s);
    Complex operator*(Complex &s);
    Complex operator/(Complex &s);
    void show();
};
Complex::Complex(double r=0,double i=0)
{
real=r;
imag=i;
}
Complex Complex::operator+(Complex &s)
{
double r=real+s.real;
double i=imag+s.imag;
return Complex(r,i);
}
Complex Complex::operator-(Complex &s)
{
double r=real-s.real;
double i=imag-s.imag;
return Complex(r,i);
}
Complex Complex::operator*(Complex &s)
{
double r=real*s.real-imag*s.imag;
double i=real*s.imag+imag*s.real;
return Complex(r,i);
}
Complex Complex::operator/(Complex &s)
{
double d=s.real*s.real+s.imag*s.imag;
if(d==0) {cout<<"分母为0,错!"<<endl;exit(1);}
else
{double r=(real*s.real+imag*s.imag)/d;
double i=(imag*s.real-real*s.imag)/d;
return Complex(r,i);
}
}
void Complex::show()
{
if(real!=0) cout<<real;
else ;
 if(imag<0) cout<<"-"<<imag<<"i"<<endl;
 if(imag==0) cout<<endl;
 if(imag>0) cout<<"+"<<imag<<"i"<<endl;
}

void main()
{
Complex c1(1,2),c2(3,4),c3,c4,c5,c6;
c3=c1+c2;
c4=c1-c2;
c5=c1*c2;
c6=c1/c2;
c1.show();
c2.show();
c3.show();
c4.show();
c5.show();
c6.show();
}
  
搜索更多相关主题的帖子: 构造 函数 
2009-12-26 22:25
英英
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:29
专家分:133
注 册:2009-3-3
收藏
得分:5 
可以在外面定义啊,这个程序应该是你的运算符重载函数出问题了吧,构造函数是程序自动调用的,而不能由用户调用,所以你不能将它返回返回
2009-12-26 23:10
guchao2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:101
专家分:106
注 册:2009-4-13
收藏
得分:5 
我喜欢这种写法·································


// 在复数类下对+、-、*、/、<<运算符进行重载

#include<iostream.h>

class complex
{
private:
    double real;
    double imag;
public:
    complex();
    complex(double r,double i);
    friend complex operator+(complex a,complex b);
    friend complex operator-(complex a,complex b);
    friend complex operator*(complex a,complex b);
    friend complex operator/(complex a,complex b);
    friend ostream &operator<<(ostream &out,complex c);
};

complex::complex()
{
    real=1;
    imag=1;
}

complex::complex(double r,double i)
{
    real=r;
    imag=i;
}

complex operator+(complex a,complex b)
{
    complex c;
    c.real=a.real+b.real;
    c.imag=a.imag+b.imag;
    return c;
}

complex operator-(complex a,complex b)
{
    complex c;
    c.real=a.real-b.real;
    c.imag=a.imag-b.imag;
    return c;
}

complex operator*(complex a,complex b)
{
    complex c;
    c.real=a.real*b.real-a.imag*b.imag;
    c.imag=a.real*b.imag+a.imag*b.real;
    return c;
}

complex operator/(complex a,complex b)
{
    complex c;
    c.real=(a.real*b.real+a.imag*b.imag)/(a.real*a.real-b.imag*b.imag);
    c.imag=(-a.real*b.imag-a.imag*b.real)/(a.real*a.real-b.imag*b.imag);
    return c;
}


ostream &operator<<(ostream &out,complex c)
{
    if(c.real==0)
    {
        if(c.imag==0)
            out<<"0"<<endl;
        else if(c.imag==1)
            out<<"i"<<endl;
        else if(c.imag==-1)
            out<<"-i"<<endl;
        else if(c.imag<0)
            out<<"-"<<c.imag<<"i"<<endl;
        else
            out<<c.imag<<"i"<<endl;
    }
    else
    {
        if(c.imag==0)
            out<<c.real<<endl;
        else if(c.imag==1)
            out<<c.real<<"+"<<"i"<<endl;
        else if(c.imag==-1)
            out<<c.real<<"-i"<<endl;
        else if(c.imag<0)
            out<<c.real<<"-"<<c.imag<<"i"<<endl;
        else
            out<<c.real<<"+"<<c.imag<<"i"<<endl;
    }
    return out;


}

void main()
{
    complex t1(2,3),t2(3,4),t3;
    t3=t1+t2;
    cout<<t3;
    t3=t1-t2;
    cout<<t3;
    t3=t1*t2;
    cout<<t3;
    t3=t1/t2;
    cout<<t3;
}
2009-12-26 23:35
shiyuehai
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:96
专家分:116
注 册:2009-9-4
收藏
得分:5 
构造函数应该没问题吧
2009-12-26 23:41
faye3000
Rank: 2
等 级:论坛游民
帖 子:11
专家分:25
注 册:2009-10-7
收藏
得分:5 
如果是你上面的代码的话,在外面是没错的,但是和里面的重复了!
2009-12-27 12:07
快速回复:为什么构造函数在外面不行?高手帮忙啊!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017326 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved