简体   繁体   English

C ++复合构造函数

[英]C++ Composition Constructor

So I've got a program with classes Car, Route and Taxi. 所以我有一个包含Car,Route和Taxi类的程序。 I'm using composition in order to obtain the data from the classes Car and Route. 我正在使用组合以便从Car和Route类中获取数据。 Each class needs to be able to enter data on it's own and the data entered to be checked. 每个类都需要能够自己输入数据并输入要检查的数据。 I'm having difficulties with how to make the constructor of the Taxi class the way that he uses and checks the data with the get/set methods of the classes Car and Route. 我在如何使Taxi类的构造函数成为他使用和使用Car和Route类的get / set方法检查数据的方式上遇到困难。 How to access the parameters in the setRouteTaxidata method? 如何访问setRouteTaxidata方法中的参数?

Any suggestions? 有什么建议么?

class RouteTaxi {
    private:
        int id;
    public:
        Car car;
        Route route;    
        RouteTaxi();
        ~RouteTaxi();
        void setRouteTaxidata(string cbrand, string cmodel, int cyears, int cseatingCapacity, double cloadCapacity, double cfuelConsumption,string rnodes, double rrouteLength, int rtoursPerDay, int i);


};

RouteTaxi::RouteTaxi(){
    setRouteTaxidata(??)   ??????
}

void RouteTaxi::setRouteTaxidata(string cbrand, string cmodel, int cyears, int cseatingCapacity, double cloadCapacity, double cfuelConsumption, string rnodes, double rrouteLength, int rtoursPerDay, int i){
    car.setBrand(cbrand);

}

Your constructor RouteTaxi() is a default constructor. 您的构造函数RouteTaxi()是默认构造函数。 It must create a valid object without any of those, so you would have to have acceptable default values. 它必须创建一个没有任何对象的有效对象,因此您必须具有可接受的默认值。

There's no good way to make a default route, unless you have a "nowhere" route with a "nocar" car, so you can instead require those arguments in the constructor, as in this example: 除非您有一条带有“ nocar”汽车的“ nowhere”路线,否则没有默认路线的好方法,因此您可以在构造函数中要求使用这些参数,如以下示例所示:

#include<string>
using std::string;

class Car {
public:
    Car(string cbrand, string cmodel, int cyears, int cseatingCapacity, double cloadCapacity, double cfuelConsumption){}
};

class Route {
public:
    Route(string rnodes, double rrouteLength, int rtoursPerDay){}
};

class RouteTaxi
{
private:
    int id;
public:
    Car car;
    Route route;
    RouteTaxi(string cbrand, string cmodel, int cyears, int
        cseatingCapacity, double cloadCapacity, double cfuelConsumption,
        string rnodes, double rrouteLength, int rtoursPerDay, int i)
        : car(cbrand, cmodel, cyears, cseatingCapacity, cloadCapacity, cfuelConsumption),
          route(rnodes, rrouteLength, rtoursPerDay),
          id(i)
    {}
    static RouteTaxi generate_from_console_input();
};

// this is a factory function (class static member of RouteTaxi)
RouteTaxi RouteTaxi::generate_from_console_input() {
    // input from console
    string cbrand, cmodel, rnodes;
    int cyears, cseatingCapacity, rtoursPerDay, i;
    double cloadCapacity, cfuelConsuption, rrouteLength;

    // return the object
    // this is an error, using uninitialized data, simply because I am not actually getting the data from the console. You will do that, so it will not be a problem.
    return RouteTaxi(cbrand, cmodel, cyears, cseatingCapacity, cloadCapacity, cfuelConsuption, rnodes, rrouteLength, rtoursPerDay, i);
}

Note the colon and initializer list which the constructor uses to initialize other objects before it executes its own constructor body. 请注意冒号和初始化程序列表,构造函数在执行其自己的构造函数主体之前将其用于初始化其他对象。 http://en.cppreference.com/w/cpp/language/initializer_list http://en.cppreference.com/w/cpp/language/initializer_list

The factory generator is another way to create objects. 工厂生成器是创建对象的另一种方法。 You need all the info before you call the actual constructor, so ... write a function which gets it, before you actually call the constructor. 在调用实际的构造函数之前,您需要所有信息,因此...在实际调用构造函数之前,编写一个获取该信息的函数。

This is a well known design pattern. 这是一种众所周知的设计模式。 See https://en.wikipedia.org/wiki/Creational_pattern . 参见https://en.wikipedia.org/wiki/Creational_pattern Although those go into a lot more detail. 尽管这些细节很多。 You are not dealing with a lot of different types of RouteTaxi and subclasses. 您没有处理很多不同类型的RouteTaxi和子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM