使用C++进行聚合

Aggregation using C++

本文关键字:C++ 使用      更新时间:2023-10-16

我正试图使一个类与另一个类协同工作。它应该减少另一个类的成员。

我的头等舱是

   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英里,它应该会使速度计当前的燃油量减少一加仑。这应该是一个聚合关系,而不是组合。

请帮我理解如何建立这种关系,以及应该如何称呼它。提前谢谢。

这是我的主要功能顺便说一句,我有所有的权利#包括我只是没有在这里列出他们

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

这里有很多问题。

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

在当前状态下,很难说代码出了什么问题。

正如所写的,您已经完成了一篇作文;SpeedometerBike部分,因为它是一个字段。若要使其成为聚合,请使Bike包含指向Speedometer指针。请注意,因此,您可能需要Bike来创建或获取初始Speedometer(可以是NULL,也可以在构造函数中传递一个(,并且您可能希望向Bike添加访问器方法,以便添加/删除/更改Speedometer

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

[edit 2]正如@cjm571所指出的,main函数是直接在"无实体"的Speedometer上创建和操作的。它不应该在Bike上吗?:(

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