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

  }

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

  {

  std::cout<<“please input x coordinate of the circle center(interger):”<<'\n';

  is>>circle.centerx;

  std::cout<<“please input y coordinate of the circle center(integer)”<<'\n';

  is>>circle.centery;

  std::cout<<“please input the radius of the circle (integer)”<<'\n';

  is>>circle.radius;

  return is;

  }

  myrectangle ::myrectangle()

  {

  length=0;

  width=0;

  }

  double myrectangle::area()

  {

  double area;

  area=length*width;

  return area;

  }

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

  {

  std::cout<<“please in put the length of the rectangle (integer)”<<'\n';

  is>>rectangle.length;

  std::cout<<“please input the width of the rectangle (integer)”<<'\n';

  is>>rectangle.width;

  return is;

  }

  4、(1) 实现描述栈的类Stack ,其中定义了栈的大小size,(即栈中元素的个数),并包括进栈函数Push

  ,出栈函数Pop和显示栈顶元素的函数Top,(自已定义了一个gettop函数)。

  (2)定义基类element,至少包含纯虚函数showme;

  (3)从基类element中派生整数类MYinteger和字符串类mystring,具体实现上述纯虚函数showme,

  显示该元素的类型和相应值。

  (3)重载“>>”操作符,使得可以通过cin直接读入上述整数类和字符类的对象值。

  编定main 测试以上功能

  Main.cpp

  #include“stack.h”

  #include<iostream>

  using std::cout;

  using std::cin;

  int main()

  {

  int i=0;

  integer *myint;

  mystring *mystrings;

  stack* stack1=new stack;

  while(i!=6){

  cout<<“please chiose(1-6):”;

  cout<<“1:push an integer”<<'\n';

  cout<<“2:push a string ”<<'\n';

  cout<<“3:pop”<<'\n';

  cout<<“4:top”<<'\n';

  cout<<“5:gettop”<<'\n';

  cout<<“6:exit”<<'\n';

  cin>>i;

  switch(i)

  {

  case 1:

  myint=new integer;

  cin>>*myint;

  stack1->push(myint);

  break;

  case 2:

  mystrings =new mystring;

  cin>>*mystrings;

  stack1->push(mystrings);

  break;

  case 3:

  stack1->pop();

  break;

  case 4:

  stack1 ->top();

  break;

  case 5:

  stack1->gettop();

  break;

  case 6:

  break;

  }

  }

  return 0;

  }

  Stack.h

  #ifndef STACK_H

  #define STACK_H

  #include<iosfwd>

  class element

  {

  public:

  virtual void showme()=0;

  };

  class integer:public element

  {

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

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