#include
using namespace std;
class shaped
{
double width,length;
public:
shaped(){width = 0;length = 0;}
double getWidth(){return width;}
double getLength(){return length;}
void get_data()
{
cin>>width;
cin>>length;
if(!cin.good())
{
cin.clear();
length=0;
}
}
virtual void display_area()
{
}
};
class triangle:public shaped
{
public :
void display_area()
{
cout<<"Area of triangle = "<<getWidth()*getLength()<class rectangle:public shaped
{
public:
void display_area()
{
cout<<"Area of rectangle = "<<getWidth()*getLength()/2<class circle:public shaped
{
public:
void display_area()
{
cout<<"Area of circle = "<<3.1416*getWidth()*getWidth()<int main()
{
triangle t;
rectangle r;
circle c;
cout<<"Enter the value of x & y for triangle :";
t.get_data();
cout<<"Enter the value of x & y for rectangle :";
r.get_data();
cout<<"Enter the value of radius for circle :";
c.get_data();
t.display_area();
r.display_area();
c.display_area();
return 0;
}
删除cin>>length
在园的类里重载getdata
不成熟的小建议, 程序逻辑有问题, 这种继承比较乱, 基类数据成员和派生类数据成员逻辑对不上, 建议基类只做公用接口, 和数据成员有关的放在派生类中.
当然, 如果就是要这种逻辑, 那就在基类多加一个只 cin wide的函数, 如果在派生类中重载, 基类的私有成员必须变为protect, 否则无法直接访问, 也就无法改变.