如何编写派生类构造函数的代码?

How should I write the code for the derived class constructor?

本文关键字:代码 构造函数 何编写 派生      更新时间:2023-10-16

我有两个类。传送门和航空公司。两者分别是虚拟的和抽象的。代码编写得很粗略。很抱歉。我已经创建了派生类Airline和Portal分别为AIR和POR。

基本上,我的main创建了派生类POR的门户指针,然后将其传递到航空公司派生类AIR中。Airline有一个以门户指针为参数的构造函数,但我对如何为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

AIR的构造还需要构造基类。
Airline没有默认构造函数,它只有一个可以接受Portal*的构造函数。

由于Airline已经有一个门户成员,您不应该在AIR中定义另一个门户成员。

你应该把构造函数的参数传递给Airline的构造函数:

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