用c++编程。编写重载函数area()和perime(),分别编写圆,长方形,正方形的
用c++编程。编写重载函数area()和perime(),分别编写圆,长方形,正方形的面积和周长,并在主函数中调用,输出面积和周长的值,要求圆周率定义为常量3.14。...
用c++编程。编写重载函数area()和perime(),分别编写圆,长方形,正方形的面积和周长,并在主函数中调用,输出面积和周长的值,要求圆周率定义为常量3.14。
展开
2个回答
展开全部
#include <iostream>
using namespace std;
#define PI 3.14
struct _circular
{
double radius;//半径
double perimeter; //周长
double area; //面积
};
struct _square
{
double length; //边长
double perimeter; //周长
double area; //面积
};
struct _rectangle
{
double length;//长
double width;//宽
double perimeter; //周长
double area; //面积
};
void area(struct _circular *cir);
void area(struct _square *squ);
void area(struct _rectangle *rect);
int main(int argc, char const *argv[])
{
struct _circular cirlular;
struct _square square;
struct _rectangle rectangle;
cout << "输入圆的半径: " ;
cin >> cirlular.radius;
cout << "输入正方形的边长: ";
cin >> square.length;
cout << "输入长方形的长和宽: ";
cin >> rectangle.length >> rectangle.width;
area(&cirlular);
area(&square);
area(&rectangle);
cout << "圆的周长为:" << cirlular.perimeter << ",圆的面积为:" << cirlular.area << endl;
cout << "正方形的周长为:" << square.perimeter << ",圆的面积为:" << square.area << endl;
cout << "长方形的周长为:" << rectangle.perimeter << ",圆的面积为:" << rectangle.area << endl;
return 0;
}
void area(struct _circular *cir)
{
cir->perimeter = 2*PI*(cir->radius);
cir->area = PI*(cir->radius)*(cir->radius);
}
void area(struct _square *squ)
{
squ->perimeter = 4*(squ->length);
squ->area = (squ->length)*(squ->length);
}
void area(struct _rectangle *rect)
{
rect->perimeter = 2*((rect->length) + (rect->width));
rect->area = (rect->length) * (rect->width);
}
using namespace std;
#define PI 3.14
struct _circular
{
double radius;//半径
double perimeter; //周长
double area; //面积
};
struct _square
{
double length; //边长
double perimeter; //周长
double area; //面积
};
struct _rectangle
{
double length;//长
double width;//宽
double perimeter; //周长
double area; //面积
};
void area(struct _circular *cir);
void area(struct _square *squ);
void area(struct _rectangle *rect);
int main(int argc, char const *argv[])
{
struct _circular cirlular;
struct _square square;
struct _rectangle rectangle;
cout << "输入圆的半径: " ;
cin >> cirlular.radius;
cout << "输入正方形的边长: ";
cin >> square.length;
cout << "输入长方形的长和宽: ";
cin >> rectangle.length >> rectangle.width;
area(&cirlular);
area(&square);
area(&rectangle);
cout << "圆的周长为:" << cirlular.perimeter << ",圆的面积为:" << cirlular.area << endl;
cout << "正方形的周长为:" << square.perimeter << ",圆的面积为:" << square.area << endl;
cout << "长方形的周长为:" << rectangle.perimeter << ",圆的面积为:" << rectangle.area << endl;
return 0;
}
void area(struct _circular *cir)
{
cir->perimeter = 2*PI*(cir->radius);
cir->area = PI*(cir->radius)*(cir->radius);
}
void area(struct _square *squ)
{
squ->perimeter = 4*(squ->length);
squ->area = (squ->length)*(squ->length);
}
void area(struct _rectangle *rect)
{
rect->perimeter = 2*((rect->length) + (rect->width));
rect->area = (rect->length) * (rect->width);
}
展开全部
你这个重载我不知道怎么重载,应该是重写吧?如果是重写的话就是下面的代码
#include <iostream>
using namespace std;
#define PI 3.14
// 几何图形类
class Shape
{
public:
Shape() {}
virtual ~Shape() {}
// 计算面积
virtual double area() const { return 0.0; }
// 计算周长
virtual double perime() const { return 0.0; }
};
// 圆
class Circle : public Shape
{
public:
Circle(double dRadius) :m_dRadius(dRadius) {}
// 计算面积
double area() const { return PI * m_dRadius * m_dRadius; }
// 计算周长
double perime() const { return 2 * PI * m_dRadius; }
private:
double m_dRadius;
};
// 长方形
class Rectangle :public Shape
{
public:
Rectangle(double dLength, double dWidth) :m_dLength(dLength), m_dWidth(dWidth) {}
// 计算面积
double area() const { return m_dLength * m_dWidth; }
// 计算周长
double perime() const { return 2 * (m_dLength + m_dWidth); }
private:
double m_dLength;
double m_dWidth;
};
// 正方形
class Square : public Shape
{
public:
Square(double dEdge) :m_dEdge(dEdge) {}
// 计算面积
double area() const { return m_dEdge * m_dEdge; }
// 计算周长
double perime() const { return 4.0 * m_dEdge; }
private:
double m_dEdge;
};
int main()
{
// 生成图形
Shape *pCircle = new Circle(1.0);
Shape *pRectangle = new Rectangle(2.0, 1.0);
Shape *pSquare = new Square(1.0);
// 输出结果
cout << "圆的面积为:" << pCircle->area() << " 周长为:" << pCircle->perime() << endl;
cout << "长方形的面积为:" << pRectangle->area() << " 周长为:" << pRectangle->perime() << endl;
cout << "正方形的面积为:" << pSquare->area() << " 周长为:" << pSquare->perime() << endl;
// 释放内存
delete pCircle;
delete pRectangle;
delete pSquare;
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询