#include<iostream.h>
#include<conio.h>
#include<string.h>
class flight
{
private :
int flightno;
char source[30],destination[30];
protected :
double fare;
public :
flight()
{
flightno=0;
source[0]='\0';
destination[0]='\0';
fare=0.0 ;
}
flight(int f,char s[],char d[],double fr)
{
flightno=f;
strcpy(source,s);
strcpy(destination,d);
fare=fr;
}
flight(flight &f)
{
flightno=f.flightno;
strcpy(source,f.source);
strcpy(destination,f.destination);
fare=f.fare;
}
virtual void accept()
{
cout<<"\nEnter Fligt no :";
cin>>flightno;
cout<<"\nEnter source :";
cin.getline(source,30);
cout<<"\nEnter destination :";
cin.getline(destination,30);
cout<<"\nEnter fare :";
cin>>fare;
}
virtual void display()
{
cout<<"\nFlight number :"<<flightno;
cout<<"\nsource :"<<source;
cout<<"\nDestination :"<<destination;
cout<<"\nflight fare :"<<fare;
}
virtual double computefare()=0;
};
class domestic : public flight
{
private :
int noc,noa;
double totalfare,discount;
public :
domestic()
{
noc=0;
noa=0;
totalfare=0.0;
discount=0.0;
}
domestic(int f,char s[],char d[],double fr,int nc,int na):flight(f,s,d,fr)
{
noc=nc;
noa=na;
totalfare=0.0;
discount=0.0;
}
domestic(domestic &d):flight(d)
{
noc=d.noc;
noa=d.noa;
totalfare=d.totalfare;
discount=d.discount;
}
void accept()
{
flight::accept();
cout<<"\nEnter no. of adults :";
cin>>noa;
cout<<"\nEnter no. of children :";
cin>>noc;
}
void display()
{
flight::display();
cout<<"\n no. of adults :"<<noa;
cout<<"\n no. of children :"<<noc;
cout<<"\n total fare :"<<totalfare;
cout<<"\n discount :"<<discount;
if(discount!=0)
cout<<"After disc: "<<(totalfare-discount);
}
double computefare()
{
totalfare=noc*flight::fare*0.5+noa*flight::fare;
if(totalfare>40000)
discount=0.02*totalfare;
return totalfare-discount;
}
};
class internatinal : public flight
{
private :
int nop;
double totalfare,tax;
public :
internatinal()
{
nop=0;
totalfare=0.0;
tax=0.0;
}
internatinal(int f,char s[],char d[],double fr,int np):flight(f,s,d,fr)
{
nop=np;
totalfare=0.0;
tax=0.0;
}
internatinal(internatinal &i):flight(i)
{
nop=i.nop;
totalfare=i.totalfare;
tax=i.tax;
}
void accept()
{
flight::accept();
cout<<"\nEnter no. of passenger :";
cin>>nop;
}
void display()
{
flight::display();
cout<<"\n no. of passenger :"<<nop;
cout<<"\n total fare :"<<totalfare;
cout<<"\n tax :"<<tax;
cout<<"After tax: "<<(totalfare+tax);
}
double computefare()
{
totalfare=nop*flight::fare;
tax=0.30*totalfare;
return totalfare+tax;
}
};
int main()
{
int i,n,ch;
double total=0;
cout<<"\n Enter no of transaction :" ;
cin>>n;
flight *f=new flight[n];
for(i=0;i<n;i++)
{
cout<<"\n Enter 1 : domestic \n 2. internatinal :";
cin>>ch;
f[i]=ch==1?new domestic():new internatinal();
f[i]->accept();
total+=f[i]->computefare();
}
cout<<"\n totaL fare :"<<total;
getch();
return 0;
}
Я использую Turdo C ++ Complier.Мой код не в состоянии скомпилировать ошибку лежит в main () в "flight * f = new flight [n];"line (ошибка: 1.) Невозможно создать экземпляр абстрактного класса 'flight' в функции main () 2.) Класс 'flight' является абстрактным из-за 'flight :: computefare () = 0' в функции main ()).Насколько я помню, мы не можем создавать объекты абстрактного класса, но мы можем создавать его указатели.И здесь я создаю только указатели, но все еще получаю эту ошибку.