2016-10-19 2 views
-1

두 클래스가 있습니다. 포털 및 항공사. 둘 다 가상과 추상적입니다. 이 코드는 실제로 대략 만들어졌습니다. 그 죄송합니다. 필자는 Airline 및 Portal의 파생 클래스를 각각 AIR 및 POR로 작성했습니다.파생 클래스 생성자에 대한 코드는 어떻게 작성해야합니까?

기본적으로 내 메인은 파생 클래스 POR의 포털 포인터를 생성하고 항공기 파생 클래스 AIR에 전달됩니다. 항공사 포털 포인터를 인수로 사용하는 생성자가 있지만 AIR 생성자를 만드는 방법에 관해서는 혼란 스럽습니다.

내가 이해할 수없는 오류가 발생합니다. 클래스 파일은 우리에게 주어지며 교수마다 변경 될 수 없습니다. 클래스 파일에 문제가 있으면 알려주십시오.

class Portal { 
public: 
Portal() {} 
virtual ~Portal() {} 

enum SortField {Airline, Time, Duration, Price}; 
enum SortOrder {Ascending, Descending}; 
enum BuyOption {Cheapest, Fastest, Earliest, Latest}; 

// initialize route information by reading in input file with government data/guidelines 
virtual void initRoutes(string routeInfo) {} 

// return constraints/guidelines for a given origin+destination combination 
virtual void routeInfo(string origin, string destination, float& dist, float& duration, float& minPrice, float& maxPrice, float& timeVariation) {} 

// display (to cout) the list of flights with available seats between origin and destination 
// List is to be shown in sorted order, based on sortField and sortOrder 
// sortField is one of the values of enum SortField 
// sortOrder is one of values of enum SortOrder 
virtual void showFlights(string origin, string destination, SortField sortField = Airline, SortOrder sortOrder = Descending) {} 

// purchase a ticket based on BuyOption criteria, optionally specifying a preferred airline 
// This will be for the last selected origin+destination combination 
virtual bool buyTicket(BuyOption cirteria, string airline = NULL) = 0; 

}; 

class POR: public Portal{ 
protected: 
vector<Route *> rt_list; 
public: 
void initRoutes(string routeInfo){ 

    int n=0; 
    string line; 

    float val; 
    string str; 

    ifstream rfile; 
    rfile.open(routeInfo.c_str(),ios::in); 
    if(rfile.is_open()){ 
     while(!rfile.eof()){ 
      getline(rfile,line); 
      ++n; 
     } 
    } 
    rfile.close(); 

    rfile.open(routeInfo.c_str(),ios::in); 
    Route *ptr; 

    while(!rfile.eof()){ 
     rfile >> str; 
     ptr->setorigin(str); 
     rfile >> str; 
     ptr->setdestination(str); 
     rfile >> val; 
     ptr->setdistance(val); 
     rfile >> val; 
     ptr->setduration(val); 
     rfile >> val; 
     ptr->setminpkm(val); 
     rfile >> val; 
     ptr->setmaxpkm(val); 
     rfile >> val; 
     ptr->setdev(val); 

     rt_list.push_back(ptr); 
    } 
    rfile.close(); 
} 

void routeInfo(string origin, string destination, float& dist, float& duration,float& minPrice, float& maxPrice, float& timeVariation){ 
    int len = rt_list.size(); 
    string str1,str2; 
    for(int i = 0;i < len;i++){ 
     str1 = rt_list[i]->getOrigin(); 
     str2 = rt_list[i]->getDestination(); 
     if((str1 == origin and str2 == destination) or (str1 == destination and str2 == origin)){ 
      cout<<rt_list[i]->getDistance(); 
      cout<<rt_list[i]->getDuration(); 
      cout<<rt_list[i]->getMinpkm() * rt_list[i]->getDistance(); 
      cout<<rt_list[i]->getMaxpkm() * rt_list[i]->getDistance(); 
      cout<<rt_list[i]->getDev(); 
     } 
    } 
} 

void showFlights(string origin, string destination, SortField sortField = Airline,SortOrder sortOrder = Descending){ 

} 

bool buyTicket(BuyOption cirteria, string airline = NULL){ 

} 
}; 
class Airline { 

private: 
Portal *portal; 

protected: 
Portal *getPortal() { 
    return portal; 
} 

public: 

Airline(Portal *pl):portal(pl){ 

} 

virtual ~Airline() {} 

// reads in the input file for the airline and initializes its 
// information about routes, capacity etc 
virtual void init(string ifile) = 0; 

virtual string getName() = 0; // the name of the airline. Should have the last 4 digits of the roll no. 

// return the list of flights for this airline between origin and destination cities 
// The list of flights is appended to the vector flights that is passed in. 
// Input vector flights will not be a null reference 
virtual void findFlights(string origin, string destination, 
         vector<Flight *>& flights) {} 

// get the current price for a specified flight of this airline 
virtual float getPrice(Flight *flight) = 0; 

// buy a ticket from this airline for a particular flight 
// Returns true if the ticket can be issues (i.e. there are seats available on the flight 
virtual bool issueTicket(Flight *flight) = 0; 

// number of tickets sold today 
virtual int getNumSold() = 0; 

// get total revenue and passenger km for the day 
virtual void getSalesStats(float& revenue, float& passKm) {} 

}; 

