#include <iostream>
#include <string>
using namespace std;
class Motor
{
protected:
string make, model, reg_no, color, type;
public:
Motor()
: make(""), model(""), reg_no(""), color(""), type("")
{}
Motor(string mk, string md, string rg, string c, string t)
: make(mk), model(md), reg_no(rg), color(c), type(t)
{}
string getRegNo()
{
return reg_no;
}
virtual void print()
{
cout<< "Make: " << make<<endl
<<"Model: "<<model<<endl
<<"Reg Num: "<<reg_no<<endl
<<"Color: "<<color<<endl
<<"Type: "<<type<<endl;
}
~Motor()
{}
};
class Loader : public Motor
{
private:
int load_cap;
public:
Loader()
: load_cap(0)
{}
Loader(string mk, string md, string rg, string c, string t, int lc)
: Motor(mk, md, rg, c, t) , load_cap(lc)
{}
void print()
{
cout << "Loading Capacity: "<<endl;
}
~Loader()
{}
};
class Car : public Motor
{
private:
int seat_cap;
public:
Car()
: seat_cap(0)
{}
Car(string mk, string md, string rg, string c, string t, int sc)
: Motor(mk, md, rg, c, t) , seat_cap(sc)
{}
void print()
{
cout << "Seating Capacity: "<<seat_cap <<endl;
}
~Car()
{}
};
class Garage
{
private:
string name;
int index;
int cap;
Motor **m;
public:
Garage()
: name("") , index(0) , cap(10)
{
m = new Motor *[10];
}
Garage(string n, int c)
: name(n) , index(0) , cap(c)
{
m = new Motor *[cap];
}
bool IsEmpty()
{
if (index <= cap)
{
return true;
cout << "Empty places: " << cap - index -1 <<endl;
}
return false;
}
bool IsFull()
{
if (index >= cap-1)
{
return true;
cout << "Empty places: " << cap - index -1 <<endl;
}
return false;
}
void Push(Motor *p)
{
m[index] = p;
index++;
}
bool Find(string reg)
{
for (int i=0; i<cap-1; i++)
{
if (m[i]->getRegNo() == reg)
return true;
}
return false;
}
void Remove(string reg)
{
int c;
for (int i=0; i<cap-1; i++)
{
if (m[i]->getRegNo() == reg)
c = i;
break;
}
m[c] = m[index];
index--;
}
~Garage()
{
delete [] *m;
}
void print()
{
for (int i=0; i<index; i++)
{
m[i]->print();
}
}
};
void display(Motor *p, Garage *g)
{
int x;
string reg;
cout<<"1. Add motor to Garage"<<endl
<<"2. Remove motor from Garage"<<endl
<<"3. Display parked Motors"<<endl
<<"4. Find Motor"<<endl
<<"5. Check if Garage is Full"<<endl
<<"0. Exit"<<endl;
cin >> x;
switch(x)
{
case 1:
if (g->IsEmpty())
{
cout<<"Enter make, model, reg_no, color, type"<<endl;
string mk, md, rg, co, ty, lc, sc;
/*******************************
********************************/
}
else
{
cout <<"Sorry.. No space available"<<endl;
}
break;
case 2:
cout <<"Input registration number of car"<<endl;
cin >> reg;
g->Remove(reg);
break;
case 3:
g->print();
return display(p,g);
break;
case 4:
cout <<"Input registration number of car"<<endl;
cin >> reg;
if(g->Find(reg))
{
cout <<"Yes.. it is parked"<<endl;
}
else
{
cout <<"No such car parked inside."<<endl;
}
return display(p,g);
break;
case 5:
if(g->IsFull())
{
cout <<"Capacity Full !"<<endl;
}
else
{
cout <<"Place is Available .."<<endl;
}
return display(p,g);
break;
case 0:
break;
default:
return display(p,g);
}
}
int main()
{
cout<<"Welcome to Garage"<<endl;
Motor a;
Car b;
Loader c;
Garage d;
Motor *p[3];
Garage *g;
display(p[3], g);
delete [] *p;
return 0;
}
In Line # 184, how can we Add a car to garage using the Pointer of type Motor class, 'p' ? Motor Class is the base class and the derived classes are the Loader and Car Classes... How can we initialize both of them using the Motor Class pointer in line # 184.(VOID DISPLAY() Function; case 1)