C++继承——以应用抽象类,求圆、圆内接正方形、圆外切正方形的面积和周长为例

这篇博客介绍了C++中继承的三种方式:public、protected和private,并讨论了继承的优缺点。通过一个实例,展示了如何使用抽象类来计算圆、圆内接正方形和圆外切正方形的面积和周长,以此说明继承在实际问题中的应用。
摘要由CSDN通过智能技术生成

c++中基类的继承及其使用,以圆的内接正方形和外切正方形为例

1:C++中继承的方式
C++中的继承主要分为三类:public公有继承、protected保护继承以及private私有继承,其使用范围这里不再赘述。
2:C++继承的优缺点
优点:
1、类继承是在编译时刻静态定义的,且可直接使用,
2、类继承可以较方便地改变父类的实现。
缺点:
1、因为继承在编译时刻就定义了,所以无法在运行时刻改变从父类继承的实现
2、父类通常至少定义了子类的部分行为,父类的任何改变都可能影响子类的行为
3、如果继承下来的实现不适合解决新的问题,则父类必须重写或被其他更适合的类替换。这种依赖关系限制了灵活性并最终限制了复用性。

例子——应用抽象类,求圆、圆内接正方形、圆外切正方形的面积和周长。


```cpp
#include <iostream>
#include <math.h>
using namespace std;
double pi=3.14;
class container
{
	public:
		container(double x){r=x;}
		virtual void area()=0;
		virtual void permeter()=0;
	protected:
		double r;
};
class circle:public container
{
	public:
		circle(double x):container(x){}
		void area()
		{
			cout<<"圆的面积是:"&
好的,以下是代码实现: ```c++ #include <iostream> #include <cmath> using namespace std; // 基类Shape,包含公共的属性和方法 class Shape { public: virtual double getArea() = 0; // 获取面积的抽象方法 virtual double getPerimeter() = 0; // 获取周长的抽象方法 }; // 矩形类Rectangle,继承自Shape基类 class Rectangle : public Shape { private: double width; double height; public: Rectangle(double w, double h) : width(w), height(h) {} double getArea() override { return width * height; } // 计算矩形面积 double getPerimeter() override { return 2 * (width + height); } // 计算矩形周长 }; // 正方形类Square,继承自Rectangle类 class Square : public Rectangle { public: Square(double s) : Rectangle(s, s) {} double getArea() override { return Rectangle::getArea(); } // 计算正方形面积 double getPerimeter() override { return Rectangle::getPerimeter(); } // 计算正方形周长 }; // 三角形类Triangle,继承自Shape基类 class Triangle : public Shape { private: double a; double b; double c; public: Triangle(double sideA, double sideB, double sideC) : a(sideA), b(sideB), c(sideC) {} double getArea() override { // 计算三角形面积 double p = (a + b + c) / 2; return sqrt(p * (p - a) * (p - b) * (p - c)); } double getPerimeter() override { return a + b + c; } // 计算三角形周长 }; // 形类Circle,继承自Shape基类 class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) {} double getArea() override { return M_PI * radius * radius; } // 计算面积 double getPerimeter() override { return 2 * M_PI * radius; } // 计算周长 }; // 椭类Ellipse,继承自Shape基类 class Ellipse : public Shape { private: double a; double b; public: Ellipse(double axisA, double axisB) : a(axisA), b(axisB) {} double getArea() override { return M_PI * a * b; } // 计算椭面积 double getPerimeter() override { double h = pow(a - b, 2) / pow(a + b, 2); return M_PI * (a + b) * (1 + 3 * h / (10 + sqrt(4 - 3 * h))); } // 计算椭周长 }; int main() { // 接收数据 double width, height, sideA, sideB, sideC, radius, axisA, axisB; cout << "请输入矩形的宽和高:"; cin >> width >> height; cout << "请输入正方形的边长:"; cin >> sideA; cout << "请输入三角形的三边长:"; cin >> sideA >> sideB >> sideC; cout << "请输入形的半径:"; cin >> radius; cout << "请输入椭的长轴和短轴:"; cin >> axisA >> axisB; // 计算周长面积 Rectangle rect(width, height); Square square(sideA); Triangle triangle(sideA, sideB, sideC); Circle circle(radius); Ellipse ellipse(axisA, axisB); // 输出结果 cout << "矩形的周长为:" << rect.getPerimeter() << ",面积为:" << rect.getArea() << endl; cout << "正方形周长为:" << square.getPerimeter() << ",面积为:" << square.getArea() << endl; cout << "三角形的周长为:" << triangle.getPerimeter() << ",面积为:" << triangle.getArea() << endl; cout << "形的周长为:" << circle.getPerimeter() << ",面积为:" << circle.getArea() << endl; cout << "椭周长为:" << ellipse.getPerimeter() << ",面积为:" << ellipse.getArea() << endl; return 0; } ``` 代码中定义了一个`Shape`基类,包含抽象的获取周长面积的方法。其中,`Rectangle`、`Square`、`Triangle`、`Circle`和`Ellipse`等类继承自`Shape`基类,分别实现对应的周长面积计算方法。在`main`函数中,从键盘接收数据后,分别用各个类的实例计算并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值