hhl987805837 2024-01-03 22:48 采纳率: 66.7%
浏览 3

怎么实现声明和定义分离


#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cmath>
using namespace std;

// 定义一个抽象类 Shape,表示几何形状
class Shape {
public:
    // 纯虚函数,计算面积
    virtual double area() = 0;
    // 纯虚函数,计算周长
    virtual double perimeter() = 0;
    // 纯虚函数,输出形状信息
    virtual void print() = 0;
    // 虚析构函数
    virtual ~Shape() {}
};

// 定义一个派生类 Rectangle,表示矩形
class Rectangle : public Shape {
private:
    double length; // 长
    double width; // 宽
public:
    // 构造函数,初始化长和宽
    Rectangle(double l, double w) : length(l), width(w) {}
    // 重载 area 函数,计算矩形的面积
    double area() override {
        return length * width;
    }
    // 重载 perimeter 函数,计算矩形的周长
    double perimeter() override {
        return 2 * (length + width);
    }
    // 重载 print 函数,输出矩形的信息
    void print() override {
        cout << "Rectangle: length = " << length << ", width = " << width << ", area = " << area() << ", perimeter = " << perimeter() << endl;
    }
};

// 定义一个派生类 Circle,表示圆形
class Circle : public Shape {
private:
    double radius; // 半径
    const double PI = 3.14159; // 圆周率
public:
    // 构造函数,初始化半径
    Circle(double r) : radius(r) {}
    // 重载 area 函数,计算圆形的面积
    double area() override {
        return PI * radius * radius;
    }
    // 重载 perimeter 函数,计算圆形的周长
    double perimeter() override {
        return 2 * PI * radius;
    }
    // 重载 print 函数,输出圆形的信息
    void print() override {
        cout << "Circle: radius = " << radius << ", area = " << area() << ", perimeter = " << perimeter() << endl;
    }
};

// 定义一个派生类 Triangle,表示三角形
class Triangle : public Shape {
private:
    double a; // 边长 a
    double b; // 边长 b
    double c; // 边长 c
public:
    // 构造函数,初始化三条边
    Triangle(double x, double y, double z) : a(x), b(y), c(z) {
        // 判断是否能构成三角形,如果不能,抛出异常
        if (a + b <= c || a + c <= b || b + c <= a) {
            throw invalid_argument("Invalid triangle sides!");
        }
    }
    // 重载 area 函数,计算三角形的面积
    double area() override {
        // 使用海伦公式
        double p = (a + b + c) / 2.0; // 半周长
        return sqrt(p * (p - a) * (p - b) * (p - c)); // 面积
    }
    // 重载 perimeter 函数,计算三角形的周长
    double perimeter() override {
        return a + b + c;
    }
    // 重载 print 函数,输出三角形的信息
    void print() override {
        cout << "Triangle: a = " << a << ", b = " << b << ", c = " << c << ", area = " << area() << ", perimeter = " << perimeter() << endl;
    }
};
// 定义一个派生类 diamond ,代表菱形
class Diamond : public Shape{
private:
    double diagonal1;//对角线1 
    double diagonal2;//对角线2
public:
        //构造函数,初始化两条边
        Diamond(double d1,double d2) :diagonal1(d1) , diagonal2(d2) {}
        // 重载 area 函数 
        double area() override{
        return diagonal1 * diagonal2;}
        // 重载 perimeter 函数 ,计算函数周长
        double perimeter() override {
        return 4 * (std::sqrt(diagonal1*diagonal1)+(diagonal2*diagonal2)/4);}
        // 重载 print 函数,输出菱形的信息
    void print() override {
        cout << "Diagonal: d1 = " << diagonal1 << ", Diagonal2 = " << diagonal2 << ", area = " << area() << ", perimeter = " << perimeter() << endl;
    } 
}; 


// 定义一个运算符重载函数,实现两个形状的面积相加 
double operator+(Shape& s1, Shape& s2) {
    return s1.area() + s2.area();
}

// 定义一个比较函数,按照面积从小到大排序
bool compare(Shape* s1, Shape* s2) {
    return s1->area() < s2->area();
}

// 定义一个函数,将形状的数据写入文件
void writeToFile(vector<Shape*>& shapes, const string& filename) {
    // 打开文件,如果不存在则创建
    ofstream fout(filename);
    // 判断文件是否打开成功
    if (fout) {
        // 遍历形状的向量
        for (int i = 0; i < shapes.size(); i++) {
            // 获取当前形状的类型
            string type;
            if (dynamic_cast<Rectangle*>(shapes[i])) {
                type = "Rectangle";
            }
            else if (dynamic_cast<Circle*>(shapes[i])) {
                type = "Circle";
            }
            else if (dynamic_cast<Triangle*>(shapes[i])) {
                type = "Triangle";
            }
            else if (dynamic_cast<Diamond*>(shapes[i])) {
                type = "Diagonal";
            }
            // 将形状的类型、编号、面积、周长写入文件,用逗号分隔
            fout << type << "," << i + 1 << "," << shapes[i]->area() << "," << shapes[i]->perimeter() << endl;
        }
        // 关闭文件
        fout.close();
    }
    else {
        // 文件打开失败,输出错误信息
        cerr << "Failed to open file: " << filename << endl;
    }
}

// 主函数
int main() {
    // 创建一个形状的向量,用于存储各种形状的指针
    vector<Shape*> shapes;
    shapes.push_back(new Rectangle(3, 4));
    shapes.push_back(new Rectangle(4, 5));
    shapes.push_back(new Rectangle(5, 6));
    shapes.push_back(new Rectangle(6, 7));
    shapes.push_back(new Rectangle(8, 9));
    // 创建五个圆形对象,并将其指针加入向量
    shapes.push_back(new Circle(5));
    shapes.push_back(new Circle(6));
    shapes.push_back(new Circle(7));
    shapes.push_back(new Circle(8));
    shapes.push_back(new Circle(9));
    // 创建五个三角形对象,并将其指针加入向量
    shapes.push_back(new Triangle(3, 4, 5));
    shapes.push_back(new Triangle(4, 5, 6));
    shapes.push_back(new Triangle(5, 6, 7));
    shapes.push_back(new Triangle(6, 7, 8));
    shapes.push_back(new Triangle(7, 8, 9));
    //创建五个菱形对象,并将其指针加入向量
    shapes.push_back(new Diamond(3, 4));
    shapes.push_back(new Diamond(4, 5));
    shapes.push_back(new Diamond(5, 6));
    shapes.push_back(new Diamond(6, 7));
    shapes.push_back(new Diamond(7, 8)); 
    // 输出形状的信息
    cout << "Shapes information:" << endl;
    for (auto s : shapes) {
        s->print();
    }
    // 计算两个形状的面积相加,并输出结果
    cout << "The sum of area of the first and second shape is: " << *shapes[0] + *shapes[1] << endl;
    // 按照面积从小到大对形状进行排序
    sort(shapes.begin(), shapes.end(), compare);
    // 输出排序后的形状信息
    cout << "Shapes information after sorting by area:" << endl;
    for (auto s : shapes) {
        s->print();
    }
    // 将形状的数据写入文件
    writeToFile(shapes, "shapes.txt");
    // 释放形状的内存
    for (auto s : shapes) {
        delete s;
    }
    return 0;
}
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2024-01-04 08:35
    关注

    【相关推荐】



    • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/831736
    • 你也可以参考下这篇文章:类声明和成员函数定义分离
    • 除此之外, 这篇博客: 随机化算法求定积分中的 2.解题思路 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

      在这里插入图片描述
      思路:由上图可以看出,当 x(1,2)区间内时,函数f(x)的值是不大于 1的,故可在1 < x < 20 < y < 1范围内进行随机投介质,观察落在f(x)范围的概率


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 1月3日

悬赏问题

  • ¥15 Opencv(C++)异常
  • ¥15 VScode上配置C语言环境
  • ¥15 汇编语言没有主程序吗?
  • ¥15 这个函数为什么会爆内存
  • ¥15 无法装系统,grub成了顽固拦路虎
  • ¥15 springboot aop 应用启动异常
  • ¥15 matlab有关债券凸性久期的代码
  • ¥15 lvgl v8.2定时器提前到来
  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错