【问题标题】:How to access derived class member function using Base class function?如何使用基类函数访问派生类成员函数?
【发布时间】: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


【解决方案1】:

问题

这里:

void Vehicle::leaveParking(Vehicle* v)
{
    ...
}

您无法访问Car::getChargePerHr(),因为vVehicle 而不是Car。 显然,您正在尝试实现多态性,因为您似乎希望 Vehicle 的派生类在它们离开停车场时执行相同的操作。

解决方案

  1. Vehicle::getChargePerHr() 声明为纯虚拟(或者如果您想要默认实现,则为虚拟)
  2. 在派生类中提供getChargePerHr() 的实现
  3. 仅使用您在Vehicle 中定义的方法实现Vehicle::leaveParking()

在运行时,虚拟表将解析覆盖并调用正确的派生实现。

其他问题

  1. 您从Vehicle 继承而没有声明其析构函数为虚拟的。这意味着如果任何子类需要执行清理,它们的析构函数将不会被调用。

  2. Bike 类声明后缺少分号。

  3. 如果每个Vehicle 在离开停车场时都做同样的事情,那么让leaveParking() 成为虚拟是没有意义的——如果你希望它能够被子类覆盖,你可以将成员函数设为虚拟。

  4. Vehicle::leaveParking() 可能不应该将另一个车辆作为参数。该函数作用于车辆本身,而不是其他车辆。

  5. 如果您的构造函数为空,最好将其排除在类声明之外,因为它可能会混淆可能阅读您代码的其他人。

还有更多问题。我建议你接受 aalimian 的建议来阅读 C/C++。您的代码显示了许多误解。

代码

将所有内容放在一起,这是一个示例:

class Vehicle
{
public:
    int entryTime;
    int exitTime;

    virtual ~Vehicle() = default;

    void leaveParking();
    virtual int getChargePerHr() = 0;
};

void Vehicle::leaveParking()
{
    // This will call a derived class's implementation
    int pay = getChargePerHr();
    // Do more vehicle stuff
}

class Car : public Vehicle
{
private:
    int chargePerHr = 30;

public:
    int getChargePerHr() override;
};

int Car::getChargePerHr()
{
    return chargePerHr;
}

class Bike : public Vehicle
{
private:
    int chargePerHr = 10;

public:
    int getChargePerHr() override;
};

int Bike::getChargePerHr()
{
    return chargePerHr;
}

你可以在here看到这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多