C++ examples for Class:Operator Overload
Overload addition operator for Pool objects
#include <iostream> #include <vector> #include <cstdlib> // For random number generator #include <ctime> // For time function #include <iostream> #include <iomanip> #include <algorithm> // For max() and min() functions class Pool{//from www . j a va2 s . 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 {std::max(lv, wv)}, width {std::min(lv, wv)}, height {hv} {} Pool() = default; 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 << ")"; } // 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}; std::srand((unsigned)std::time(0)); const int poolCount {20}; std::vector<Pool> pools; // 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].listPool(); pools[second].listPool(); 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]).listPool(); std::cout << "\nThe volume of the sum is " << minVolume << std::endl; }