第8章习题答案

};

s s::operator+( s obj ) { strcat( str,obj.str );

return str; //或return *this } int main()

{ s obj1( \ obj3 = obj1 + obj2;

cout << obj3.gets() << endl; }

(2)使用友员函数

#include #include using namespace std; class s { public:

s(){*str='\\0';} s(char *pstr)

{strcpy(str,pstr);

}

char *gets() {returnstr;

}

friend s operator+(s obj1,s obj2); private:

charstr[100]; };

s operator+(s obj1,s obj2) { stempobj;

strcat(tempobj.str,obj1.str); strcat(tempobj.str,obj2.str); returntempobj; }

int main()

{ s obj1(\ obj3=obj1+obj2;

cout<

10.定义一个整数计算类Integer,实现短整数 +,-,*,/ 基本算术运算。要求可以进行数据范围检查(-32768~32767,或自行设定),数据溢出时显示错误信息并中断程序运行。

【解答】

#include using namespace std; class Integer

{private: short a; public:

Integer (short n=0){ a=n;} Integer operator +(Integer); Integer operator -(Integer); Integer operator *(Integer); Integer operator /(Integer); Integer operator =(Integer);

void display() {cout<

Integer Integer::operator+(Integer x) { Integer temp;

if(a+x.a<-32768||a+x.a>32767)

{cout<<\temp.a=a+x.a; return temp; }

Integer Integer::operator-(Integer x) {Integer temp;

if(a-x.a<-32768||a-x.a>32767)

{cout<<\temp.a=a-x.a; return temp; }

Integer Integer::operator*(Integer x) {Integer temp;

if(a*x.a<-32768||a*x.a>32767) {cout<<\

abort();}

temp.a=a*x.a; return temp; }

Integer Integer::operator/(Integer x) {Integer temp;

if(a/x.a<-32768||a/x.a>32767)

{cout<<\temp.a=a/x.a; return temp; }

Integer Integer::operator=(Integer x) { a=x.a;

return *this; }

overflow!\

int main()

{ Integer A(90),B(30),C; cout<<\cout<<\C=A+B;

cout<<\C=A-B;

cout<<\C=A*B;

cout<<\C=A/B;

cout<<\}

11.使用虚函数编写程序求球体和圆柱体的体积及表面积。由于球体和圆柱体都可以看做由圆继承而来,所以可以把圆类Circle作为基类。在Circle类中定义一个数据成员radius和两个虚函数area和volume。由Circle类派生Sphere类和Column类。在派生类中对虚函数area和volume重新定义,分别求球体和圆柱体的体积及表面积。 【解答】

#include using namespace std; const double PI=3.14159265; class circle {public:

circle(double r) {radius=r;}

virtual double area() {return 0.0; }

virtual double volume(){return 0.0;} protected: };

classsphere:public circle { public:

sphere(double r):circle(r){ } double area()

{return 4.0*PI*radius*radius;}

double volume() double radius;

{ return 4.0*PI*radius*radius*radius/3.0;} };

classcolumn:public circle { public:

column(double r,double h):circle(r) { height=h; } double area()

{return 2.0*PI*radius*(height+radius);}

double volume()

{return PI*radius*radius*height; } private: }; int main() { circle *p;

spheresobj(2); p=&sobj;

cout<<\球体:\

cout<<\体积=\double height;

cout<<\表面积=\

columncobj(3,5);

p = &cobj;

cout<<\圆柱体:\

cout<<\体积=\

cout<<\表面积=\}

12.某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元。副教授的固定工资为3000元,每个课时补贴30元。讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。 【解答】

#include using namespace std; class teacher { public:

teacher(char tname[],int time) {strcpy(name,tname);

coursetime=time;

}

virtualint pay()=0; virtual void print()=0; char *getname()

{ return name; }

intgetcoursetime()

{ returncoursetime;} protected: };

classprofessor:public teacher { public: };

classassociateprofessor:public teacher { public:

associateprofessor(char pname[],int time):teacher(pname,time){ } int pay()

{return 3000+coursetime*30;} void print()

{ cout<<\副教授:\

professor(char pname[],int time):teacher(pname,time){ } int pay()

{ return 5000+coursetime*50; void print()

{ cout<<\教授:\}

}

char name[30];

intcoursetime;

};

classlecturer:public teacher { public:

lecturer(char pname[],int time):teacher(pname,time){ } int pay()

};

{ return 2000+coursetime*20; }

void print()

{ cout<<\讲师:\

int main()

{professor pobj(\李小平\

pobj.print();

cout<<'\\t'<<\工资:\associateprofessorapobj(\王芳芳\

apobj.print(); }

cout<<'\\t'<<\工资:\lecturer lobj(\何大建\lobj.print();

cout<<'\\t'<<\工资:\

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4