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

相关文章

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