0

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

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?

2
  • The posted code doesn't show getChargePerHr being implemented in any of the derived classes. Please post the real code and the exact error message(s) you get. Commented Dec 14, 2020 at 18:22
  • Your code has a number of issues and it seems like you have gotten some of the most basic concepts of C/C++ wrong. I suggest you first study a bit through books/tutorials. Commented Dec 14, 2020 at 18:23

1 Answer 1

1

Problem

Here:

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

You're not able to access Car::getChargePerHr() because v is a Vehicle not a Car. Clearly you're attempting to achieve polymorphism since it appears you want derived classes of Vehicle to perform the same actions when they leave parking.

Solution

  1. Declare Vehicle::getChargePerHr() as pure virtual (or virtual if you want a default implementation)
  2. Provide implementations of getChargePerHr() in your derived classes
  3. Implement Vehicle::leaveParking() using just the methods you've defined in Vehicle

At runtime, the virtual table will resolve overrides and call the correct derived implementation.

Other Issues

  1. You are inheriting from Vehicle without declaring its destructor virtual. This means if any child classes need to perform clean-up, their destructors won't be called.

  2. You're missing a semicolon after the Bike class declaration.

  3. If every Vehicle does the same thing when leaving parking, it doesn't make sense to have leaveParking() be virtual—you make a member function virtual if you want it to be able to be overridden by child classes.

  4. Vehicle::leaveParking() should probably not be taking another Vehicle as a parameter. The function acts on the vehicle itself, not on a different one.

  5. If your constructor is empty, it's better to leave it out of the class declaration since it can confuse others who might read your code.

And many more issues. I suggest you take aalimian's advice to read up on C/C++. Your code shows many misunderstandings.

Code

Putting everything together, here's an example:

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

You can see this in action here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.