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++ 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函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa