c++实现产品功能(简单基础功能)

通过c++实现公司产品功能,要求能在VS和CB上完美运行,实现的功能基础简单,挑战难度低,希望能够得到解答

img

img

基于new bing的编写,有帮助记得采纳一下哦!:
【运行结果】

img

 
#include <iostream>
#include <string>
 
// 摄像机类
class Camera {
public:
  // 成员函数:拍摄照片
  void takePhoto() {
    std::cout << "Take a photo." << std::endl;
  }
  // 成员函数:设置图像质量
  void setImageQuality(int quality) {
    std::cout << "Set image quality to " << quality << "." << std::endl;
  }
};
 
// 行车记录仪类
class CarRecorder {
public:
  // 默认构造函数
  CarRecorder() : m_camera(new Camera()) {}
  // 带参构造函数
  CarRecorder(Camera* camera) : m_camera(camera) {}
  // 析构函数
  ~CarRecorder() {
    delete m_camera;
  }
  // 成员函数:操作行车记录仪的菜单
  void operateMenu() {
    std::cout << "Operate the menu of car recorder." << std::endl;
  }
  // 成员函数:使用摄像机拍摄
  void takePhoto() {
    m_camera->takePhoto();
  }
  // 成员函数:使用摄像机设置图像质量
  void setImageQuality(int quality) {
    m_camera->setImageQuality(quality);
  }
private:
  Camera* m_camera;
};
 
// 继承方式:行车记录仪类(使用摄像机的所有功能)
class CarRecorderWithInheritAll : public Camera {
public:
  // 成员函数:操作行车记录仪的菜单
  void operateMenu() {
    std::cout << "Operate the menu of car recorder." << std::endl;
  }
};
 
// 继承方式:行车记录仪类(仅使用摄像机的拍摄功能和图像质量设定功能)
class CarRecorderWithInheritPart : public Camera {
public:
  // 成员函数:操作行车记录仪的菜单
  void operateMenu() {
    std::cout << "Operate the menu of car recorder." << std::endl;
  }
};
 
// 继承方式:行车记录仪类(仅使用行车记录仪的菜单)
class CarRecorderWithInheritNone : public CarRecorder {
public:
  // 默认构造函数
  CarRecorderWithInheritNone() {}
  // 带参构造函数
  CarRecorderWithInheritNone(Camera* camera) : CarRecorder(camera) {}
};
 
// 组合方式:行车记录仪类
class CarRecorderWithCompose {
public:
  // 默认构造函数
  CarRecorderWithCompose() : m_camera(new Camera()) {}
  // 带参构造函数
  CarRecorderWithCompose(Camera* camera) : m_camera(camera) {}
  // 析构函数
  ~CarRecorderWithCompose() {
    delete m_camera;
  }
  // 成员函数:操作行车记录仪的菜单
  void operateMenu() {
    std::cout << "Operate the menu of car recorder." << std::endl;
  }
  // 成员函数:使用摄像机拍摄
  void takePhoto() {
    m_camera->takePhoto();
  }
  // 成员函数:使用摄像机设置图像质量
  void setImageQuality(int quality) {
    m_camera->setImageQuality(quality);
  }
private:
  Camera* m_camera;
};
 
int main() {
  // 继承方式
  std::cout << "Test inheritance." << std::endl;
 
  // 子类可以使用父类的所有成员
  CarRecorderWithInheritAll car1;
  car1.takePhoto();
  car1.setImageQuality(100);
  car1.operateMenu();
 
  // 子类只能使用父类的部分成员
  CarRecorderWithInheritPart car2;
  car2.takePhoto();
  car2.setImageQuality(100);
  car2.operateMenu();
 
  // 子类没有父类的成员
  Camera* camera = new Camera();
  CarRecorderWithInheritNone car3(camera);
  car3.operateMenu();
  delete camera;
 
  // 组合方式
  std::cout << "Test composition." << std::endl;
 
  // 使用组合方式创建对象,与继承方式效果一样
  Camera* camera2 = new Camera();
  CarRecorderWithCompose car4(camera2);
  car4.takePhoto();
  car4.setImageQuality(100);
  car4.operateMenu();
  delete camera2;
 
  // 同名函数覆盖
  std::cout << "Test overriding of same named functions." << std::endl;
  CarRecorderWithInheritAll car5;
  car5.takePhoto();  // 调用的是父类的 takePhoto() 函数
  car5.setImageQuality(100);  // 调用的是父类的 setImageQuality() 函数
 
  CarRecorderWithCompose car6;
  car6.takePhoto();  // 调用的是 Camera 类的 takePhoto() 函数
  car6.setImageQuality(100);  // 调用的是 Camera 类的 setImageQuality() 函数
 
 // 多态
std::cout << "Test polymorphism." << std::endl;
Camera* camera3 = new Camera();
CarRecorder* car7 = new CarRecorder(camera3);  // 使用派生类对象
CarRecorderWithInheritPart* car8 = new CarRecorderWithInheritPart();  // 使用派生类对象
CarRecorder* car9 = new CarRecorderWithInheritNone(new Camera());  // 使用基类指针和派生类对象
car7->takePhoto();  // 调用的是派生类对象的 takePhoto() 函数
car8->takePhoto();  // 调用的是派生类对象的 takePhoto() 函数
car9->takePhoto();  // 调用的是 Camera 类的 takePhoto() 函数
delete camera3;
delete car7;
delete car8;
delete car9;


  return 0;
}
 

在上述代码中,定义了 Camera 类和 CarRecorder 类,其中 Camera 类表示摄像机,CarRecorder 类表示行车记录仪。行车记录仪包含了摄像机的拍摄和图像质量设定功能,可以使用摄像机拍摄照片和设置图像质量,同时也有自己的操作菜单。使用继承方式和组合方式分别实现行车记录仪类,并且分别演示了不同的继承方式下成员访问属性的不同情况。

在继承方式中,分别使用了三个不同的继承方式来实现行车记录仪类:一个是继承所有摄像机的功能,一个是继承部分摄像机功能,另一个是没有继承摄像机的功能。演示了不同继承方式下的成员访问属性的不同情况,并且使用了同名函数覆盖来体现覆盖的概念。

在组合方式中,使用了 CarRecorderWithCompose 类来实现行车记录仪类,使用了 Camera 类的对象来实现摄像机的功能。与继承方式不同,组合方式使用了对象的组合关系来实现行车记录仪类。同时也演示了对象的初始化和析构顺序的不同,并且同样使用了同名函数覆盖来体现覆盖的概念。

最后,通过多态来演示多态的概念。由于 CarRecorder 类是一个抽象的概念,因此我们可以通过基类指针来引用不同的派生类实例,然后调用虚函数来体现多态的概念。

帮你简单写了几行,你可以继续扩展,记得采纳一下

#include <iostream>
using namespace std;

class Dashcam {
public:
    Dashcam(int res = 720, int time = 5);  // 构造函数
    virtual ~Dashcam();  // 析构函数
    virtual void takePhoto();  // 拍照
    virtual void recordVideo();  // 录像
    virtual void setResolution(int res);  // 设置分辨率
    virtual void setRecordTime(int time);  // 设置拍摄时间

protected:
    int resolution;  // 分辨率
    int recordTime;  // 拍摄时间
};

Dashcam::Dashcam(int res, int time) {
    resolution = res;
    recordTime = time;
    cout << "Dashcam created with resolution " << resolution << "p and recording time " << recordTime << "s." << endl;
}

Dashcam::~Dashcam() {
    cout << "Dashcam destroyed." << endl;
}

void Dashcam::takePhoto() {
    cout << "Take photo with resolution " << resolution << "p." << endl;
}

void Dashcam::recordVideo() {
    cout << "Record video for " << recordTime << "s with resolution " << resolution << "p." << endl;
}

void Dashcam::setResolution(int res) {
    resolution = res;
    cout << "Resolution set to " << resolution << "p." << endl;
}

void Dashcam::setRecordTime(int time) {
    recordTime = time;
    cout << "Recording time set to " << recordTime << "s." << endl;
}

class Dashcam1 : public Dashcam {
public:
    Dashcam1(int res = 720, int time = 5);  // 构造函数
    virtual ~Dashcam1();  // 析构函数
    virtual void takePhoto();  // 拍照
    virtual void recordVideo();  // 录像
};

Dashcam1::Dashcam1(int res, int time) : Dashcam(res, time) {
    cout << "Dashcam1 created with resolution " << resolution << "p and recording time " << recordTime << "s." << endl;
}

Dashcam1::~Dashcam1() {
    cout << "Dashcam1 destroyed." << endl;
}

