本文主要是介绍C++面向对象程序设计之几何图形计算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Shape是一个几何图形的基类,它至少有求自身周长函数Circumference()和求自身面积函数Area().从Shape类派生出Circle类、Ellipse类、Triangle类和Rectangle类,分别维承基类Shape的 Circumference()和Area(),并增加新的成员。编写主函数,定义各派生类对象,求多派生类对象的周长之和、面积之和。
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Shape
{
public:virtual double area() = 0;virtual double Ctrcumference() = 0;};class Circle : public Shape
{
public:Circle(double r) :radius(r) {}virtual double area() { return 3.14 * radius * radius; }virtual double Ctrcumference() { return 2 * 3.14 * radius; }protected:double radius;
};class Recentage : public Shape
{
public:Recentage(double l, double w) : longth(l), width(w) {}virtual double area() { return longth * width; }virtual double Ctrcumference() { return 2 * (longth * width); }protected:double longth;double width;
};class Triangle : public Shape
{
public:Triangle(double l, double w) : longth(l), width(w) {}virtual double area() { return 0.5 * longth * width; }virtual double Ctrcumference() { return (longth + width + sqrt(longth * longth + width * width)); }protected:double longth;double width;
};
class Ellipse : public Shape
{
public:Ellipse(double sa, double la) { short_as = sa; long_as = la; }virtual double area() { return 3.14 * short_as * long_as; }virtual double Ctrcumference() { return (2 * 3.14 * short_as + 4 * (long_as - short_as)); }
protected:double short_as;double long_as;};void printArea_Ctr(Shape& s)
{cout <<"面积:"<< s.area() <<","<< "周长:"<<s.Ctrcumference() << endl;
}void Meum_Selcet()
{cout << "-----------欢迎使用几何图形运算-----------------" << endl;cout << " number 0: circle(圆)" << endl;cout << " number 1: Recentage(矩形)" << endl;cout << " number 2: Triangle(三角形)" << endl;cout << " number 3: ellipse(椭圆)" << endl;cout << " number else: 四种几何图形的周长,面积总和 " << endl;}int main()
{Meum_Selcet();
loop:{double ToTal_Area = 0;double ToTal_Ctr = 0;cout << "Please choose the parterns of the Geometric Calculation:" << endl;int choose = 0;cin >> choose;switch (choose){case 0:{double r = 0;cout << "Please enter the menbers of Circle:" << endl;cin >> r;Circle circle(r);cout << "the area and the ctrcumference of the circle:" << endl;printArea_Ctr(circle);}; break;case 1:{ cout << "Please enter the menbers of recentage:" << endl;double lo = 0; double wi = 0;cin >> lo >> wi;Recentage recentage(lo, wi);cout << "the area and the ctrcumference of the recentage:" << endl;printArea_Ctr(recentage);}; break;case 2:{ cout << "Please enter the menbers of triangle:" << endl;double lt = 0; double wt = 0;cin >> lt >> wt;Triangle triangle(lt, wt);cout << "the area and the ctrcumference of the triangle:" << endl;printArea_Ctr(triangle);}; break;case 3:{cout << "Please enter the menbers of ellipse:" << endl;double sl = 0; double ll = 0;cin >> sl >> ll;Ellipse ellipse(sl, ll);cout << "the area and the ctrcumference of ellipse:" << endl;printArea_Ctr(ellipse);}; break;default:{ cout << "Please enter the parameters of the four Geometry: " << endl;cout << "圆半径,矩形长宽,三角形底和高,椭圆长短半轴:" << endl;double r1, lo1, wi1, lt1, wt1, sl1, ll1;cin >> r1 >> lo1 >> wi1 >> lt1 >> wt1 >> sl1 >> ll1;Circle circle(r1);Recentage recentage(lo1, wi1);Triangle triangle(lt1, wt1);Ellipse ellipse(sl1, ll1);ToTal_Area = circle.area() + recentage.area() + triangle.area() + ellipse.area();ToTal_Ctr = circle.Ctrcumference() + recentage.Ctrcumference() + triangle.Ctrcumference() + ellipse.Ctrcumference();cout << "派生类对象的面积之和、周长之和:" << endl;cout << ToTal_Area << "," << ToTal_Ctr << endl;}; break;}cout << '\n' << "输入Left离开计算界面/输入Continue继续进行运算" << endl;string ch;cin >> ch;if (ch == "Left")cout << "————————感谢使用几何图形运算!——————————" << endl;else if (ch == "Continue")goto loop;elsecout << "请重新选择(Left or Continue)!" << endl;}return 0;}
运行结果如下:
这篇关于C++面向对象程序设计之几何图形计算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!