您现在的位置: 汕头自考网 >> 串讲笔记 >> 理学类 >> 正文
  • 北大“面向对象程序设计”上机试题(含答案)
  • 发布日期时间:2007-3-6  来源:不详   点击数:  作者:佚名

  cin>>ID;

  int flag=1;

  int i=0;

  while((i<accnum)&&(flag))

  {

  if(ID==account[i]->getID())

  flag=0;

  else

  i++;

  }

  if(flag)

  cout<<endl<<“ the account does not exist ”<<endl;

  else

  {

  account[i]->showme();

  int choice =0;

  while (choice!=3)

  {

  cout<<“ 1: save money ”<<endl;

  cout<<“ 2: withdraw money ”<<endl;

  cout<<“3: return ”<<endl;

  cout<<“ please input your choice:”;

  cin>>choice;

  cout<<endl;

  switch( choice)

  {

  case 1:

  account[i]->saving();

  break;

  case 2:

  account[i]->withdrow();

  break;

  case 3:

  break;

  }

  }

  }

  return ;

  }

  3、(1)定义基类MYgraph,至少包含纯虚函数 Area,计算图形面积。

  (2)从基类MYgraph中派生圆形类MYcircle和矩形类MYrectangle,其中圆形信息包括圆形半径

  和圆心,矩形信息包括矩形的长和宽,具体实现上述纯虚函数Area,计算圆形和矩形的面积。

  (3)重载输入>>操作符,使得可以通过cin 直接读入上述圆形类和矩形类的对象值。

  (4)编定main 函数,测试上述所要求的各种功能,即可以读入圆或矩形的对象值,计算读入图形的面积并显示。

  Main.cpp

  #include“string.h”

  #include<iostream>

  int main()

  {mycircle circle;

  myrectangle rectangles;

  std::cin>>circle;

  std::cout<<“the area of the circle is ”<<circle.area()<<'\n';

  std::cin>>rectangles;

  std::cout<<“the area of the rectangle is ”<<rectangles.area()<<'\n';

  return 0;

  }

  String.h

  #ifndef STRING_H

  #define STRING_H

  #include<iomanip>

  #include<iostream>

  using std::istream;

  class graphy

  {

  public:

  virtual double area()=0;

  };

  class mycircle:graphy

  {

  public:

  mycircle();

  double area();

  friend istream &operator >>(istream & is,mycircle &circle);

  private:

  int centerx;

  int centery;

  int radius;

  };

  class myrectangle :graphy

  {

  public:

  myrectangle();

  double area();

  friend istream &operator >>(istream & is ,myrectangle &rectangle);

  private:

  int length;

  int width;

  };

  #endif

  String.cpp

  #include<cmath>

  #include<iostream>

  #include“string.h”

  //using std::istream;

  mycircle ::mycircle()

  {

  centerx=0;

  centery=0;

  radius=0;

  }

  double mycircle::area()

  {

  double area;

  area=3.14*radius*radius;

  ret

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] 下一页

文章转载请注明来源于:汕头自考网