当前位置:首页 > C++程序设计习题大荟萃
{
a=new int(x);
cout<< ”Constructor”<<*a< ~ A(){ delete a; cout<<”Destructor!”< }; void main() { A x(3),*p; p=new A(5); delete p; } 6. #include #include int a,b;char op; public: A(int aa;int bb,char ch) { a=aa;b=bb;op=ch; } int Comp() { switch(op) { case ‘+’:return a+b; case ‘-’:return a-b; case ‘*’:return a*b; case ‘/’:if(b!=0) return a/b;else exit(1); case ‘%’:if(b!=0) return a%b;else exit(1); default: exit(1); } } void SetA(int aa,int bb,char ch) { a=aa;b=bb;op=ch; } }; void main(void) { A x(3,5,’*’); int a=x.Comp(); x.SetA(4,9,’+’); a+=x.Comp(); cout<<”a=”< 7. #include class B{ int a,b; public: B() { a=b=0; } B(int aa,int bb) { a=aa;b=bb; } B operator +(B&x) { B r; r.a=a+x.a; r.b=b+x.b; return r; } B operator -(B&x) { B r; r.a=a-x.a; r.b=b-x.b; return r; } void OutB() { cout< void main() { B x(3,5),y(8,4),z1,z2; z1=x+y;z2=x-y; z1.OutB(); z2.OutB(); } 8. #include #include int yuan,jiao,fen; void Norm() { if(fen>9) { jiao+=fen/10; fen%=10; } if(jiao>9) { yuan+=jiao/10;jiao%=10; 25 } } void Error() { cout<<”data not negative!”< return *this; } RMB& operator *(int n) { exit(1); } public: RMB(int a=0,int b=0,int c=0) { if(a<0||b<0||c<0) Error(); yuan=a; jiao=b; fen=c; Norm(); } void Output() cout< RMB &operator +(RMB&r) { yuan+=r.yuan;jiao+=r.jiao;fen+=r.fen; Norm(); return *this; } RMB&operator –(RMB&r) { fen=fen+jiao*10+yuan*100; jiao=yuan=0; if(fen>=r.fen) fen-=r.fen; else Error(); Norm(); jiao=jiao+yuan*10; yuan=0; if(jiao>=r.jiao) jiao-=r.jiao; else Error(); Norm(); if(yuan>=r.yuan) yuan-=r.yuan; else Error(); if(n<0) Error(); fen *=n;jiao*=n;yuan*=n; Norm(); return *this; } }; void main() { RMB a,b(4,5,9),c,d(b),e; a.SetValume(2,8,5); c+a; c+b; d-a; e=d; e*3; a.Output();b.Output;c.Output();d.Output();e.Output(); } 9. #include #include friend ostreams & operator<<(ostream& ostr,Strings& s); public: Strings():ps(0) ( ) Strings(char *ss) { ps=new char[strlen(ss)+1]; strcpy(ps,ss); } Strings(String& s) { ps=new char[strlen(s.ps)+1]; strcpy(ps,s.ps); } Strings& operator=(Strings& s) { delete []ps; ps=new char[strlen(s.ps)+1]; strcpy(ps,s.ps); return *this; } ~ A()Strings() 26 { delete[]ps; cout<<”release dynamic memory space1”< } void Setps(char *ss) { delete[]ps; ps=new char[strlen(ss)+1]; strcpy(ps,ss); } int operator>(Strings&s) { if(strcmp(ps,s.ps)>0) return 1; else return 0; } int operator==(String& s) { if(strcmp(ps,s.ps)==0) return 1; else return 0; } int operator<(Strings& s) { if(strcmp(ps,s.ps)<0) return 1; else return 0; } Strings operator+(Strings& s) { Strings r; r.ps=new char[strlen(this->ps)+strlen(s.ps)+1]; strcpy(r.ps,this->ps); strcat(r.ps,s.ps); return r; } }; ostream& operator<<(ostream& ostr,Strings& s) { if(s.ps) ostr< } void main() { Strings s1,s2(“C++ language”),s3; s3.Setps(“programing.”); s1=s2+s3; cout< if(s1>s2) cout<<”s1>s2”< else if(s1==s2) cout<<”s1==s2”< 四、按题目要求编写出程序、函数或类 2 1. 下面是定义二次多项式ax+bx+c所对应的 类 #include Quadratic(){a=b=c=0;} Quadratic(double aa,doble bb,double cc); Quadratic operator +(Quadratic&x); Quadratic operator -(Quadratic&x); double Compute(double x); int Root(double& r1,doble& r2); void Print(); }; 其中:加、减操作符重载函数完成*this和x的加或减运算,并将运算结果返回;Compute 2 函数根据x的值计算二次多项式ax+bx+c=0的根,要求当不是二次方程(即a=0)时返回-1,当有实根时返回 1,并由引用参数r1和r2带回这两个实根,当无实根时返回0;Print函 2 数按ax**2+bx+c的格式(x用x**2表示)输出二次多项式,并且当b和c的值为负时,其前面不能出现加号。试写出在类定义中声明的每个成员函数在体外的定义。 2. 请定义一个矩形类(Rectangle),私有数据 成员为矩形的长度(len)和宽度(wid),无参构造函数置len和wid为0,带参构造函数置len 27 和wid为对应形参的值,另外还包括求矩形周2. #include 长,求矩形面积、取矩形长度、取矩形宽度、修改矩形长度和宽度为对应形参的值,输出矩形尺寸等公用成员函数。要求输出矩形尺寸的格式为”length:长度,width:宽度”。 第九章 类的继承与多态性 一、填空题 1. 对基类数据成员的初始化是通过执行派生类构 造函数中的______来实现的。 2. 在一个派生类中,对基类成员、类对象成员和非 对象成员和非类对象成员的初始化次序是先______,后______,最后为______。 3. 当撤消一个含有基类和类对象成员的派生类对 象时,将首先完成______的析构函数定义体的执行,接着完成______的析构函数定义体的执行,最后完成______的析构函数定义体的执行。 4. 假定一个类AB中有一个静态整型成员bb,在类外 3. 为它进行定义并初始化为0时,所使用的语句为______。 5. 假定类AB中有一个公用属性的静态数据成员bb, 在类外不通过对象名访问该成员bb的写法为______。 二、写出下列程序运行后的输出结果 1. #include class A{ int a; public: A(int aa=0):a(aa) { cout<<”Constructor A!”< class B:public A{ int b; public: B(int aa,int bb):A(aa),b(bb) { cout<<”Constructor B!”< void main() { B x(2,3),y(4,5); } 28 class A{ int a; public: A(int aa=0){ a=aa; } ~ A(){cout<<”Destructor A!”< class B:public A{ int b; public: B(int aa=0,int bb=0):A(aa){b=bb;} ~ B(){cout<<”Destructor B!”< void main() { B x(5),y(6,7); } #include class AX{ int x; public: AX(int xx=0):x(xx) { cout<<”AX constructor.”< void Output() { cout< int Get(){ return x; } }; class BX:public AX{ int y; AX z; public: BX(int xx=0,int yy=0):AX(xx),y(yy),z(xx+yy) { cout<<”BX constructor:”< { AX::Output(); cout<
共分享92篇相关文档