《面向对象与C++》上机作业5.docx
- 文档编号:8037941
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:12
- 大小:84.94KB
《面向对象与C++》上机作业5.docx
《《面向对象与C++》上机作业5.docx》由会员分享,可在线阅读,更多相关《《面向对象与C++》上机作业5.docx(12页珍藏版)》请在冰豆网上搜索。
《面向对象与C++》上机作业5
8-5. 编写并测试4个重载函数Double(x),返回值为输入参数的两倍;参数类型分别为int、long、float、double,返回值类型与参数类型一样。
#include
usingnamespacestd;
intDouble(intx){
return2*x;
}
longDouble(longx){
return2*x;
}
floatDouble(floatx){
return2*x;
}
doubleDouble(doublex){
return2*x;
}
intmain()
{
inta=7;
longb=885656666;
floatc=8.25;
doubled=8.50;
cout< cout< cout< cout< getchar(); return0; } 8-6. 编写并测试一个类Rectangle,有itsLength(长)、itsWidth(宽)等数据成员,有重载的构造函数Rectangle()、Rectangle(intwidth,intlength)。 classRectangle{ public: intitsLength; intitsWidth; Rectangle(){ itsLength=0; itsWidth=0; } Rectangle(intwidth,intlength){ itsLength=length; itsWidth=width; } }; 8-7. 编写并测试一个计数器Counter类,对其重载运算符+。 #include usingnamespacestd; classCounter{ public: inta; intoperator+(Counterc) { returnthis->a+c.a; } }; intmain() { Countera1; a1.a=45; Countera2(a1); cout< return0; } 8-13. 编写并测试类Point,有数据成员x、y,为其定义友元函数实现重载+。 #include usingnamespacestd; classPoint{ intx; inty; public: Point(){ x=0; y=0; } Point(inta,intb){ x=a; y=b; } friendPointoperator+(Pointp1,Pointp2); voidprint(){ cout<<"x="< } }; Pointoperator+(Pointp1,Pointp2){ Pointp(p1.x+p2.x,p1.y+p2.y); returnp; } intmain() { Pointa1(1,1),a2(2,2),a3; a3=a1+a2; a3.print(); return0; } 编程题#1: 大整数运算 封装一个大整数类HugeInt,以支持任意大整数的四则运算,重载你认为需要的所有运算符,比如=,==,>等等运算符。 测试类是否正确: 如计算 n=100! +50! #include #include #include usingnamespacestd; classHugeInt: vector public: HugeInt(intn=0) { push_back(n); check(); } HugeInt&check() { while(! empty()&&! back())pop_back(); if(empty())return*this; for(inti=1;i { (*this)[i]+=(*this)[i-1]/10; (*this)[i-1]%=10; } while(back()>=10) { push_back(back()/10); (*this)[size()-2]%=10; } return*this; } friendistream&operator>>(istream&is,HugeInt&n); friendostream&operator<<(ostream&os,constHugeInt&n); friendbooloperator! =(constHugeInt&a,constHugeInt&b); friendbooloperator==(constHugeInt&a,constHugeInt&b); friendbooloperator<(constHugeInt&a,constHugeInt&b); friendbooloperator>(constHugeInt&a,constHugeInt&b); friendbooloperator<=(constHugeInt&a,constHugeInt&b); friendbooloperator>=(constHugeInt&a,constHugeInt&b); friendHugeIntoperator+=(HugeInt&a,constHugeInt&b); friendHugeIntoperator+(HugeInta,constHugeInt&b); friendHugeIntoperator-=(HugeInt&a,HugeIntb); friendHugeIntoperator-(HugeInta,constHugeInt&b); friendHugeIntoperator*(constHugeInt&a,constHugeInt&b); friendHugeIntoperator*=(HugeInt&a,constHugeInt&b); friendHugeIntdivmod(HugeInt&a,constHugeInt&b); friendHugeIntoperator/(HugeInta,constHugeInt&b); friendHugeIntoperator/=(HugeInt&a,constHugeInt&b); friendHugeIntoperator%=(HugeInt&a,constHugeInt&b); friendHugeIntoperator%(HugeInta,constHugeInt&b); friendHugeIntpow(constHugeInt&n,constHugeInt&k); HugeIntoperator! (void); }; istream&operator>>(istream&is,HugeInt&n) { strings; is>>s; n.clear(); for(inti=s.size()-1;i>=0;--i){ n.push_back(s[i]-'0'); } returnis; } ostream&operator<<(ostream&os,constHugeInt&n) { if(n.empty())os<<0; for(inti=n.size()-1;i>=0;--i)os< returnos; } booloperator! =(constHugeInt&a,constHugeInt&b) { if(a.size()! =b.size())return1; for(inti=a.size()-1;i>=0;--i) if(a[i]! =b[i])return1; return0; } booloperator==(constHugeInt&a,constHugeInt&b) { return! (a! =b); } booloperator<(constHugeInt&a,constHugeInt&b) { if(a.size()!
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象与C+ 面向 对象 上机 作业