基类是抽象类我迷了。。这个程序如何改正啊?求解答

超低调呀 |浏览817次
收藏|2019/04/17 09:06

满意回答

2019/04/17 09:26

程序错的太多了(与抽象类无关的)改好的你参考下#include<iostream>using namespace std;const double PI=3.14159;class Shape{ public: Shape(int x,int y=0); virtual void getarea()=0; virtual void getpreim()=0; protected: int m_x; int m_y;};Shape::Shape(int x,int y){ m_x=x,m_y=y;}void Shape::getarea(){ cout<<" destructor!"<<endl;}void Shape::getpreim(){ cout<<" destructor!"<<endl;}class Rectangle:public Shape{ public: Rectangle(int rx,int ry); void getarea(); void getpreim(); private: int m_rx; int m_ry;};Rectangle::Rectangle(int rx,int ry):Shape(rx,ry) { }void Rectangle::getarea(){ cout<<"面积="<<m_x*m_y<<endl;}void Rectangle::getpreim(){ cout<<"周长="<<2*(m_x+m_y)<<endl;}class Circle:public Shape{ public: Circle(int r); void getarea(); void getpreim(); private: int m_r;}; Circle::Circle(int r):Shape(0,0) { m_r=r;}void Circle::getarea(){ cout<<"面积2="<<PI*m_r*m_r<<endl;}void Circle::getpreim(){ cout<<"周长2="<<2*PI*m_r<<endl;}int main(){ Shape *a[2]; a[0]=new Rectangle(10,5); a[0]=new Rectangle(10,5); a[0]->getarea(); a[0]->getpreim(); a[1]=new Circle(10); a[1]=new Circle(10); a[1]->getarea(); a[1]->getpreim(); for(int i=0; i<2; i++) { delete a[i]; } system("pause"); return 0;}

whoami1978

其他回答(0)
0人关注该问题
+1

 加载中...