如何编写派生类构造函数的代码?
How should I write the code for the derived class constructor?
我有两个类。传送门和航空公司。两者分别是虚拟的和抽象的。代码编写得很粗略。很抱歉。我已经创建了派生类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)
{
}
相关文章:
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 是否可以避免在以下代码中复制/移动构造函数的需要?
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 为什么我的代码中没有调用move构造函数
- 基于构造函数的存在禁用代码
- 如何从构造函数返回错误代码?
- 为什么在我的代码中调用复制构造函数而不是移动构造函数?
- 如何基于构造函数参数模板化类成员函数的代码
- C++,处理多个构造函数重载和冗余代码
- 为什么在使用转换构造函数编译代码时需要 const 复制构造函数?
- 如何在C ++代码中搜索所有构造函数?
- C++具有移动和复制构造函数的类中的代码重复
- C++参数化构造函数使代码在传递大输入时停止工作
- 为什么没有参数的构造函数似乎是此代码的问题
- 该构造函数代码如何将指针作为参数
- 为什么此C++代码中的构造函数模棱两可以及如何修复它
- Python 错误:在 SWIG 生成的C++模板化代码模块中没有定义构造函数
- 为什么在下面的代码中调用复制构造函数两次
- 编译器将私人构造函数作为程序代码中的候选人提供
- 如何避免样板代码构造函数