C++程序(1)设计一个图形类,从其派生矩形,圆,三角形等类,设计计算图形面积函数f1,判断点是否在图形内部函数f2 (2)设计好类后,编写主函数进行测试。

时间: 2024-03-22 14:37:41 浏览: 30
以下是对应的C++代码: ```cpp #include <iostream> #include <cmath> using namespace std; class Shape { protected: double area; public: virtual double f1() = 0; virtual bool f2(double x, double y) = 0; }; class Rectangle : public Shape { private: double length; double width; double x1, y1, x2, y2, x3, y3, x4, y4; public: Rectangle(double l, double w, double x, double y) : length(l), width(w) { x1 = x; y1 = y; x2 = x + length; y2 = y; x3 = x + length; y3 = y + width; x4 = x; y4 = y + width; } double f1() { area = length * width; return area; } bool f2(double x, double y) { if (x > x1 && x < x3 && y > y1 && y < y3) return true; else return false; } }; class Circle : public Shape { private: double radius; double x, y; public: Circle(double r, double x0, double y0) : radius(r), x(x0), y(y0) {} double f1() { area = 3.14 * radius * radius; return area; } bool f2(double x0, double y0) { double d = sqrt((x0 - x) * (x0 - x) + (y0 - y) * (y0 - y)); if (d <= radius) return true; else return false; } }; class Triangle : public Shape { private: double a, b, c; double x1, y1, x2, y2, x3, y3; public: Triangle(double a0, double b0, double c0, double x0, double y0) : a(a0), b(b0), c(c0) { x1 = x0; y1 = y0; x2 = x0 + b0; y2 = y0; x3 = x0 + (b0 * b0 + c0 * c0 - a0 * a0) / (2 * b0); y3 = y0 + sqrt(c0 * c0 - (x3 - x2) * (x3 - x2)); } double f1() { double p = (a + b + c) / 2; area = sqrt(p * (p - a) * (p - b) * (p - c)); return area; } bool f2(double x0, double y0) { double d1 = (x2 - x1) * (y0 - y1) - (y2 - y1) * (x0 - x1); double d2 = (x3 - x2) * (y0 - y2) - (y3 - y2) * (x0 - x2); double d3 = (x1 - x3) * (y0 - y3) - (y1 - y3) * (x0 - x3); if ((d1 > 0 && d2 > 0 && d3 > 0) || (d1 < 0 && d2 < 0 && d3 < 0)) return true; else return false; } }; int main() { Rectangle r(5.0, 3.0, 0, 0); Circle c(4.0, 0, 0); Triangle t(5.0, 4.0, 3.0, 0, 0); cout << "Rectangle area: " << r.f1() << endl; cout << "Point (1, 1) is in Rectangle? " << r.f2(1, 1) << endl; cout << "Circle area: " << c.f1() << endl; cout << "Point (3, 4) is in Circle? " << c.f2(3, 4) << endl; cout << "Triangle area: " << t.f1() << endl; cout << "Point (2, 2) is in Triangle? " << t.f2(2, 2) << endl; return 0; } ``` 在这个程序中,我们首先定义了一个抽象基类 `Shape`,其中包含两个纯虚函数 `f1()` 和 `f2()`,分别用于计算图形的面积和判断点是否在图形内部。然后我们从这个基类中派生出三个类:`Rectangle`、`Circle` 和 `Triangle`,分别代表矩形、圆和三角形。 在每个派生类中,我们都实现了基类中的两个纯虚函数,并添加了一些额外的数据成员和函数。例如,在 `Rectangle` 类中,我们定义了矩形的长、宽和四个顶点的坐标,并在 `f2()` 函数中通过比较点的坐标和矩形顶点的坐标来判断点是否在矩形内部。 最后,在主函数中,我们创建了一个矩形、一个圆和一个三角形的对象,并分别调用它们的 `f1()` 和 `f2()` 函数来进行测试。

相关推荐

最新推荐

recommend-type

按以下描述和要求建立两个类:基类 Rectangle(矩形类) 和派生类 Cube(正方体)

按以下描述和要求建立两个类:基类 Rectangle(矩形类) 和派生类 Cube(正方体) 1. Rectangle 私有成员:  double x1, y1; //左下角的坐标  double x2, y2; //右上角的坐标 公有成员:  带缺省值的构造...
recommend-type

C++获取类的成员函数的函数指针详解及实例代码

主要介绍了C++获取类的成员函数的函数指针详解及实例代码的相关资料,需要的朋友可以参考下
recommend-type

网考《C++语言程序设计》模拟题.docx

网考《C++语言程序设计》模拟题,需要的自取,基本上都能找到原题,已考过,网考《C++语言程序设计》
recommend-type

C++实现图形界面时钟表盘代码

主要介绍了C++实现图形界面时钟表盘代码,涉及坐标函数的应用及图形界面程序设计,需要的朋友可以参考下
recommend-type

2019常州市程序设计小能手比赛试题.pdf

2019常州市程序设计小能手比赛试题 常州市中小学C++编程比赛试题 涉及if else 单循环,嵌套循环,一维数组,二维数组
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。