面向对象程序设计试卷作业答案(2)

2019-04-21 15:47

Nup=up*r1.up;

i=Maxdivisor(Nup,Ndown);

return Rational(Nup/i,Ndown/i); }

void Rational::showNomal() {

cout<

main() {

Rational r1(4,14); Rational r2(5,8); Rational r;

cout<<\ r1.showNomal();

cout<<\ r2.showNomal(); r=r1*r2;

cout<<\ r.showNomal();

return 0; }

作业五

定义包含时、分、秒信息的时间类Time,并实现时间对象的流输入输出,格式为

时:分:秒,如23:35:18。

(本题总分10分,类结构4分,输入输出函数实现各2分,主函数2分。程序结构完整,有不妥之处,酌情扣分。)

#include \

#include

class Time {

friend ostream & operator<<(ostream &output, Time &t);

friend istream & operator>>(istream &input, Time &t); public:

Time(int h=0, int m=0, int s=0); private:

int hour, minute, second; };

Time::Time

(int h, int m, int s) {

hour=h; minute=m; second=s; }

ostream & operator<<(ostream &output, Time &t) {

output << t.hour << \

return output; }

istream & operator>>(istream &input, Time &t) {

char a,b;

input >> t.hour >> a >> t.minute >> b >> t.second ;

return input; }

int main() {

Time t;

cout << \:mm:ss: \cin >> t;//调用自己定义的运算符重载函数operator>>(cin,t) cout << \

cout << t << endl;//调用自己定义的运算符重载函数operator<<(cout,t) return 0; }

作业六

对于下面的类MyString,要求重载一些运算符后可以计算表达式:a = b + c,

其中a、b、c都是类MyString的对象,+用以实现两个字符串对象的连接。 请重载相应的运算符并编写程序测试。(本题总分10分,类结构4分,重载函数实现4分,主函数2分。程序结构完整,有不妥之处,酌情扣分。)

class MyString { public:

MyString(char *s) {

str = new char[strlen(s)+1]; strcpy(str, s); }

~MyString() { delete []str; }

private:

char *str; };

标记单词或重新标记。


面向对象程序设计试卷作业答案(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:数学与统计学学院中期报告模板

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: