0

我试图让一个班级与另一个班级合作。它应该减少另一个类的成员。

我的第一堂课是

   class Bike
{
   private:
   int miles;
   Speedometer speedom;
   static int fuelCount;

   public:

   Bike();
   Bike(int, Speedometer*);       //Problem occurs here

   ~Bike();

   int getMiles();
   int getFuelCount();

   void incrementMiles();
};

    int Bike::fuelCount = 0;

   Bike::Bike()
   {
miles = 0;
fuelCount++;
   }

   Bike::Bike(int m, Speedometer * spm)  //This is where I am having problems 
   {
     miles = m;
     speedom = &spm;
   }



   Bike::~Bike()
   {
       cout << "The Bike's destructor is running." << endl;
       fuelCount--;
   }

   int Bike::getMiles()
   {
       return miles;
   }

   int Bike::getFuelCount()
   {
            return fuelCount;
   }

   void Bike::incrementMiles()
   {
            miles++;
            if (miles == 999999)
            miles = 0;
   }

应该包含在第一个类中的另一个类是:

   Class Speedometer
   {
     private:
          int fuel;


     public:
           Speedometer();
           Speedometer(int);

           ~Speedometer();

           int getFuel();

           void incrementFuel();
           void decrementFuel();
   };


   Speedometer::Speedometer()
   {
      fuel = 0;
   }

   Speedometer::Speedometer(int f)
   {
     fuel = f;
   }


   int Speedometer::getFuel()
   {
   return fuel;
   }


   void Speedometer::incrementFuel()
   {
      if (fuel <= 15)
          fuel++;
   }

   void Speedometer::decrementFuel()
   {
      if (fuel > 0)
      fuel--;
   }

他们应该一起工作。自行车是能够与速度计对象一起工作的。每行驶 24 英里,它应将车速表的当前燃料量减少一加仑。这应该是聚合关系而不是组合。

请帮助我了解如何建立这种关系以及应该如何称呼它。先感谢您。

顺便说一句,这是我的主要功能 - 我有所有正确的#includes 我只是没有在这里列出它们

   int main(int argc, char *argv[])
   {
     Speedometer a(999970, spd);

   for(int count = 0; count <=24; count++)
       a.decrementMiles();

   while (a.getFuel() > 0)
   {
          a.incrementMiles();

          cout<< "Miles:" << a.getMiles() << endl;
          cout<< "Fuel:" << a.getFuel() << endl;
    }

   return 0;
   }
4

3 回答 3

0
 #include <iostream>

using namespace std;


class Bike 
{
   private:
   int miles;   
   static int fuelCount;
                        // Speedometer speedom;  

   public:


   Bike();
   Bike(int);  //  Speedometer *);  check comment on line 82

   ~Bike();


   int getMiles();
   int getFuelCount();

   void incrementMiles();
};

    int Bike::fuelCount = 0;

   Bike::Bike()
   {
    miles = 0;
    fuelCount++;
   }

   Bike::Bike(int m)//Speedometer (*spm) I don't see the purpose of this in the current state of the program, I may not be seing the whole picture
   {
     miles = m;
                    /* speedom = spm; remember, there must be a parent and a child class, at the current state you'r trying
                    to call a child from parent, the child class has not been defined, so i switched them and now Bike is a chiled. */
   }



   Bike::~Bike()
   {
       cout << "The Bike's destructor is running." << endl;
       fuelCount--;
   }

   int Bike::getMiles()
   {
       return miles;
   }

   int Bike::getFuelCount()
   {
        return fuelCount;
   }

   void Bike::incrementMiles()
   {
        miles++;
        if (miles == 999)
        miles = 0;
   }


   class Speedometer
   {
     private:
      int fuel;


     public:
       Speedometer();
       Speedometer(int f);            
       int getFuel();
       Bike theBike;  // This is what you needed in order to make incrementMiles to work.
       void incrementFuel();
       void decrementFuel();    

   };


   Speedometer::Speedometer()
   {
      fuel = 0;
  }

   Speedometer::Speedometer(int f)
   {
 fuel = f;
   }


   int Speedometer::getFuel()
   {
   return fuel;
   }


   void Speedometer::incrementFuel()
   {
  if (fuel <= 15)
      fuel++;
   }

   void Speedometer::decrementFuel()
   {
  if (fuel > 0)
  fuel--;
   }


int main(int argc, char *argv[])
   {
 Speedometer a(999); //You never declared this, did you mean spm???

   for(int count = 0; count <=24; count++)
   a.theBike.incrementMiles();

   while (a.getFuel() > 0)
    {
      a.theBike.incrementMiles();

      cout<< "Miles:" << a.theBike.getMiles() << endl;
      cout<< "Fuel:" << a.getFuel() << endl;

}

   cin.get();

   return 0;
   }                                             //There is no break declared (that i can see at least) so the program runs an infinite loop 
                                            // Don't want to add too many things to it, I don't know what your plan is.
                                           // Hoping to have made it clearer.
于 2013-04-23T23:23:06.620 回答
0

你在这里有很多问题。

首先,在您的 中main(),您Speedometer使用尚未实现的构造函数构造对象。您定义的唯一构造函数是默认构造函数和Speedometer(int). 然后你调用Speedometer(int, ???),???因为spd你没有spd在你提供的代码中声明任何地方,所以我们不知道它是什么。

真的不可能说出你的代码在当前状态下有什么问题。

于 2013-04-23T17:23:37.907 回答
0

如所写,您已作曲;Speedometer是的一部分, Bike因为它是一个领域。要使其成为聚合,请Bike保留指向Speedometer. 请注意,因此,您可能需要Bike创建或获取一个初始Speedometer值(可能是NULL开始,或在构造函数中传递一个),并且您可能希望添加访问器方法以Bike添加/删除/更改Speedometer.

[编辑]Bike可能还需要知道如何Speedometer正确处理以避免泄漏。

[编辑 2] 同样正如@cjm571 指出的那样,您的main函数是直接在 "disembodied" 上创建和操作的Speedometer。不应该在a上Bike吗?:)

于 2013-04-23T17:26:20.837 回答