怎么黑进网站后台,深圳市门户网站建设品牌,效果好企业营销型网站建设公司,物联网平台网站前言 大家好吖#xff0c;欢迎来到 YY 滴C考前速过系列 #xff0c;热烈欢迎#xff01; 本章主要内容面向接触过C的老铁 主要内容含#xff1a; 欢迎订阅 YY滴C专栏#xff01;更多干货持续更新#xff01;以下是传送门#xff01; YY的《C》专栏YY的《C11》专栏YY的《… 前言 大家好吖欢迎来到 YY 滴C考前速过系列 热烈欢迎 本章主要内容面向接触过C的老铁 主要内容含 欢迎订阅 YY滴C专栏更多干货持续更新以下是传送门 YY的《C》专栏YY的《C11》专栏YY的《Linux》专栏YY的《数据结构》专栏YY的《C语言基础》专栏YY的《初学者易错点》专栏YY的《小小知识点》专栏YY的《单片机期末速过》专栏YY的《C期末速过》专栏 目录 程序1程序2程序3 程序1 写一个程序定义抽象基类Container由它派生出3个派生类Sphere球体、Cylinder圆柱体、Cube正方体。用虚函数分别计算几种图形的表面积和体积。 以下是一个使用虚函数计算不同图形表面积和体积的示例程序
#include iostream
#include cmath
using namespace std;// 抽象基类Container
class Container {
public:virtual double calculateSurfaceArea() 0;virtual double calculateVolume() 0;
};// 派生类Sphere球体
class Sphere : public Container {
private:double radius;public:Sphere(double r) : radius(r) {}double calculateSurfaceArea() override {return 4 * M_PI * radius * radius;}double calculateVolume() override {return (4.0 / 3.0) * M_PI * radius * radius * radius;}
};// 派生类Cylinder圆柱体
class Cylinder : public Container {
private:double radius;double height;public:Cylinder(double r, double h) : radius(r), height(h) {}double calculateSurfaceArea() override {return 2 * M_PI * radius * radius 2 * M_PI * radius * height;}double calculateVolume() override {return M_PI * radius * radius * height;}
};// 派生类Cube正方体
class Cube : public Container {
private:double sideLength;public:Cube(double s) : sideLength(s) {}double calculateSurfaceArea() override {return 6 * sideLength * sideLength;}double calculateVolume() override {return sideLength * sideLength * sideLength;}
};int main() {Sphere s(5);Cylinder c(3, 7);Cube cu(4);cout Sphere Surface Area: s.calculateSurfaceArea() endl;cout Sphere Volume: s.calculateVolume() endl;cout Cylinder Surface Area: c.calculateSurfaceArea() endl;cout Cylinder Volume: c.calculateVolume() endl;cout Cube Surface Area: cu.calculateSurfaceArea() endl;cout Cube Volume: cu.calculateVolume() endl;return 0;
}在这个示例中我们定义了抽象基类 Container并派生出了 Sphere球体、Cylinder圆柱体和 Cube正方体三个派生类。每个派生类都实现了 calculateSurfaceArea 和 calculateVolume 虚函数来计算不同图形的表面积和体积。在 main 函数中我们创建了每种图形的实例并输出了它们的表面积和体积。 程序2 编写程序定义抽象基类Shapearea( )为求图形面积的虚成员函数。由它派生出三个派生类Circle圆形、Rectangle长方形、和Triangle 三角形用虚函数area分别计算各种图形的面积。在主函数中分别创建派生类的对象并计算其面积求出它们的面积的和。要求用基类指针数组使它的每一个元素指向一个派生类的对象以体现多态性。 以下是一个使用多态性的示例程序根据要求定义了抽象基类 Shape 和三个派生类 Circle圆形、Rectangle长方形和 Triangle三角形
#include iostream
#include cmath
using namespace std;// 抽象基类Shape
class Shape {
public:virtual double area() 0;
};// 派生类Circle圆形
class Circle : public Shape {
private:double radius;public:Circle(double r) : radius(r) {}double area() override {return M_PI * radius * radius;}
};// 派生类Rectangle长方形
class Rectangle : public Shape {
private:double length;double width;public:Rectangle(double l, double w) : length(l), width(w) {}double area() override {return length * width;}
};// 派生类Triangle三角形
class Triangle : public Shape {
private:double base;double height;public:Triangle(double b, double h) : base(b), height(h) {}double area() override {return 0.5 * base * height;}
};int main() {Shape* shapes[3];shapes[0] new Circle(5);shapes[1] new Rectangle(4, 6);shapes[2] new Triangle(3, 8);double totalArea 0;for (int i 0; i 3; i) {totalArea shapes[i]-area();}cout Total area of all shapes: totalArea endl;for (int i 0; i 3; i) {delete shapes[i];}return 0;
}在这个示例中我们定义了抽象基类 Shape并派生出了 Circle圆形、Rectangle长方形和 Triangle三角形三个派生类。每个派生类都实现了虚函数 area 来计算不同图形的面积。在 main 函数中我们创建了一个基类指针数组使其每个元素指向一个派生类的对象以体现多态性。然后我们计算了每个图形的面积并求出它们的面积的和。最后记得释放动态分配的内存。 程序3 计算正方体、球体、圆柱体的体积。要求定义基类shape在其中定义求体积的纯虚函数volume并定义一个数据成员r它可作为球体的半径、正方体的边长以及圆柱体的底面圆半径。由shape类派生出3个类分别是正方体类、球体类和圆柱体类在这3个类中都具有求体积的重定义版本。在主函数中统一求3种对象的体积实现多态性。 以下是符合您要求的C程序其中定义了一个抽象基类 Shape并派生出了 Cube正方体、Sphere球体和 Cylinder圆柱体三个派生类
#include iostream
#include cmath
using namespace std;// 抽象基类Shape
class Shape {
public:virtual double volume() 0;
};// 派生类Cube正方体
class Cube : public Shape {
private:double sideLength;public:Cube(double s) : sideLength(s) {}double volume() override {return sideLength * sideLength * sideLength;}
};// 派生类Sphere球体
class Sphere : public Shape {
private:double radius;public:Sphere(double r) : radius(r) {}double volume() override {return (4.0 / 3.0) * M_PI * pow(radius, 3);}
};// 派生类Cylinder圆柱体
class Cylinder : public Shape {
private:double radius;double height;public:Cylinder(double r, double h) : radius(r), height(h) {}double volume() override {return M_PI * pow(radius, 2) * height;}
};int main() {Shape* shapes[3];shapes[0] new Cube(5); // 正方体边长为5shapes[1] new Sphere(3); // 球体半径为3shapes[2] new Cylinder(4, 6); // 圆柱体底面圆半径为4高度为6for (int i 0; i 3; i) {cout Volume of shape i1 : shapes[i]-volume() endl;}for (int i 0; i 3; i) {delete shapes[i];}return 0;
}在这个示例中我们定义了抽象基类 Shape并派生出了 Cube正方体、Sphere球体和 Cylinder圆柱体三个派生类。每个派生类都实现了虚函数 volume 来计算不同图形的体积。在 main 函数中我们创建了一个基类指针数组使其每个元素指向一个派生类的对象以体现多态性。然后我们计算了每个图形的体积并输出结果。最后记得释放动态分配的内存。