c++ - 无法在函数 main() 中创建抽象类的指针

标签 c++ pointers abstract

  #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++ 编译器。我的代码无法编译,错误在于“flight *f=new flight[n];”中的 main()行(错误是:1。)无法创建抽象实例 函数 main() 中的“飞行”类 2.) 类“flight”是抽象的,因为函数 main() 中的“flight::computefare() = 0”)。 据我所知,我们不能创建抽象类的对象,但我们可以创建它的指针。在这里,我只创建了指针,但仍然出现此错误。

最佳答案

你正在创建一个指向飞行的指针,但你也在尝试创建 n飞行对象:您正在创建的数组是飞行数组,而不是飞行指针数组。

所以这行不通。

例如,您可以使用 std::vector<flight*> (也可能在容器内使用智能指针)。 (或飞行指针数组。)

对于您的作业,不要试图在一行中把所有内容都揉成一团,这对您没有任何帮助。写的再清楚点就可以了:

if (ch == 1)
    f[i] = new domestic();
else
    f[i] = new internatinal();

(另一种选择是使用强制转换,但这会使该行的可读性更差。)

关于c++ - 无法在函数 main() 中创建抽象类的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9962388/

相关文章:

c++ - gl_FragCoord 与 ivec2 转换

c++ - 如何声明成员的访问者?

Delphi7,传递对象的接口(interface) - 释放对象时导致无效指针操作

java - java中父类和子类的接口(interface)实现

rust - 如何解决 Rust 中缺少抽象类的问题?

c++ - 带/不带虚拟的纯虚拟方法的实现?

c++ - 使用 HTTP 方法 POST 或 PUT 上传数据

c++ - 使用 "addinclude unistd"修改 Makefile

c++ - 在堆中创建和使用数组

c - 从字符串中删除多余的空格(就地)