class AIR:public Airline{ 
protected: 
POR *portal; 
string name; 
int seats,nflightsperday; 
string o,d; 
vector<string> org; 
vector<string> dest; 
vector<int> nflights; 
vector<FLY *> flight_list; 
public: 
//CONSTRUCTOR ?????? 

/* 
AIR(){ 
    ; 
} 
*/ 

AIR(POR *pl):portal(pl){ 
    name = ""; 
} 

void init(string ifile){ 
    FLY *ptr; 
    ifstream fin; 
    fin.open(ifile.c_str(), ios::in); 
    fin >> seats; 
    while(!fin.eof()){ 
     fin >> o >> d; 
     org.push_back(o); 
     dest.push_back(d); 
     fin >> nflightsperday; 
     nflights.push_back(nflightsperday); 

     for(int i=0;i<nflightsperday;i++){ 
      ptr->setOrigin(o); 
      ptr->setDestination(d); 
      ptr->setName(ifile.c_str()); 
      flight_list.push_back(ptr); 
     } 

    } 
    fin.close(); 
} 

/*void readfile(ifstream& fin,string ifile){ 
    fin.open(ifile.c_str(), ios::in); 
    fin >> seats; 
    while(!fin.eof()){ 
     fin >> o >> d; 
     org.push_back(o); 
     dest.push_back(d); 
     fin >> nflightsperday; 
     nflights.push_back(nflightsperday); 
    } 
    fin.close(); 
}*/ 

string getName(){ 
    //cout<<typeid(obj).name(); 
    return "AIR"; 
} 
void findFlights(string origin, string destination, vector<Flight *>& flights){ 

} 
float getPrice(Flight *flight){ 

} 
bool issueTicket(Flight *flight){ 

} 
int getNumSold(){ 
    return -1; 
} 
void getTicketStats(float& revenue, float& passKm){ 

} 
}; 


int main(){ 
POR *x=new POR; 
AIR a1(x); 
return 0; 
} 

나는 그런 오류를 받고 있어요 : 어쨌든 여기

는 코드입니다.

version4.cpp: In constructor ‘AIR::AIR(POR*)’: 
version4.cpp:303:24: error: no matching function for call to ‘Airline::Airline()’ 
AIR(POR *pl):portal(pl){ 
        ^
version4.cpp:303:24: note: candidates are: 
version4.cpp:251:2: note: Airline::Airline(Portal*) 
Airline(Portal *pl):portal(pl){ 
^ 
version4.cpp:251:2: note: candidate expects 1 argument, 0 provided 
version4.cpp:239:7: note: Airline::Airline(const Airline&) 
class Airline { 
    ^
version4.cpp:239:7: note: candidate expects 1 argument, 0 provided 
+0

너무 오래, 너무 많은 코드를. 문제를 보여주는 작은 프로그램으로 잘라냅니다. 그렇게하면 문제를 찾는 데 도움이 될 수도 있습니다. – mjs

+0

인수를 허용하는 생성자를 정의하면 기본 생성자가 삭제됩니다. – AndyG

+0

'eof' [here] (http://stackoverflow.com/questions/5605125/why-is-iostreameof- inside-a-loop-condition-considered-wrong)에 대해 설명합니다. – molbdnilo

답변

0

또한 AIR의 구조는 기본 클래스를 구성해야합니다.
Airline에는 기본 생성자가 없으며 Portal*을 허용하는 생성자 만 있습니다.

Airline에는 이미 포털 멤버가 있으므로 AIR에 다른 멤버를 정의하면 안됩니다.

대신 당신은 Airline의 건설에 생성자의 매개 변수를 전달해야합니다

AIR::AIR(Portal* p) : Airline(p) 
{ 

} 
관련 문제