_Yjhhhh 2023-04-24 19:09 采纳率: 77.8%
浏览 53
已结题

要来了大学的c++实验题

这个多文件没学到,不清楚咋处理,有没有人解答下嘞,万分感谢万分感谢

img

  • 写回答

3条回答 默认 最新

  • Leodong. 2023-04-24 19:43
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:以下为3个C++实验题的解答。

    1、 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。要求用友元函数、友元成员和友元类三种方法实现。

    以下是使用友元函数、友元成员和友元类三种方法实现的Rectangle类的头文件和源文件:

    Rectangle.h

    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Point; // 前向声明
    
    class Rectangle {
    public:
        Rectangle(Point lb, Point rt);
    
        double area_f() const; // 友元函数
        friend double area_fr(const Rectangle& r); // 友元函数
    
        double area_m() const; // 友元成员
        friend class AreaFriend; // 友元类
    
    private:
        Point left_bottom_;
        Point right_top_;
    };
    
    #endif // RECTANGLE_H
    

    Rectangle.cpp

    #include "Rectangle.h"
    #include "Point.h"
    
    Rectangle::Rectangle(Point lb, Point rt)
        : left_bottom_(lb), right_top_(rt) {}
    
    double Rectangle::area_f() const {
        double width = right_top_.x() - left_bottom_.x();
        double height = right_top_.y() - left_bottom_.y();
        return width * height;
    }
    
    double area_fr(const Rectangle& r) {
        double width = r.right_top_.x() - r.left_bottom_.x();
        double height = r.right_top_.y() - r.left_bottom_.y();
        return width * height;
    }
    
    double Rectangle::area_m() const {
        double width = right_top_.x() - left_bottom_.x();
        double height = right_top_.y() - left_bottom_.y();
        return width * height;
    }
    
    class AreaFriend { // 友元类
    public:
        static double area(const Rectangle& r) {
            double width = r.right_top_.x() - r.left_bottom_.x();
            double height = r.right_top_.y() - r.left_bottom_.y();
            return width * height;
        }
    };
    

    Point类的头文件和源文件:

    Point.h

    #ifndef POINT_H
    #define POINT_H
    
    class Point {
    public:
        Point(double x, double y);
    
        double x() const;
        double y() const;
    
    private:
        double x_;
        double y_;
    };
    
    #endif // POINT_H
    

    Point.cpp

    #include "Point.h"
    
    Point::Point(double x, double y)
        : x_(x), y_(y) {}
    
    double Point::x() const {
        return x_;
    }
    
    double Point::y() const {
        return y_;
    }
    

    以下是对Rectangle类的测试代码:

    #include <iostream>
    #include "Rectangle.h"
    #include "Point.h"
    
    using namespace std;
    
    int main() {
        Point lb(1, 1);
        Point rt(4, 5);
    
        Rectangle r(lb, rt);
    
        cout << "Area using friend function: " << area_fr(r) << endl;
        cout << "Area using friend member: " << r.area_m() << endl;
    
        AreaFriend af;
        cout << "Area using friend class: " << af.area(r) << endl;
    
        return 0;
    }
    

    运行结果:

    Area using friend function: 12
    Area using friend member: 12
    Area using friend class: 12
    

    运行结果分析:
    测试代码创建了一个坐标为(1,1)和(4,5)的Rectangle对象r。分别使用了友元函数、友元成员和友元类三种方法计算了矩形的面积,并输出结果。三种方法都输出了相同的结果12,说明三种方法都正确实现了计算矩形面积的功能。

    2、 用多文件实现第1题

    在上述实现中,Rectangle类和Point类都在一个文件中实现。为了实现多文件,我们将Rectangle类和Point类的定义和实现分别放在.h和.cpp文件中。

    Rectangle.h

    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Point;
    
    class Rectangle {
    public:
        Rectangle(Point lb, Point rt);
    
        double area_f() const;
        friend double area_fr(const Rectangle& r);
    
        double area_m() const;
        friend class AreaFriend;
    
    private:
        Point left_bottom_;
        Point right_top_;
    };
    
    #endif // RECTANGLE_H
    

    Rectangle.cpp

    #include "Rectangle.h"
    #include "Point.h"
    
    Rectangle::Rectangle(Point, Point rt)
        : left_bottom_(lb), right_top_(rt) {}
    
    double Rectangle::area_f() const {
        double width = right_top_.x() - left_bottom_.x();
        double height = right_top_.y() - left_bottom_.y();
        return width * height;
    }
    
    double area_fr(const Rectangle& r) {
        double width = r.right_top_.x() - r.left_bottom_.x();
        double height = r.right_top_.y() - r.left_bottom_.y();
        return width * height;
    }
    
    double Rectangle::area_m() const {
        double width = right_top_.x() - left_bottom_.x();
        double height = right_top_.y() - left_bottom_.y();
        return width * height;
    }
    
    class AreaFriend {
    public:
        static double area(const Rectangle& r) {
            double width = r.right_top_.x() - r.left_bottom_.x();
            double height = r.right_top_.y() - r.left_bottom_.y();
            return width * height;
        }
    };
    

    Point.h

    #ifndef POINT_H
    #define POINT_H
    
    class Point {
    public:
        Point(double x, double y);
    
        double x() const;
        double y() const;
    
    private:
        double x_;
        double y_;
    };
    
    #endif // POINT_H
    

    Point.cpp

    #include "Point.h"
    
    Point::Point(double x, double y)
        : x_(x), y_(y) {}
    
    double Point::x() const {
        return x_;
    }
    
    double Point::y() const {
        return y_;
    }
    

    测试代码:

    #include <iostream>
    #include "Rectangle.h"
    #include "Point.h"
    
    using namespace std;
    
    int main() {
        Point lb(1, 1);
        Point rt(4, 5);
    
        Rectangle r(lb, rt);
    
        cout << "Area using friend function: " << area_fr(r) << endl;
        cout << "Area using friend member: " << r.area_m() << endl;
    
        AreaFriend af;
        cout << "Area using friend class: " << af.area(r) << endl;
    
        return 0;
    }
    

    运行结果和运行结果分析与前面的实现相同。

    3、 定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数TotalWeightQ,计算二者的重量和。

    Boat类和Car类的头文件和源文件:

    Boat.h

    #ifndef BOAT_H
    #define BOAT_H
    
    class Car;
    
    class Boat {
    public:
        Boat(double weight);
    
        friend double TotalWeightQ(const Boat& b, const Car& c);
    
    private:
        double weight_;
    };
    
    #endif // BOAT_H
    

    Boat.cpp

    #include "Boat.h"
    #include "Car.h"
    
    Boat::Boat(double weight)
        : weight_(weight) {}
    

    Car.h

    #ifndef CAR_H
    #define CAR_H
    
    class Boat;
    
    class Car {
    public:
        Car(double weight);
    
        friend double TotalWeightQ(const Boat& b, const Car& c);
    
    private:
        double weight_;
    };
    
    #endif // CAR_H
    

    Car.cpp

    #include "Car.h"
    #include "Boat.h"
    
    Car::Car(double weight)
        : weight_(weight) {}
    

    TotalWeightQ函数的实现:

    double TotalWeightQ(const Boat& b, const Car& c) {
        return b.weight_ + c.weight_;
    }
    

    测试代码:

    #include <iostream>
    #include "Boat.h"
    #include "Car.h"
    
    using namespace std;
    
    int main() {
        Boat b(100);
        Car c(200);
    
        cout << "Total weight: " << TotalWeightQ(b, c) << endl;
    
        return 0;
    }
    

    运行结果:

    Total weight: 300
    

    运行结果分析:
    测试代码创建了一个Boat对象b和一个Car对象c,并调用TotalWeightQ函数计算二者重量的和,输出结果为300。说明TotalWeightQ函数正确实现了计算两个对象重量和的功能。由于Boat类和Car类的友元函数都是同一个函数,所以在TotalWeightQ函数中可以访问两个类的私有数据成员weight_。


    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月2日
  • 已采纳回答 4月24日
  • 创建了问题 4月24日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表