a=10,b=10 a=15,b=18 Donstructor. Donstructor.
5、#include 
char msg[30]; protected: int n; public:
Base(char s[],int m=0):n(m) { strcpy(msg,s); }
void output(void)
  {  cout< class Derived1:public Base{ private:  int n; public:   Derived1(int m=1):  Base(\ {  n=m; }   void output(void)  {  cout< class Derived2:public Derived1 {  private:  int n; public:   Derived2(int m=2):  Derived1(m-1)  { n=m; }   void output(void)  {  cout<     Derived1::output();  }  };  int main() {   Base B(\ Derived2 D;  B.output();  D.output(); }  程序运行结果: 1  Base Class 2 1 0 Base      6、#include    X(int i)  {       x=i; cout<<”int: ”<     x=(int)i*2; cout<<” double: ”<    { cout<<” ~X(): ”< void main() {  int a=8;  double b=11.0; X x1(55), x2(a*5); X x3=X(2*a); X x4=X(b); }  程序输出结果: int: 55,called int: 40,called int: 16,called  double: 11,called  ~X(): 22,called   ~X(): 16,called  ~X(): 40,called  ~X(): 55,called  7.  #include   void Setij(int a,int b){i=a,j=b;}  ~Samp()   { cout<<\ }   int GetMuti(){return i*j;}  protected:  int i;  int j; };    int main() {   Samp *p;   p=new Samp[5];  if(!p)   { cout<<\  return 1;  }   for(int j=0;j<5;j++)   p[j].Setij(j,j);  for(int k=0;k<5;k++)    cout<<\ <  delete[]p;  return 0; }  Muti[0] is:0 Muti[1] is:1 Muti[2] is:4 Muti[3] is:9 Muti[4] is:16 Destroying..4 Destroying..3 Destroying..2 Destroying..1  Destroying..0  8、 #include  void main( ) { int a=2;  for(int j=0;j<2;j++) cout< 输出结果为:  6 6   9.  #include class Abc {public:      Abc( ){a=0; b=0; c=0;}  Abc(int i,int j,int k){a=i; b=j; c=k;}  void get(int&i,int&j,int&k){i=a; j=b; k=c;} Abc operator * (Abc obj); private:      int a,b,c; };  Abc  Abc::operator * (Abc obj) {Abc tempobj;   tempobj.a=a*obj.a;  tempobj.b=b*obj.b;  tempobj.c=c*obj.c;  return tempobj; }  void main()  { Abc obj1(1,2,3),obj2(5,5,5),obj3;   int a,b,c;    obj3=obj1*obj2;   obj3.get(a,b,c);  cout<<”(obj1*obj2):\\t”<<”a=”< cout<<”(obj2*obj3):\\t”<<”a=”< }   (obj1*obj2):    a=5     b=10    c=15 (obj2*obj3):    a=25    b=50    c=75  五、编程题  1、编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle( 长方形)、Trapezoid (梯形)和Triangle (三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。  不易理解的语句需要加适当注释。 注:主函数中定义如下对象  Circle circle(12.6);                                              Square square(3.5);                  Rectangle rectangle(4.5,8.4);     Trapezoid trapezoid(2.0,4.5,3.2);     Triangle triangle(4.5,8.4);     #include class Circle:public Shape{ public:       };  class Square:public Shape{ public:      Square(double a):l(a){} virtual double area() {   return l*l;  Circle(double a):r(a){} virtual double area() {  }  double r;  return r*r*Pi; virtual double area() {  }  return 0;  private:  百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库c++ 期末练习题+答案(4)在线全文阅读。
相关推荐: