一个C++问题,请大家帮帮忙啊

时间:2022-06-02 08:57:11

一个C++小程序,请大家帮帮忙哈。。。谢谢

怎么将私有数据传到派生类,让派生类函数成员实用。

#include<iostream.h>
const double PI=3.1415;
class Shape
{
public:
 Shape(int l,int w,int r):length(r),width(w),radius(r){}
 virtual void getArea()=0;
 virtual void getPerim()=0;
    virtual int getl()=0;
 virtual int getw()=0;
 virtual int getr()=0;
private:
 int length,width,radius;
};
class Rectangle:public Shape
{
public:
 Rectangle(int i,int j,int k):Shape(i,j,k){}
 void getArea();
 void getPerim();
 int getl(){return length;}
 int getw(){return width;}
 int getr(){return radius;}
};
void Rectangle::getArea()
{
 int area;
 area=getl()*getw();
 cout<<"Rectangle area is:"<<area<<endl;
}
void Rectangle::getPerim()
{
 int perim;
 perim=getl()+getl()+getw()+getw();
 cout<<"Recatanle perim is:"<<perim<<endl;
}
class Circle:public Shape
{
public:
 Circle(int i,int j,int k):Shape(i,j,k){}
 void getArea();
 void getPerim();
 int getl(){return length;}
 int getw(){return width;}
 int getr(){return radius;}
};
void Circle::getArea()
{
 double area;
 area=PI*getr()*getr();
 cout<<"Circle area is:"<<area<<endl;
}
void Circle::getPerim()
{
 double perim;
 perim=2*PI*getr();
 cout<<"Circle perim is:"<<perim<<endl;
}
void fun(Shape *ptr)
{
 ptr->getArea();
 ptr->getPerim();
}
int main()
{
 int x,y,z;
 cout<<"please input length and width:";cin>>x>>y;
 cout<<"please input radius:";cin>>z;
 Rectangle R(x,y,z);
 Circle C(x,y,z);
 fun(&R);
 fun(&C);
 return 0;
}