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++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

Python中经纬度距离计算的实现方式

《Python中经纬度距离计算的实现方式》文章介绍Python中计算经纬度距离的方法及中国加密坐标系转换工具,主要方法包括geopy(Vincenty/Karney)、Haversine、pyproj... 目录一、基本方法1. 使用geopy库(推荐)2. 手动实现 Haversine 公式3. 使用py

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用