C++面向对象程序设计之几何图形计算

2023-10-15 11:40

本文主要是介绍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++面向对象程序设计之几何图形计算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/217459

相关文章

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序