C++ Pointer and References to Class Objects
#include <iomanip> #include <iostream> #include <memory> #include <vector> #include <cstdlib> // For random number generator #include <ctime> // For time function class Pool/*from w w w .ja v 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 // Volume of a pool { return length*width*height; } int compare(const Pool& pool) { if (volume() < pool.volume()) return -1; if (volume() == pool.volume()) return 0; return 1; } void listPool() { std::cout << " Pool(" << std::setw(2) << length << "," << std::setw(2) << width << "," << std::setw(2) << height << ")"; } }; template <typename T> using ptr = std::shared_ptr<T>; class Package { private: ptr<Pool> pPool; // Pointer to the Pool object ptr<Package> pNext; // Pointer to the next Package public: Package(ptr<Pool> pb) : pPool {pb}, pNext {} {} // Constructor ptr<Pool>& getPool() { return pPool; } // Retrieve the Pool pointer ptr<Package>& getNext() { return pNext; } // Get next Package address void setNext(ptr<Package>& pPackage) // Point to next object { pNext = pPackage; } }; class Truckload { private: ptr<Package> pHead; // First in the list ptr<Package> pTail; // Last in the list ptr<Package> pCurrent; // Last retrieved from the list public: Truckload() {} // No-arg constructor empty truckload Truckload(ptr<Pool> pPool) // Constructor - one Pool { pHead = pTail = std::make_shared<Package>(pPool); } Truckload(const std::vector< ptr<Pool> >& pools); // Constructor - vector of Pools ptr<Pool> getFirstPool(); // Get the first Pool ptr<Pool> getNextPool(); // Get the next Pool void addPool(ptr<Pool> pPool); // Add a new Pool bool deletePool(ptr<Pool> pPool); // Delete a Pool void listPools(); // Output the Pools }; Truckload::Truckload(const std::vector< ptr<Pool> >& pools) { for (auto pPool : pools) { addPool(pPool); } } void Truckload::addPool(ptr<Pool> pPool) { auto pPackage = std::make_shared<Package>(pPool); // Create a Package if (pHead) // Check list is not empty pTail->setNext(pPackage); // Add the new object to the tail else // List is empty pHead = pPackage; // so new object is the head pTail = pPackage; // Store its address as tail } ptr<Pool> Truckload::getFirstPool() { pCurrent = pHead->getNext(); return pHead->getPool(); } ptr<Pool> Truckload::getNextPool() { if (!pCurrent) // If there's no current... return getFirstPool(); // ...return the 1st auto pPackage = pCurrent->getNext(); // Save the next package if (pPackage) // If there is one... { pCurrent = pPackage; // Update current to the next return pPackage->getPool(); } pCurrent = nullptr; // If we get to here... return nullptr; // ...there was no next } void Truckload::listPools() { pCurrent = pHead; int count {}; while (pCurrent) { pCurrent->getPool()->listPool(); pCurrent = pCurrent->getNext(); if(! (++count % 5)) std::cout << std::endl; } if (count % 5) std::cout << std::endl; } // Function to generate a random integer 1 to count inline int random(int count) { return 1 + static_cast<int> (count*static_cast<double>(std::rand())/(RAND_MAX + 1.0)); } int main() { const int dimLimit {99}; // Upper limit on Pool dimensions std::srand((unsigned)std::time(0)); // Initialize the random number generator Truckload load1; // Create an empty list // Add 12 random Pool objects to the list const int poolCount {12}; for (int i {} ; i < poolCount ; ++i) load1.addPool(std::make_shared<Pool>(random(dimLimit), random(dimLimit), random(dimLimit))); std::cout << "The first list:\n"; load1.listPools(); // Find the largest Pool in the list ptr<Pool> pPool {load1.getFirstPool()}; ptr<Pool> pNextPool {}; while (pNextPool = load1.getNextPool()) // Assign & then test pointer to next Pool if (pPool->compare(*pNextPool) < 0) pPool = pNextPool; std::cout << "\nThe largest pool in the first list is:"; pPool->listPool(); std::cout << std::endl; load1.deletePool(pPool); std::cout << "\nAfter deleting the largest pool, the list contains:\n"; load1.listPools(); const int nPools {20}; // Number of vector elements std::vector< ptr<Pool> > pools; // Array of Pool objects for (int i {} ; i < nPools ; ++i) pools.push_back(std::make_shared<Pool>( random(dimLimit), random(dimLimit), random(dimLimit))); Truckload load2(pools); std::cout << "\nThe second list:\n"; load2.listPools(); pPool = load2.getFirstPool(); while (pNextPool = load2.getNextPool()) if (pPool->compare(*pNextPool) > 0) pPool = pNextPool; std::cout << "\nThe smallest pool in the second list is"; pPool->listPool(); std::cout << std::endl; }