C++ examples for Class:Operator Overload
Using the addition operator for Pool objects with this pointer
#include <iostream> #include <vector> #include <cstdlib> // For random number generator #include <ctime> // For time function #include <iostream> #include <iomanip> class Pool/*from w w w .ja v a 2s.co m*/ { 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 void listPool(); // Output the Pool }; // 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}; } // Output the Pool inline void Pool::listPool() { std::cout << " Pool(" << std::setw(2) << length << "," << std::setw(2) << width << "," << std::setw(2) << height << ")"; } // Stream output for Pool objects std::ostream& operator<<(std::ostream& stream, const Pool& pool) { stream << " Pool(" << std::setw(2) << pool.getLength() << "," << std::setw(2) << pool.getWidth() << "," << std::setw(2) << pool.getHeight() << ")"; return stream; } // Function to generate integral random pool dimensions from 1 to max_size inline double random(double max_size) { return 1 + static_cast<int>(max_size* static_cast<double>(std::rand()) / (RAND_MAX + 1.0)); } int main() { const double dimLimit {99.0}; // Upper limit on Pool dimensions std::srand((unsigned) std::time(0)); // Initialize the random number generator const int poolCount {20}; // Number of Pool object to be created std::vector<Pool> pools; // Vector of Pool objects // Create 20 Pool objects for (int i {}; i < poolCount; ++i) pools.push_back(Pool {random(dimLimit), random(dimLimit), random(dimLimit)}); int first {}; // Index of first Pool object of pair int second {1}; // Index of second Pool object of pair double minVolume {(pools[first] + pools[second]).volume()}; for (int i {}; i < poolCount - 1; ++i) for (int j {i + 1}; j < poolCount; j++) if (pools[i] + pools[j] < minVolume) { first = i; second = j; minVolume = (pools[i] + pools[j]).volume(); } std::cout << "The two pools that sum to the smallest volume are: " << pools[first] << pools[second]; std::cout << "\nThe volume of the first pool is " << pools[first].volume(); std::cout << "\nThe volume of the second pool is " << pools[second].volume(); std::cout << "\nThe pool that the sum of these pools is" << pools[first] + pools[second]; std::cout << "\nThe volume of the sum is " << minVolume << std::endl; }