Implementing the * operator for the Pool class to pre-multiply by an integer - C++ Class

C++ examples for Class:Operator Overload

Description

Implementing the * operator for the Pool class to pre-multiply by an integer

Demo Code

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

class Pool//from  w ww . j  a v a  2  s.  c  om
{
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

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

// 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 pool {2, 3, 4};
  std::cout << "Pool is " << pool << std::endl;
  int n {3};
  Pool newPool = n*pool;
  std::cout << "After pre-multiplying by " << n << " pool is " << newPool << std::endl;
}

Result


Related Tutorials