0
#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)

Shaan
  • 25
  • 6
  • You should have a virtual destructor. – chris Apr 13 '14 at 04:36
  • 1
    @chris, please tell what will be the use of virtual destructor? – Shaan Apr 13 '14 at 04:39
  • You derive from `Motor` and use it polymorphically. A virtual destructor makes sure the derived class is destroyed properly as well. See http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – chris Apr 13 '14 at 04:51
  • Your code is very long. I'm sure you could have removed quite a bit of code without removing the essence of your question. YOu should have done so, in order to not waste the time of those answering you to wade through tons of code which is irrelevant to the question, and in your own interest because quite a few of those who may be able to answer your question *won't* waste their time that way. You are asking others to make an effort to answer your question, so you should also invest some effort to make it not unnecessarily hard for them to do so. – celtschk Apr 13 '14 at 08:43

1 Answers1

1

Q: In Line # 184, how can we Add a car to garage using the Pointer of type Motor class, 'p' ?

A: Line # 184 and 185 in the posted code are:

        /*******************************
        ********************************/

Perhaps you meant line # 239, which is:

Motor *p[3];

Assuming you meant line # 239, p is an array of 3 pointers to Motor. You can add a Car to it simply by:

p[0] = &b; // I see that you have the statement "Car b;" a few lines above.

However I see the following problems in your code that ought to be fixed for the program to run:

  • The functions Garage::Find and Garage::Remove need to stop the iteration at index-1, not at cap-1 since m[i] is not initialized for i >= index.

  • The lines

    Motor *p[3];
    Garage *g;
    display(p[3], g);
    

    don't make sense. You have not initialized p to anything sensible. p[3] is an uninitialized value that will be problematic if you use it downstream. Same thing with g. It is not initialized yet is used in display.

  • The function display has the input argument Motor* p, but the argument is not used anywhere. In its current incarnation, it seems to be OK if you pass it a valid Garage*.

My suggestion:

  1. Change display to:

      void display(Garage& g)
      {
         // Work with `g` as an object, not as a pointer to an object.
         // You don't need the `Motor* p` argument since you don't use it
         // at all.
      }
    
  2. With the new version of display in place, don't need much of the contents of main. It can be simplified to:

      int main()
      {
          cout<<"Welcome to Garage"<<endl;
          Garage g;
          display(g);
          return 0;
      }
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270