为了账号安全,请及时绑定邮箱和手机立即绑定

不知道为什么程序报错

#include<iostream>

#include<string>

using namespace std;

class Shape{

public:

Shape();

virtual ~Shape();

virtual double calcArea(){

  cout<<"Shape---calcArea()"<<endl;

  return 0;

}

};

Shape::Shape(){

  cout<<"Shape()"<<endl;

}

Shape::~Shape(){

  cout<<"~Shape()"<<endl;

}

class Circle:public Shape{

protected:

double r;

Coordinate *pCenter;

public:

Circle(double _r);

virtual ~Circle();

virtual double calcArea(){

   cout<<"Circle---calcArea()"<<endl;

   return 3.14*r*r;

}

};

Circle::Circle(double _r){

   r=_r;

   pCenter=new Coordinate(3,5);

   cout<<"Circle"<<endl;

}

Circle::~Circle(){

delete pCenter;

pCenter=NULL;

   cout<<"~Circle()"<<endl;

}

class Rect:public Shape{

protected:

   double width;

   double height;

public:

Rect(double _width,double _height);

virtual ~Rect();

    virtual double calcArea(){

   cout<<"Rect---calcArea()"<<endl;

   return width*height;

}

};

Rect::Rect(double _width,double _height){

   width=_width;

   height=_height;

   cout<<"Rect()"<<endl;

}

Rect::~Rect(){

  cout<<"~Rect()"<<endl;

}

class Coordiante{

private:

int x;

int y;

public:

Coordinate(int _x,int _y);

~Coordinate();

};

Coordinate(int _x,int _y){

  cout<<"Coordinate()"<<endl;

  x=_x;

  y=_y;

}

Coordinate::~Coordinate(){

  cout<<"~Coordinate()"<<endl;

}

int main(){

  Shape *shape1=new Rect(5,6);

  Shape *shape2=new Circle(5);

  shape1->calcArea();

  shape2->calcArea();

  delete shape1;

  shape1=NULL;

  delete shape2;

  shape2=NULL;

  return 0;

}


正在回答

1 回答

  1. Coordinate 要放在前面定义,这样shape才能调用

  2. 定义Coordinate的构造函数要写成Coordinate::Coordinate(int _x, int _y)

  3. 你的class Coordinate打错了

2 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之多态篇
  • 参与学习       66236    人
  • 解答问题       314    个

本教程将带领大家体会面向对象三大特性中的多态特性

进入课程

不知道为什么程序报错

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信