简体   繁体   中英

C++ Composition Constructor

So I've got a program with classes Car, Route and Taxi. I'm using composition in order to obtain the data from the classes Car and 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. How to access the parameters in the setRouteTaxidata method?

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. 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:

#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

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 . Although those go into a lot more detail. You are not dealing with a lot of different types of RouteTaxi and subclasses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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