【发布时间】:2021-03-25 00:45:11
【问题描述】:
我正在尝试设计一个停车系统(低级设计)
有些类的行为是这样的。
class Vehicle
{
public:
int entryTime;
int exitTime;
virtual void leaveParking(Vehicle*);
virtual int getChargePerHr();
//virtual void getChargePerHr() = 0;
Vehicle() {}
};
class Car : public Vehicle
{
private :
int chargePerHr = 30;
public:
void leaveParking(Vehicle*);
int getChargePerHr();
Car(){}
};
class Bike : public Vehicle
{
private :
int chargePerHr = 10;
public:
void leaveParking(Vehicle*);
int getChargePerHr();
Bike(){}
}
void Vehicle ::leaveParking(Vehicle* v)
{
int pay = v-> // Here expecting Car class member function getChargePerHr() should come
//so that I can access private member chargePerHr of car class.
// But I am not able to access the Car class member function here.
}
int main()
{
Car c1; // assume Car c1 has already parked.
Vehicle v;
Vehicle* vptr = new Vehicle();
vptr = new Car();
c1.leaveParking(vptr); // Car c1 wants to leave the parking place
}
我想使用基类 Vehicle 成员函数访问 Car 类的 getChargePerHr()。
我尝试了纯虚函数,但仍然无法实现。
谁能帮帮我?
【问题讨论】:
-
发布的代码未显示
getChargePerHr在任何派生类中实现。请发布您收到的真实代码和确切的错误消息。 -
你的代码有很多问题,你似乎把一些最基本的 C/C++ 概念弄错了。我建议你先通过书籍/教程学习一下。
标签: c++ class oop c++11 inheritance