如何在派生class中实现虚函数?
How to implement a virtual function in derived class?
考虑以下 UML:
当我尝试在Single.cpp
中实现虚函数rate()
时,出现错误:无法分配抽象类型的对象'Single'
预定* b = new Single();
addBooking()
函数应该创建一个 Single
类型的指针 Booking
,添加指向 QList
的指针(BookingList
派生自 QList<Booking*>
) 和 return Booking
指针。
当我注释掉虚函数时,一切正常。为什么会发生这种情况,我该如何解决?
下面是我的程序的最小版本:
#ifndef BOOKING_H
#define BOOKING_H
class Booking
{
public:
Booking();
double SINGLE_PPPN = 200.00;
virtual double rate() = 0;
};
#endif // BOOKING_H
#include "booking.h"
Booking::Booking()
{
}
#ifndef BOOKINGLIST_H
#define BOOKINGLIST_H
#include <QList>
#include <iostream>
#include "booking.h"
#include "single.h"
using namespace std;
class BookingList : public QList<Booking*>
{
public:
BookingList();
Booking* addBooking();
void deleteAll();
};
#endif // BOOKINGLIST_H
#include "bookinglist.h"
BookingList::BookingList()
{
}
Booking* BookingList::addBooking()
{
Booking* b = new Single();
this->append(b);
cout << "Total bookings: " << this->size() << "\n\n" <<endl;
return b;
}
void BookingList::deleteAll()
{
for (int i = 0; i < this->count(); i++)
{
cout << "Deleting Item At: " << i << endl;
delete this->at(i);
}
}
#ifndef SINGLE_H
#define SINGLE_H
#include "booking.h"
class Single : public Booking
{
public:
Single();
};
#endif // SINGLE_H
#include "single.h"
Single::Single()
{
}
double Single::rate()
{
return Booking::SINGLE_PPPN;
}
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
您需要在 class 定义中声明您的方法 override
class Single : public Booking
{
public:
Single();
double rate() override; // this line
};
然后你就可以在你的cpp文件中实际实现这个方法了
double Single::rate()
{
return Booking::SINGLE_PPPN;
}
考虑以下 UML:
当我尝试在Single.cpp
中实现虚函数rate()
时,出现错误:无法分配抽象类型的对象'Single'
预定* b = new Single();
addBooking()
函数应该创建一个 Single
类型的指针 Booking
,添加指向 QList
的指针(BookingList
派生自 QList<Booking*>
) 和 return Booking
指针。
当我注释掉虚函数时,一切正常。为什么会发生这种情况,我该如何解决?
下面是我的程序的最小版本:
#ifndef BOOKING_H
#define BOOKING_H
class Booking
{
public:
Booking();
double SINGLE_PPPN = 200.00;
virtual double rate() = 0;
};
#endif // BOOKING_H
#include "booking.h"
Booking::Booking()
{
}
#ifndef BOOKINGLIST_H
#define BOOKINGLIST_H
#include <QList>
#include <iostream>
#include "booking.h"
#include "single.h"
using namespace std;
class BookingList : public QList<Booking*>
{
public:
BookingList();
Booking* addBooking();
void deleteAll();
};
#endif // BOOKINGLIST_H
#include "bookinglist.h"
BookingList::BookingList()
{
}
Booking* BookingList::addBooking()
{
Booking* b = new Single();
this->append(b);
cout << "Total bookings: " << this->size() << "\n\n" <<endl;
return b;
}
void BookingList::deleteAll()
{
for (int i = 0; i < this->count(); i++)
{
cout << "Deleting Item At: " << i << endl;
delete this->at(i);
}
}
#ifndef SINGLE_H
#define SINGLE_H
#include "booking.h"
class Single : public Booking
{
public:
Single();
};
#endif // SINGLE_H
#include "single.h"
Single::Single()
{
}
double Single::rate()
{
return Booking::SINGLE_PPPN;
}
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
您需要在 class 定义中声明您的方法 override
class Single : public Booking
{
public:
Single();
double rate() override; // this line
};
然后你就可以在你的cpp文件中实际实现这个方法了
double Single::rate()
{
return Booking::SINGLE_PPPN;
}