lundi 14 décembre 2020

How to access derived class member function using Base class function?

I am trying to design a parking system (Low-level Design )
Some classes behave like this.

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.
}

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
    
}               
           

I want to access getChargePerHr() of Car class using Base class Vehicle member function.
I tried with pure virtual function but still could not make it.
Could anyone help me?

Aucun commentaire:

Enregistrer un commentaire