这是我的代码:
/*
create an abstract class shape and derived classes rectangle and circle from class shape, implement abstract method of class shape in rectangle and circle. Class circle contains radius as data members rectangle class contains length and breadth.
*/
class shape
{
virtual void displayArea() = 0;
virtual void get_radius(double r) = 0;
virtual void get_length(double a) = 0;
virtual void get_breadth(double b) = 0;
};
class rectangle: public shape
{
protected:
double length;
double breadth;
public:
virtual void get_length(double a)
{
length = a;
}
virtual void get_breadth(double b)
{
breadth = b;
}
virtual void get_radius(double r)
{
cout << endl;
}
virtual void displayArea()
{
cout << "Area of RECTANGLE = " << length*breadth << endl;
}
};
class circle: public shape
{
protected:
double radius;
public:
virtual void get_length(double a)
{
cout << endl;
}
virtual void get_breadth(double b)
{
cout << endl;
}
virtual void get_radius(double r)
{
radius = r;
}
virtual void displayArea()
{
cout << "Area of circle = " << 3.14*radius*radius << endl;
}
};
int main()
{
shape* shapes;
double l, r, b;
rectangle R;
circle C;
cout << "Enter the length and breadth for rectangle\n" << endl;
cin >> l >> b;
cout << "\nEnter the radius of circle\n " << endl;
cin >> r;
R.get_length(l);
R.get_breadth(b);
C.get_radius(r);
shapes[0] = R;
shapes[1] = C;
shapes[0].displayArea();
shapes[1].displayArea();
system("pause");
return 0;
}
它没有给出编译错误,但在运行时会发生这种情况:输入矩形的长度和宽度
3 3
输入圆的半径
3
然后它暂停一段时间然后终止。我很困惑,我在这里做错了什么以及如何纠正它?代码有什么问题?