【期末不挂科-C++考前速过系列P5】大二C++实验作业-多态性(3道代码题)【解析,注释】

简介: 【期末不挂科-C++考前速过系列P5】大二C++实验作业-多态性(3道代码题)【解析,注释】

前言

大家好吖,欢迎来到 YY 滴C++考前速过系列 ,热烈欢迎! 本章主要内容面向接触过C++的老铁

主要内容含:

\


程序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:

编写程序:定义抽象基类Shape,area( )为求图形面积的虚成员函数。由它派生出三个派生类: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); // 正方体边长为5
    shapes[1] = new Sphere(3); // 球体半径为3
    shapes[2] = new Cylinder(4, 6); // 圆柱体底面圆半径为4,高度为6

    for (int i = 0; i < 3; i++) {
        cout << "Volume of shape " << i+1 << ": " << shapes[i]->volume() << endl;
    }

    for (int i = 0; i < 3; i++) {
        delete shapes[i];
    }

    return 0;
}

在这个示例中,我们定义了抽象基类 Shape,并派生出了 Cube(正方体)、Sphere(球体)和

Cylinder(圆柱体)三个派生类。每个派生类都实现了虚函数 volume 来计算不同图形的体积。在 main

在这个示例中,我们定义了抽象基类 Shape,并派生出了 Cube(正方体)、Sphere(球体)和

Cylinder(圆柱体)三个派生类。每个派生类都实现了虚函数 volume 来计算不同图形的体积。在 main

相关文章
|
5天前
|
测试技术
函数式编程代码片段(无解析,代码纯享版)
函数式编程代码片段(无解析,代码纯享版)
8 0
|
6天前
|
Linux Shell 开发工具
C++ 的 ini 配置文件读写/注释库 inicpp 用法 [ header-file-only ]
这是一个C++库,名为inicpp,用于读写带有注释的INI配置文件,仅包含一个hpp头文件,无需编译,支持C++11及以上版本。该库提供简单的接口,使得操作INI文件变得容易。用户可通过`git clone`从GitHub或Gitee获取库,并通过包含`inicpp.hpp`来使用`inicpp::iniReader`类。示例代码展示了读取、写入配置项以及添加注释的功能,还提供了转换为字符串、双精度和整型的函数。项目遵循MIT许可证,示例代码可在Linux环境下编译运行。
37 0
|
6天前
|
机器学习/深度学习 编解码
【论文笔记】图像修复MPRNet:Multi-Stage Progressive Image Restoration 含代码解析2
【论文笔记】图像修复MPRNet:Multi-Stage Progressive Image Restoration 含代码解析
14 2
|
6天前
|
机器学习/深度学习 计算机视觉
【论文笔记】图像修复MPRNet:Multi-Stage Progressive Image Restoration 含代码解析1
【论文笔记】图像修复MPRNet:Multi-Stage Progressive Image Restoration 含代码解析
15 1
【51单片机】烧写教程:将代码下载到单片机中(图示&解析)
【51单片机】烧写教程:将代码下载到单片机中(图示&解析)
|
6天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
19 0
|
4天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
5天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
6天前
|
C++ Linux

推荐镜像

更多