C++ 多态性C++;车库类 #包括 #包括 使用名称空间std; 类电机 { 受保护的: 字符串品牌、型号、注册号、颜色、类型; 公众: 马达() :品牌(“”)、型号(“”)、注册号(“”)、颜色(“”)、类型(“”) {} 电机(串mk、串md、串rg、串c、串t) :品牌(mk)、型号(md)、注册号(rg)、颜色(c)、类型(t) {} 字符串getRegNo() { 返回注册号; } 虚拟空打印() { cout

C++ 多态性C++;车库类 #包括 #包括 使用名称空间std; 类电机 { 受保护的: 字符串品牌、型号、注册号、颜色、类型; 公众: 马达() :品牌(“”)、型号(“”)、注册号(“”)、颜色(“”)、类型(“”) {} 电机(串mk、串md、串rg、串c、串t) :品牌(mk)、型号(md)、注册号(rg)、颜色(c)、类型(t) {} 字符串getRegNo() { 返回注册号; } 虚拟空打印() { cout,c++,C++,Q:在第184行中,我们如何使用Motor class“p”类型的指针将汽车添加到车库 A:发布代码中的第184行和第185行是: #include <iostream> #include <string> using namespace std; class Motor { protected: string make, model, reg_no, color, type; public: Motor() : make(""), model(""),

Q:在第184行中,我们如何使用Motor class“p”类型的指针将汽车添加到车库

A:发布代码中的第184行和第185行是:

#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;
}
也许你指的是第239行,即:

        /*******************************
        ********************************/
假设您指的是第239行,
p
是一个指向
Motor
的3个指针数组。您可以通过以下方式向其添加
Car

Motor *p[3];
但是,我发现您的代码中存在以下问题,这些问题应该得到修复,程序才能运行:

  • 函数
    Garage::Find
    Garage::Remove
    需要在
    index-1
    停止迭代,而不是在
    cap-1
    停止迭代,因为
    m[i]
    没有为
    i>=index
    初始化

  • 台词

    p[0] = &b; // I see that you have the statement "Car b;" a few lines above.
    
    没有意义。您还没有将
    p
    初始化为任何合理的值。
    p[3]
    是一个未初始化的值,如果您在下游使用它,将有问题。与
    g
    相同。它尚未初始化,但在
    显示中使用

  • 函数
    display
    具有输入参数
    Motor*p
    ,但该参数未在任何地方使用。在其当前版本中,如果您向其传递有效的
    Garage*
    ,则该参数似乎正常

我的建议是:

  • 将显示更改为:

    Motor *p[3];
    Garage *g;
    display(p[3], g);
    
  • 新版本的
    display
    就位后,不需要
    main
    的太多内容。它可以简化为:

      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.
      }
    
    intmain()
    {
    
    你应该有一个虚拟析构函数。@chris,请告诉我虚拟析构函数的用途是什么?你从
    Motor
    派生并以多态方式使用它。虚拟析构函数可以确保派生类也被正确地销毁。你的代码很长。我相信你可以在不删除你的问题的本质。你应该这样做,以免浪费回答你的人的时间去费力地阅读与问题无关的大量代码,也符合你自己的利益,因为很多能够回答你问题的人不会这样浪费时间。你是在要求别人努力回答你的问题我们的问题是,所以你也应该投入一些努力,让他们不必太难做到这一点。
      int main()
      {
          cout<<"Welcome to Garage"<<endl;
          Garage g;
          display(g);
          return 0;
      }