谁能帮帮我啊,C++

来源:百度知道 编辑:UC知道 时间:2024/09/21 20:41:19
声明一个基类Shape(形状),在此基础上派生出Rectangle(矩形)和Circle(圆),二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square(正方形)。最后对所创建的Rectangle、Circle和Square三类进行测试

用vs2008调试通过

  #include "stdafx.h"
  #include "iostream"

  using namespace std;

  class shape
  {
  public:
  shape(){}
  ~shape(){}
  virtual float getarea()=0;
  };

  class rectangle:public shape
  {
  public:
  rectangle(float length,float weight){itslength=length;itsweight=weight;}
  ~rectangle(){}
  float getarea(){return itslength*itsweight;}
  protected:
  float itslength,itsweight;
  };

  class circle:public shape
  {
  public:
  circle(float radius){itsradius=radius;}
  ~circle(){}
  float getarea(){return 3.1415*itsradius*itsradius;}
  private:
  float itsradius;
  };

  class square:public rectangle
  {
  public:
  square(float sidelength):rectangle(sidelength,sidelength){}
  ~square(){}
  };

  int _tmain(int argc, _TCHAR* argv[])
  {