C++ Operator Overload the / operator for the Pool class to divide

Description

C++ Operator Overload the / operator for the Pool class to divide

#include <iostream>
#include <iostream>
#include <iomanip>

class  Pool/*from w w  w.j av a2 s. com*/
{
private:
  double length {1.0};
  double width {1.0};
  double height {1.0};

public:
  // Constructors
  Pool(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {}

  Pool() {}                                                             // No-arg constructor

  Pool(const Pool& pool)                                                  // Copy constructor
    : length {pool.length}, width {pool.width}, height {pool.height} {}

  double volume() const                                                // Function to calculate the volume
  {
    return length*width*height;
  }

  // Accessors
  double getLength() const { return length; }
  double getWidth() const { return width; }
  double getHeight() const { return height; }

  bool operator<(const Pool& aPool) const;                               // Less-than operator
  bool operator<(double aValue) const;                                 // Compare Pool volume < double value
  Pool operator+(const Pool& aPool) const;                                // Function to add two Pool objects
  Pool operator*(int n) const;                                       // Post-multiply an object by an integer
  int operator/(const Pool& aPool) const;                             // Function to divide one Pool object by another

  friend std::ostream& operator<<(std::ostream& stream, const Pool& pool);
};

inline int Pool::operator/(const Pool& pool) const{
  // Lambda expression to calculate division for a given orientation of pool
  auto trial = [this](double L, double W, double H){
           return static_cast<int>(this->length / L) * static_cast<int>(this->width / W) * static_cast<int>(this->height / H);
  };

  int result {trial(pool.length, pool.width, pool.height)};                    // pool in LxWxH orientation

  if (!result)
     return 0;

  int temp {trial(pool.length, pool.height, pool.width)};                      // pool in LxHxW orientation
  if (temp > result)
     result = temp;

  temp = trial(pool.width, pool.height, pool.length);                             // pool in WxHxL orientation
  if (temp > result)
     result = temp;

  temp = trial(pool.width, pool.length, pool.height);                             // pool in WxLxH orientation
  if (temp > result)
     result = temp;

  temp = trial(pool.height, pool.length, pool.width);                             // pool in HxLxW orientation
  if (temp > result)
     result = temp;

  temp = trial(pool.height, pool.width, pool.length);                             // pool in HxWxL orientation
  if (temp > result)
     result = temp;
  return result;
}

// Pre-multiply an object by an integer
inline Pool operator*(int n, Pool& pool)
{
  return Pool {pool.getLength(), pool.getWidth(), n*pool.getHeight()};
}

// Post-multiply an object by an integer
inline Pool Pool::operator*(int n) const
{
  return Pool {length, width, n*height};
}

// Less-than comparison for Pool objects
inline bool Pool::operator<(const Pool& aPool) const
{
  return volume() < aPool.volume();
}

// Compare the volume of a Pool object with a constant
inline bool Pool::operator<(double aValue) const
{
  return volume() < aValue;
}

// Function comparing a constant with volume of a Pool object
inline bool operator<(double aValue, const Pool& aPool)
{
  return aValue < aPool.volume();
}

// Operator function to add two Pool objects
inline Pool Pool::operator+(const Pool& aPool) const
{
  // New object has larger length and width, and sum of heights
  return Pool {length > aPool.length ? length : aPool.length,
    width > aPool.width ? width : aPool.width,
    height + aPool.height};
}

inline std::ostream& operator<<(std::ostream& stream, const Pool& pool)
{
  stream << " Pool(" << std::setw(2) << pool.length << ","
    << std::setw(2) << pool.width << ","
    << std::setw(2) << pool.height << ")";
  return stream;
}

int main()
{
  Pool pool1 {12, 5, 12};
  Pool pool2 {30, 40, 50};
  Pool pool3 {3, 4, 7};
  std::cout << "pool1 is " << pool1 << std::endl;
  std::cout << "pool2 is " << pool2 << std::endl;
  std::cout << "pool3 is " << pool3 << std::endl;
  std::cout << "pool1/pool2 is " << pool1 / pool2 << std::endl;
  std::cout << "pool2/pool3 is " << pool2 / pool3 << std::endl;
  std::cout << "pool1/pool3 is " << pool1 / pool3 << std::endl;
  std::cout << "(pool1+2*pool2)/pool3 is " << (pool1 + 2 * pool2) / pool3 << std::endl;
}



PreviousNext

Related