C++面向对象程序设计习题集(6)

2019-04-14 13:58

friend double operator+(Triangle t1,Triangle t2) {

return t1.area+t2.area; }

friend double operator+(double d,Triangle t) {

return d+t.area; } };

void main() {

Triangle tl(3,4,5),t2(4,5,6),t3(5,6,7),t4(6,7,8); double s;

cout<<\ cout<<\ cout<<\ cout<<\ s=t1+t2+t3+t4;

cout<<\总面积=\}

21.

设计一个学生类Student,包括姓名和三门课程成绩,利用重载运算符“+’’将所有学生的成绩相加放在一个对象中,再对该对象求各门课程的平均分。 【知识点】:4.2 【参考分】:30分 【难易度】:B 【答案】:

#include #include #include class Student {

char name[10]; int degl,deg2,deg3; public:

Student() {degl=deg2=deg3=0;} Student(char na[],int dl,int d2,int d3) {

strcpy(name,na);

degl=dl;deg2=d2;deg3=d3; }

friend Student operator+(Student s1,Student s2) {

statiC Student st;

st.degl=sl.degl+s2.degl; st.deg2=sl.deg2+s2.deg2; st.deg3=sl.deg3+s2.deg3;

return st; }

void disp() {

cout<

friend void avg(Student &s,int n) {

tout<

void main() {

Student sl(\ Student s3(\ cout<<\输出结果\ s1.disp(); s2.disp(); s3.disp(); s4.disp();

s=sl+s2+s3+s4; //调用重载运算符 avg(s,4); //友元函数求平均分 } 22.

设计一个时间类Time,包括时、分、秒等私有数据成员。要求实现时间的基本运算,如一时间加上另一时间、一时间减去另一时间等。 【知识点】:4.2 【参考分】:40分 【难易度】:B 【答案】:在Time类中设计如下重载运算符函数:

·Time operator+(Time):返回一时间加上另一时间得到的新时间。 ·Time operator-(Time):返回一时间减去另一时间得到的新时间。 程序如下:

#include class Time {

int hour,minute,second; public:

Time(){};

Time(int h,int m,int s) {

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

Time(int h,int m)

{

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

Time(int h) {

hour=h;minute=0;second=0; }

void sethour(int h){hour=h;)

void setminute(int m){minute=m;) void setsecond(int s){second=s;} int gethour(){return hour;) int getminute(){return minute;) int getsecond(){return second;) Time operator+(Time); Time operator-(Time); void disp() {

cout<

Time Time::operator+(Time t) {

int carry,hh,mm,ss;

ss=getsecond()+t.getsecond(); if(ss>60) {

SS--=60;

carry=l; //进位标记 }else carry=0;

mm=getminute()+t.getminute()+carry;if(mm>60) {

mm-=60; carry=l; }

else carry=0;

hh=gethour()+t.gethour()+carry; if(hh>24) hh-=24;

static Time result(hh,mm,ss); return result; }

Time Time::operator-(Time t) {

int borrow,hh,mm,ss;

ss=getsecond()-t.getsecond(); if(ss<0)

ss+=60;

\:\

borrow=l; //借位标记 }

else borrow=l;

mm=getminute()-t.getminute()-borrow; if(mm<0) {

mm+=60; borrow=1; }

else borrow=0;

hh=gethour()-t.gethour()-borrow; if(hh<0) hh+=24;

Static Time resuit(hh,mm,ss); return result; }

void main() {

Time now(2,24,39), Time Start(17,55);

Time tl=now-start,t2=now+Start; cout<<\输出结果:\ cout<<\: \ cout<<\:\ cout<<\相差:\cout<<\相加:\}

23.

设计一个点类Point(包含x和y两个私有数据成员),实现点对象之间的加、减、判相同、判不同等各种运算。 【知识点】:2.2 4.2 【参考分】:40分 【难易度】:B 【答案】:

#include Class Point {

int x,y; public:

Point(){x=y=0;)

Point(int i,int j){x=i;y=j;} Point(Point &); ~Point(){}

void offset(int,int); //提供对点的偏移

void offset(Point); //重载,偏移量用Point类对象表示

bool operator==(Point); //运算符重载,判断两个对象是否相同

bool operator!=(Point); //运算符重载,判断两个对象是否不相同 void operator+=(Point); //运算符重载,将两个点对象相加 void operator-(Point); //运算符重载,将两个点对象相减

Point operator+(Point); //运算符重载,相加并将结果放在左操作数中

Point operator-(Point); //运算符重载,相减并将结果放在左操作数中 int getx(){return x;) int gety(){return y;} void disp() {

cout<<”(\ } };

Point::Point(Point &p) x=P.X;y;p.y;

void Point::offset(int i,int j) {

x+=i;y+=j; }

void Point::offset(Point P) {

x+=P.getx();y+=P.gety(); }

bool Point::operator==(Point P) {

if(x==P.getx()&&y==p.gety()) return 1; elee return 0; }

bool Point::operator!=(Point P) {

if(x!=P.getx()||y!=p.gety()) return 1; else return 0; }

void Point::operator+=(Point P) {

x+=P.getx();y+=p.gety(); }

void Point::operator-=(Point P) {

X-=p.getx();y-=p.gety(); }

Point Point::operator+(Point P) {

this->x+=p.x;this->y+=p.y;


C++面向对象程序设计习题集(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:小学生作业鼓励性评语作业评语

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

马上注册会员

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