Parking system implementation using Classes Test Question

I just gave my online test and this question was given to us.

I used an function in which an array returns true when it is empty and returns false when it's full.

You are supposed to make a software that manages Parking lots. Vehicles get parked in a Parking lot. Each vehicle has a registration number, manufacturer and model. Vehicle can either be a car or a bike. In addition to above attributes Car also has number Of doors.

Following is part Of the code used in the Software.
Please go through it and answer the questions that follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "Vehicle. h"
class ParkingLot
public:
ParkingLot( int size);
//returns true if parking slot was available, false otherwise

bool park(Vehicle*);
Vehicle** getParkedVehicles();
int getNum_of_slots();
private:
int numof_slots;
Vehicle** parkedvehicle;
}

int main()
{
ParkingLot new ParkingLot(30);
car *corolla1 = new Car( "LOX 213", "Toyota","corolla", 4);
bike* honda1 = new Bike("LED 2179", "Honda", "CD70");
fastParking->park(corolla1);
fastParking->park(honda1);
int num=fastParking->getNum_of_slots();
Vehicle ** vehicles= fastParking->getParkedVehicles();
for(int i=0; i<num; i++)
{
/*the display() method displays registration number, manufacturer and
model in case of bike and in case of car it displays all Of above as well
as number Of doors*/
if(vehicles[i]!=0)
cout<<"Parking slot"<<i+1<<"has"<<vehicles[i]->display();
else
cout<<"Parking slot"<<i+1<<"is empty"<<endl;
}
return 0;
}


Q:Implement the park method Of the ParkingLot class.

Last edited on
We're not here to do your homework.
http://www.catb.org/esr/faqs/smart-questions.html#homework

Format your code properly if you want people to read it.
https://www.cplusplus.com/articles/jEywvCM9/
Honey attracts better than vinegar.
Bro
I submit it through a mobile.
My pc is dead and due to the lock down i can't program
Perhaps you meant
ParkingLot* fastParking = new ParkingLot(30);
You also have memory leaks because you never delete anything you new.

It would be better to just do:
1
2
3
ParkingLot fastParking;
// ...
fastParking.park(corolla);
Last edited on
I can't figure out what's causing the error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

#include <iostream>
#include <string>
using namespace std;


class Vehicle
{
public:
  string RN;
  string manufacturer;
  string model;

  Vehicle()
  {
    RN="";
    manufacturer="";
    model="";
  }
  void setVehicle(string a,string b, string c)
  {
    RN=a;
    manufacturer=b;
    model=c;
  }
  virtual void display();

};
class ParkingLot
{
public:
ParkingLot( int size)
{
  numof_slots=size;
}

// returns true if parking slot was available, false otherwise
bool park(Vehicle*)
  {
  int count = 0;
  for  (int i = 0; i < numof_slots; i++)
  {
    if (parkedVehicles[i] == 0)
    {count++;
    return true;}
    
    else
    {return false;}
  }
  if (count > 0)
  {
    return true;
  }
   }

Vehicle **getParkedVehicles()
{
  return parkedVehicles;
}
int getNum_of_slots()
{return numof_slots;}
private:
int numof_slots;
Vehicle** parkedVehicles;
};

class Car:public Vehicle
{
private:
  int NOD;
public:
  Car():Vehicle()
  {NOD=0;
  }
  Car(string a,string b,string c,int d)
  {
    setVehicle(a,b,c);
    NOD=d;
  }
 virtual void::display()
  {
    cout<<"Bike :"<<endl;
    cout<<"Registeration number = "<<RN<<endl;
    cout<<"Manufacturer = "<<manufacturer<<endl;
    cout<<"model = "<<model<<endl;
    cout<<"Number of doors = "<<NOD<<endl;
  }
};

class Bike:public Vehicle
{
private:
       
  
public:
  Bike():Vehicle()
  {
    
  }
  
    
  
  Bike(string a,string b,string c)
  {
    setBike(a,b,c);
  }
  void setBike(string a, string b, string c)
  {
    setVehicle(a,b,c);
  }
  virtual void::display()
  {
    cout<<"Bike :"<<endl;
    cout<<"Registeration number = "<<RN<<endl;
    cout<<"Manufacturer = "<<manufacturer<<endl;
    cout<<"model = "<<model<<endl;
  }

};
int main()
{
ParkingLot *fastParking= new ParkingLot(30);
Car *corolla1= new Car("LOX 213", "Toyota", "Corolla", 4);
Bike* honda1= new Bike("LED 2179", "Honda", "CD70");

fastParking->park(corolla1);
fastParking->park(honda1);

int num=fastParking->getNum_of_slots();
Vehicle ** vehicles= fastParking->getParkedVehicles();

for(int i=0; i<num; i++)
{
/*the display() method displays registration number, manufacturer and
model in case of bike and in case of car it displays all of above as well
as number of doors*/
if(vehicles[i]!=0)
{
cout<<"Parking slot "<<i+1<< " has ";
vehicles[i]->display();
}
else
cout<<"Parking slot "<<i+1<< " is empty"<<endl;
}
return 0;
}
What is the text of the error message?

virtual void::display()
This syntax looks wrong. If you're defining the function inline within the class definition, it would just be "virtual void display() { ... }".

Second, your indentation is very sloppy. Proper indentation makes the scope of logic clearer, and makes code easier to read.
Thx guys
Topic archived. No new replies allowed.