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
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 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 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;