#include <iostream>
using namespace std;
class Shape
{
public:
virtual float getArea() = 0;
};
class Circle : public Shape
{
public:
Circle(float r) { radius = r; }
void setRadius()
{
cout << "请输入圆的半径=" << endl;
cin >> radius;
}
float getArea()
{
return 3.14 * radius * radius;
}
private:
float radius;
};
class Rectangle : public Shape
{
public:
Rectangle(float l,float w) { length = l, width = w; }
void setLength()
{
cout << "请输入长方形的长=" << endl;
cin >> length;
cout << "请输入长方形的宽=" << endl;
cin >> width;
}
float getArea()
{
return length*width;
}
private:
float length;
float width;
};
class Tringle : public Shape
{
Tringle(float a, float b,float c);
public:
void setA()
{
cout << "请输入三角形的边长a=" << endl;
cin >> a;
cout << "请输入三角形的边长b=" << endl;
cin >> b;
cout << "请输入三角形的边长c=" << endl;
cin >> c;
}
float getArea()
{
return sqrt(0.5 * (a + b + c) * (0.5 * (a + b + c) - a) * (0.5 * (a + b + c) - b) * (0.5 * (a + b + c) - c));
}
private:
float a;
float b;
float c;
};
int main()
{
Shape* s[3]; // 定义一个指针数组
s[0] = new Circle();
s[1] = new Rectangle();
s[2] = new Tringle();
int i;
for (i = 0; i < 3; i++)
{
s[i]->getArea();
}
for (i = 0; i < 3; i++) // 用完后要释放堆上创建的对象
{
delete s[i];
}
return 0;
}
帮你修改好了
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <stdexcept>
class Shape
{
public:
virtual ~Shape() {}
virtual float getArea() const = 0;
};
class Circle : public Shape
{
public:
Circle(float radius)
{
setRadius(radius);
}
float getRadius() const { return _radius; }
void setRadius(float radius)
{
if (radius <= 0.0f)
throw std::invalid_argument("invalid circle");
_radius = radius;
}
float getArea() const
{
return M_PI * _radius * _radius;
}
private:
float _radius;
};
class Rectangle : public Shape
{
public:
Rectangle(float length, float width)
{
setLength(length);
setWidth(width);
}
float getLength() const { return _length; }
void setLength(float length)
{
if (length <= 0.0f)
throw std::invalid_argument("invalid rectangle");
_length = length;
}
float getWidth() const { return _width; }
void setWidth(float width)
{
if (width <= 0.0f)
throw std::invalid_argument("invalid rectangle");
_width = width;
}
float getArea() const
{
return _length * _width;
}
private:
float _length;
float _width;
};
class Tringle : public Shape
{
public:
Tringle(float a, float b, float c)
{
set(a, b, c);
}
float getA() const { return _a; }
float getB() const { return _b; }
float getC() const { return _c; }
void set(float a, float b, float c)
{
if (a <= 0.0f || b <= 0.0f || c <= 0.0f || (a + b) <= c || (a + c) <= b || (b + c) <= a)
throw std::invalid_argument("invalid triangle");
_a = a;
_b = b;
_c = c;
}
float getArea() const
{
float s = (_a + _b + _c) / 2.0f;
return sqrt(s * (s - _a) * (s - _b) * (s - _c));
}
private:
float _a;
float _b;
float _c;
};
int main()
{
try
{
Shape *s[3];
float r;
std::cout << "请输入圆的半径=";
std::cin >> r;
s[0] = new Circle(r);
float l, w;
std::cout << "请输入长方形的长=";
std::cin >> l;
std::cout << "请输入长方形的宽=";
std::cin >> w;
s[1] = new Rectangle(l, w);
float a, b, c;
std::cout << "请输入三角形的边长a=";
std::cin >> a;
std::cout << "请输入三角形的边长b=";
std::cin >> b;
std::cout << "请输入三角形的边长c=";
std::cin >> c;
s[2] = new Tringle(a, b, c);
for (int i = 0; i < 3; i++)
std::cout << "图形" << i + 1 << "的面积=" << s[i]->getArea() << '\n';
for (int i = 0; i < 3; i++)
delete s[i];
}
catch (const std::exception &e)
{
std::cerr << e.what();
}
return 0;
}