//struction-function-c
# include<iostream.h>
# include"struction-function-h.h"
class Fraction //分数类
{
private:
int fenzi;
int fenmu;
public:
Fraction():fenzi(0),femnmu(1) {}
Fraction(int n): fenzi(n),fenmu(1) {}
Fraction(int n, int d): fenzi(n),fenmu(d) {}
void out()
{
cout<<fenzi<<'/'<<fenmu<<endl;
return;
}
};
Fraction Fraction::operator+(Fraction & x)
{
Fraction y;
y.fenzi=fenzi*x.fenmu+fenmu*x.fenzi;
y.fenmu=fenmu*x.fenmu;
return y;
}
void main()
{
Fraction x,s=5;
x=s+Fraction(9,11);
x=s+8+Fraction(9,11);//因为8的存在所以这一句话不能通过编译!类型不匹配!
//如何将8正确的加在上面的式字里面(也就是如何用构造函数用于类型转换),保证式子的正确运行;
x.out();
return;
}