

很簡單的繼承,但是執行到 sp = new Rectangle(4, 6);時編譯器開始報錯
vs 和cb都不認識...
#include <iostream>
#include <windows.h>
using namespace std;
class Shape
{
public:
Shape() {}
~Shape() {}
virtual float GetArea()
{
return -1;
}
};
class Circle : public Shape
{
public:
Circle(float radius) :itsRadius(radius) {}
~Circle() {}
float GetArea()
{
return (float)3.14* itsRadius * itsRadius;
}
private:
float itsRadius;
};
class Rectangle :public Shape
{
private:
float itsWidth;
float itsLength;
public:
Rectangle(float len, float width)
{
itsLength = len;
itsWidth = width;
};
~Rectangle() {};
virtual float GetArea()
{
return itsLength * itsWidth;
}
virtual float GetLength()
{
return itsLength;
}
virtual float GetWidth()
{
return itsWidth;
}
};
class Square :public Rectangle
{
public:
Square(float len) : Rectangle(len, len) {};
~Square() {};
};
int main()
{
system("color F3");
Shape *sp;
sp = new Circle(5);
cout << "The area of the Circle is " << sp->GetArea() << endl;
delete sp;
sp = new Rectangle(4, 6);
cout << "The area of the Rectangle is " << sp->GetArea() << endl;
delete sp;
sp = new Square(5);
cout << "The area of the Square is " << sp->GetArea() << endl;
delete sp;
return 0;
}
uj5u.com熱心網友回復:
將類名class Rectangle ,改為:class Rectangle_m試試。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/269668.html
標籤:C++ 語言
上一篇:小白資料結構的求助
下一篇:新人提問
