2017-12-06 7 views
0

그래서 Car, Route, Taxi 클래스의 프로그램이 있습니다. 저는 Car와 Routes 클래스로부터 데이터를 얻기 위해 컴포지션을 사용하고 있습니다. 각 클래스는 자신의 데이터를 입력 할 수 있어야하고 확인을 위해 입력 된 데이터가 있어야합니다. Taxi 클래스의 생성자를 Car 및 Routes 클래스의 get/set 메서드를 사용하여 데이터를 사용하고 검사하는 방식으로 만드는 방법에 어려움을 겪고 있습니다. setRouteTaxidata 방법의 매개 변수에 액세스하는 방법은 무엇입니까?C++ 작곡가 생성자

제안 사항?

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); 

} 

답변

0

생성자 RouteTaxi()는 기본 생성자입니다. 그것들 중 아무 것도 사용하지 않고 유효한 객체를 만들어야 만하므로 허용 가능한 기본값을 가져야합니다.

이 예에서와 같이,이 당신이 "nocar"자동차와 "갑자기"경로가없는 한 기본 경로를 만들기 위해 더 좋은 방법은 없다, 그래서 당신이 대신 생성자에서 그 인수가 필요 할 수 있습니다

#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); 
} 

생성자가 자체 생성자 본문을 실행하기 전에 다른 개체를 초기화하는 데 사용하는 콜론 및 초기화 프로그램 목록에 유의하십시오. http://en.cppreference.com/w/cpp/language/initializer_list

개체 생성 도구 팩토리 생성기는 개체를 만드는 또 다른 방법입니다. 실제 생성자를 호출하기 전에 모든 정보가 필요하므로 ... 생성자를 실제로 호출하기 전에 가져 오는 함수를 작성하십시오.

이것은 잘 알려진 디자인 패턴입니다. https://en.wikipedia.org/wiki/Creational_pattern을 참조하십시오. 그것들은 훨씬 더 자세하게 들어갑니다. RouteTaxi와 하위 클래스의 많은 다른 유형을 다루지 않습니다.

+0

감사합니다. 그 생성자가 있습니다. 내 문제는 나중에 내가 내 개체를 콘솔에서 imputed 및 오류를 확인하려면, 나는 정적이되고 싶지 않아요. 미안 해요 만약 내가 이해가 안 돼요. –

+0

아마 공장 생성기 기능을 원할 것입니다. 그것을 추가하겠습니다. –

+0

시간 내 주셔서 감사합니다. –