使用C ++和使用合成创建对象的一些指导

问题描述 投票:-1回答:1

基本上我的问题是作文。我理解这个原理,但是我在其中一个测试中苦苦执行。

从下面的ComputerMonitor的代码,我必须创建一个最终类Complect,它将有自己的名称,计算机的名称,显示器的名称,以及将由price()函数组成的价格。

Computer.h

#ifndef COMPUTER_H
#define COMPUTER_H
#include <string>

class Computer{

public:
    Computer(std::string name, int ram, double price); 

    std::string name() const; 
    int ram() const;
    double price() const;


    void printComputer() const;

    void setComputer(std::string name, int ram, double price);

private:
    std::string its_name;
    int ram_gb;

    double cost_price;
};
#endif // COMPUTER_H

Computer.cpp

#include "Computer.h"
#include <iostream>

Computer::Computer(std::string name, int ram, double price)
: its_name(name), ram_gb(ram), cost_price(price){
}

std::string Computer::name() const {
    return its_name;
}

int Computer::ram() const {
    return ram_gb;
}

double Computer::price() const {
    return cost_price;
}

void Computer::printComputer() const{ 
    std::cout << "Computer name = " <<name() <<"\n"
    << "Computer RAM = " <<ram() <<" GB\n"
    << "Computer Price = " << price() <<" EUR \n";
}

Monitor.h

#ifndef MONITOR_H
#define MONITOR_H
#include <string>

class Monitor{

public:
    Monitor(std::string name, std::string type, double price);

    std::string name() const;
    std::string type() const;
    double price() const;

    //print computer
    void printMonitor() const;

    //set computer
    void setMonitor(std::string name, std::string type, double price);

private:
    std::string its_name;
    std::string type_set;

    double cost_price;
};

#endif // MONITOR_H

Monitor.cpp

#include "Monitor.h"
#include <iostream>

Monitor::Monitor(std::string name, std::string type, double price) : its_name(name), type_set(type), cost_price(price){
}

std::string Monitor::name() const {
    return its_name;
}

std::string Monitor::type() const{
    return type_set;
}

double Monitor::price() const {
    return cost_price;
}

void Monitor::printMonitor() const{
    std::cout << "Monitor name = " <<name() <<"\n"
    << "Monitor type = " <<type() <<"\n"
    << "Monitor price = " << price() <<" EUR \n";
}

这是我所做的课程:

Complect.h

#ifndef COMPLECT_H
#define COMPLECT_H
#include <string>

class Complect{

public:
    Complect(std::string name, std::string computername, std::string monitorname,  double price);
    std::string name() const;
    std::string computername() const;
    std::string monitorname() const;
    double price() const;

    void printComplect();

    void setComplect(std::string name, std::string computername, std::string monitorname, double price);

private:
    std::string complect_name;
    std::string computername_final;
    std::string monitorname_final;
    double cost_price;

};

#endif // COMPLECT_H

Complect.cpp

#include "Complect.h"
#include "Monitor.h"
#include "Computer.h"
#include <iostream>

Complect::Complect(std::string name, std::string computername, std::string monitorname, double price) :
complect_name(name), computername_final(computername), monitorname_final(monitorname), cost_price(price){
}

std::string Complect::name() const{
    return complect_name;
}

std::string Complect::computername() const{
    return computername_final;
}

std::string Complect::monitorname() const{
    return monitorname_final;
}

double Complect::price() const{
    return cost_price;
}

void Complect::printComplect(){
    std::cout << "Complect name = " << name() <<"\n"
    << "Computer name = " <<computername() <<"\n"
    <<"Monitor name = " <<monitorname() <<"\n"
    <<"Complect price = " <<price() <<" EUR \n";
}

以下是我如何使用Main.cpp中的类

#include <iostream>
#include "Computer.h"
#include "Monitor.h"
#include "Complect.h"

int main(){

    Computer asus("Asus One", 8, 545.95) ;
    asus.printComputer() ; 
    std::cout << "\n";

    Monitor iiyama("Iiyama Blackhawk 27inch", "LED", 299.99);
    iiyama.printMonitor();
    std::cout <<"\n";

    Complect numberOne ("Number one complect", asus.name(), iiyama.name(), iiyama.price() + asus.price());
    numberOne.printComplect();
    std::cout <<"\n";

system ("pause");
return 0;
}

最终的结果是应该是什么,所以这段代码有效。但问题是它的结构不正确。

main.cpp文件中,您将看到正在创建Complect对象。但我目前在main.cpp文件中提供该对象的所有信息。

对不起这些代码有点凌乱,但是我试图绕过这个并且在此刻挣扎......如何让complect.cpp文件中的类提供自己所有的信息?

c++ oop object composition class-design
1个回答
1
投票

目前你不使用组合,但从其他两个类复制属性,你在main的构造函数调用中做了很多工作

您的代码的第一个小变化是在构造函数中获取计算机和监视器的实例:

Complect(std::string name, const Computer &, const Monitor &);

当然最终价格也在该构造函数内计算,在main中你只需创建一个包含其名称及其部分的Complect:

Complect numberOne ("Number one complect", asus, iiyama);

现在主要幸运的是不必知道如何计算价格,否则想象一下如果公式改变并且你必须更新构造函数的所有调用:-(。计算价格的方式只是Complect的责任。

Complect numberOne ("Number one complect", asus, iiyama);

必须更新构造函数setComplect以接收由于相同原因而分离的监视器和计算机的价格。

void setComplect(std::string name, std::string computername, std::string monitorname, double computerprice, double monitorprice);

或者可能更好地用方法替换它

void setname(std::string name);
void setcomputer(std::string computername, double computerprice);
void setmonitor(std::string monitorname, double monitorprice);

但是要在Complect中复制计算机和监视器的所有属性是不切实际的,并且您没有实例的组合。第一种可能性是保存它们的副本:

class Complect{
  ...
  private:
     Computer computer;
     Monitor monitor;
     // does not need attribute "double cost_price;"
  ...
};

Complect::Complect(std::string name, const Computer & c, const Monitor & m)
   : complect_name(name), computer(c), monitor(m) {
}

std::string Complect::computername() const{
    return computer.name();
}

std::string Complect::monitorname() const{
    return monitor.name();
}

double Complect::price() const{
    return computer.price() + monitor.price();
}

void Complect::printComplect(){
    std::cout << "Complect name = " << name() <<"\n"
    << "Computer name = " << computer.name() <<"\n"
    <<"Monitor name = " << monitor.name() <<"\n"
    <<"Complect price = " << price() <<" EUR \n";
}

该解决方案的优点是,如果Monitor和Computer的初始实例消失,您不会受到影响。缺点是,例如,如果克隆部件之一的价格发生变化,除非通过调用setXXX,价格不会更新


另一种方法是不克隆监视器和计算机,但你不能只有这样:

class Complect{
  ...
  private:
     Computer & computer;
     Monitor & monitor;
     // does not need attribute "double cost_price;"
  ...
};

Complect::Complect(std::string name, const Computer & c, const Monitor & m)
   : complect_name(name), computer(c), monitor(m) {
}

因为假设存在相应的Complect实例,Monitor和Computer的实例仍然存在

幸运的是,C ++提供了有趣的功能来管理它

© www.soinside.com 2019 - 2024. All rights reserved.