void Dashcam1::takePhoto() {
    cout << "Take photo with resolution " << resolution << "p and color filter." << endl;
}

void Dashcam1::recordVideo() {
    cout << "Record video for " << recordTime << "s with resolution " << resolution << "p and frame stabilization." << endl;
}

class Dashcam2 : public Dashcam {
public:
    Dashcam2(int res = 720, int time = 5);  // 构造函数
    virtual ~Dashcam2();  // 析构函数
    virtual void takePhoto();  // 拍照
};

Dashcam2::Dashcam2(int res, int time) : Dashcam(res, time) {
    cout << "Dashcam2 created with resolution " << resolution << "p and recording time " << recordTime << "s." << endl;
}

Dashcam2::~Dashcam2() {
    cout << "Dashcam2 destroyed." << endl;
}

void Dashcam2::takePhoto() {
    cout << "Take photo with resolution " << resolution << "p and low-light enhancement." << endl;
}

int main() {
    Dashcam *dashcam = new Dashcam();
    dashcam->takePhoto();
    dashcam->recordVideo();
    dashcam->setResolution(1080);
    dashcam->setRecordTime(10);
    delete dashcam;

    Dashcam1 *dashcam1 = new Dashcam1();
    dashcam1->takePhoto();
    dashcam1->recordVideo();
    dashcam1->setResolution(1080);
    dashcam1->setRecordTime(10);
    delete dashcam1;

    Dashcam2 *dashcam2 = new Dashcam2();
    dashcam2->takePhoto();
    dashcam2->recordVideo();
    dashcam2->setResolution(1080);
    dashcam2->setRecordTime(10);
    delete dashcam2;

    return 0;
}

引用chatGPT作答,以下是实现你需求的代码:

#include <iostream>
#include <string>

using namespace std;

class Camera2 {
private:
    int imageQuality;
    string encodingAlgorithm;
public:
    Camera2(int quality, string algorithm): imageQuality(quality), encodingAlgorithm(algorithm) {}
    void takePicture() {
        cout << "Taking picture with quality " << imageQuality << " using " << encodingAlgorithm << " algorithm." << endl;
    }
};

class DrivingRecorder2 {
protected:
    Camera2 camera;
    string operatingMenu;
    bool motionRecording;
    int vehicleSpeed;
public:
    DrivingRecorder2(int quality, string algorithm, string menu, bool recording, int speed)
        : camera(quality, algorithm), operatingMenu(menu), motionRecording(recording), vehicleSpeed(speed) {}
    void operateMenu() {
        cout << "Operating the menu: " << operatingMenu << endl;
    }
    void startRecording() {
        motionRecording = true;
        cout << "Started recording." << endl;
    }
    void stopRecording() {
        motionRecording = false;
        cout << "Stopped recording." << endl;
    }
    void takePicture() {
        camera.takePicture();
    }
    void setSpeed(int speed) {
        vehicleSpeed = speed;
    }
};

class CarDrivingRecorder2 : public DrivingRecorder2 {
public:
    CarDrivingRecorder2(int quality, string algorithm, string menu, bool recording, int speed)
        : DrivingRecorder2(quality, algorithm, menu, recording, speed) {}
    void operate() {
        operateMenu();
        if (motionRecording) {
            camera.takePicture();
        }
    }
};

class CarDrivingRecorder3 : public DrivingRecorder2 {
public:
    CarDrivingRecorder3(int quality, string algorithm, string menu, bool recording, int speed)
        : DrivingRecorder2(quality, algorithm, menu, recording, speed) {}
    void operate() {
        operateMenu();
        if (motionRecording) {
            takePicture();
        }
    }
};

class CarDrivingRecorder4 : public DrivingRecorder2 {
public:
    CarDrivingRecorder4(int quality, string algorithm, string menu, bool recording, int speed)
        : DrivingRecorder2(quality, algorithm, menu, recording, speed) {}
    void operate() {
        operateMenu();
    }
};

int main() {
    CarDrivingRecorder2 recorder1(90, "H.264", "Main Menu", true, 60);
    CarDrivingRecorder3 recorder2(90, "H.264", "Main Menu", true, 60);
    CarDrivingRecorder4 recorder3(90, "H.264", "Main Menu", true, 60);

    recorder1.operate();
    recorder2.operate();
    recorder3.operate();

    return 0;
}

这个代码演示了三种不同的继承方式来设计行车记录仪类,并且添加了测试用例。