编写一个程序计算三角形、正方形和圆形三种图形的面积。

2024-12-04 11:52:08
推荐回答(3个)
回答2:

#include
#include
#include

using namespace std;

class base
{
public:
virtual void disp() = 0;
};

class triangle : public base
{
public:
triangle(int base, int height) : m_base(base), m_height(height) {}

void disp()
{
cout << "三角形面积: "渗型改 << m_base* m_height / 2 << endl;
}

private:
int m_base;
int m_height;
};

class square : public base
{
public:
square(int side) : m_side(side) {}

void disp()
{
cout << "正方形面丛判积: " << m_side* m_side << endl;
}

private:
int m_side;
};

class circle : public base
{
public:
circle(int radius) : m_radius(radius) {}

void disp()
{
cout << "圆租陆形面积: "
<< setprecision(5) // nasty!!
<< m_radius * m_radius * M_PI << endl;
}

private:
int m_radius;
};