多态性C ++ Garage类

时间:2014-04-13 04:34:44

标签: c++

#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类型的指针将汽车添加到车库,&#39; p&#39; ? Motor Class是基类,派生类是Loader和Car Classes ......我们如何使用#184行中的Motor Class指针初始化它们。(VOID DISPLAY()函数;案例1)

1 个答案:

答案 0 :(得分:1)

问:在#184行中,我们如何使用Motor类型的指针将汽车添加到车库,&#39; p&#39; ?

A:发布的代码中的第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]未初始化为{{ 1}}。

  • i >= index

    没有意义。您尚未将Motor *p[3]; Garage *g; display(p[3], g); 初始化为任何合理的内容。 p是未初始化的值,如果您在下游使用它,则会出现问题。与p[3]相同。它尚未初始化,但已用于g

  • 函数display具有输入参数display,但参数不在任何地方使用。在目前的版本中,如果你传递一个有效的Motor* p似乎没问题。

我的建议:

  1. Garage*更改为:

    display
  2. 新版 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