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'<<\工资:\