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


在184号线中,我们如何使用Motor类'p'类型的Pointer将汽车添加到车库?
Motor Class是基本类,派生类是Loader和Car类...我们如何使用第184行中的Motor Class指针来初始化它们。(VOID DISPLAY()函数;情况1)

最佳答案

问:在184号线中,我们如何使用Motor类'p'类型的Pointer将汽车添加到车库?

答:已发布代码中的行184和185为:

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


也许您的意思是第239行,即:

Motor *p[3];


假设您的意思是第239行,p是3个指向Motor的指针的数组。您可以通过以下方法简单地添加Car

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


但是,我在您的代码中看到以下问题,应解决该问题才能使程序运行:


函数Garage::FindGarage::Remove需要在index-1而不是cap-1处停止迭代,因为未为m[i]初始化i >= index
线

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


没道理您尚未将p初始化为任何明智的方法。 p[3]是一个未初始化的值,如果在下游使用它,则会出现问题。 g也是如此。它尚未初始化,但已在display中使用。
函数display具有输入参数Motor* p,但是该参数未在任何地方使用。在当前版本中,如果传递有效的Garage*似乎可以。


我的建议:


display更改为:

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

有了新版本的display,不需要太多的main内容。可以简化为:

  int main()
  {
      cout<<"Welcome to Garage"<<endl;
      Garage g;
      display(g);
      return 0;
  }

关于c++ - 多态C++车库类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23039147/

10-13 05:02