From f3a851e9510cb5577f0ef82c8dda1a7ef77a2845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Poyraz=20K=C3=BC=C3=A7=C3=BCkarslan?= <83272398+PoyrazK@users.noreply.github.com> Date: Thu, 5 Mar 2026 21:19:50 +0300 Subject: [PATCH] refactor: remove using namespace std from headers and source Removes the `using namespace std;` directive from all header and source files to prevent global namespace pollution. All standard library types, objects, and functions (e.g., `std::vector`, `std::string`, `std::cout`, `std::endl`, `std::out_of_range`) are now explicitly qualified with the `std::` prefix. This aligns the codebase with C++ best practices for better maintainability and naming collision prevention. --- include/Planet.hpp | 108 ++++++++++++++++++++-------------------- include/SolarSystem.hpp | 46 ++++++++--------- src/main.cpp | 16 +++--- tests/unit_tests.cpp | 14 +++--- 4 files changed, 91 insertions(+), 93 deletions(-) diff --git a/include/Planet.hpp b/include/Planet.hpp index 7d62504..492908a 100644 --- a/include/Planet.hpp +++ b/include/Planet.hpp @@ -24,8 +24,6 @@ #include #include -using namespace std; - /** * @brief Strict enumeration for planet data structure types. * @@ -56,8 +54,8 @@ inline PlanetType parsePlanetType(char input) { case 'q': return PlanetType::Queue; default: - throw invalid_argument(string("Invalid planet type: '") + input + - "'. Use V, S, or Q."); + throw std::invalid_argument(std::string("Invalid planet type: '") + input + + "'. Use V, S, or Q."); } } @@ -72,7 +70,7 @@ inline PlanetType parsePlanetType(char input) { */ template class BasePlanet { public: - string name; ///< Planet name, managed by std::string (RAII). + std::string name; ///< Planet name, managed by std::string (RAII). int metadata = 0; ///< Planet identifier/index. /** @@ -121,7 +119,7 @@ template class BasePlanet { * @return Type name string (e.g., "Vector", "Stack", "Queue"). * @note Complexity: O(1). */ - virtual string typeName() const = 0; + virtual std::string typeName() const = 0; /** * @brief Removes all elements below the given threshold. @@ -143,7 +141,7 @@ template class BasePlanet { */ template class VectorPlanet : public BasePlanet { public: - vector v; ///< Internal vector storage. + std::vector v; ///< Internal vector storage. /** * @brief Appends a value to the end of the vector. @@ -159,7 +157,7 @@ template class VectorPlanet : public BasePlanet { */ void pop() override { if (v.empty()) - throw underflow_error("Cannot pop from empty Vector planet"); + throw std::underflow_error("Cannot pop from empty Vector planet"); v.pop_back(); } @@ -168,7 +166,7 @@ template class VectorPlanet : public BasePlanet { * @return Current element count. * @note Complexity: O(1). */ - int size() const override { return v.size(); } + int size() const override { return static_cast(v.size()); } /** * @brief Checks whether the vector is empty. @@ -182,19 +180,19 @@ template class VectorPlanet : public BasePlanet { * @return The string literal "Vector". * @note Complexity: O(1). */ - string typeName() const override { return "Vector"; } + std::string typeName() const override { return "Vector"; } /** * @brief Displays all vector elements to stdout. * @note Complexity: O(n) where n is the number of elements. */ void display() const override { - cout << "(" << this->typeName() << ") : "; + std::cout << "(" << this->typeName() << ") : "; if (v.empty()) - cout << "EMPTY"; + std::cout << "EMPTY"; for (size_t i = 0; i < v.size(); i++) - cout << v[i] << " "; - cout << endl; + std::cout << v[i] << " "; + std::cout << std::endl; } /** @@ -209,8 +207,8 @@ template class VectorPlanet : public BasePlanet { */ void removeBelow(const T &threshold, int &removedCount) override { int beforeSize = static_cast(v.size()); - v.erase(remove_if(v.begin(), v.end(), - [&threshold](const T &val) { return val < threshold; }), + v.erase(std::remove_if(v.begin(), v.end(), + [&threshold](const T &val) { return val < threshold; }), v.end()); removedCount += beforeSize - static_cast(v.size()); } @@ -226,7 +224,7 @@ template class VectorPlanet : public BasePlanet { */ template class StackPlanet : public BasePlanet { public: - stack s; ///< Internal stack storage. + std::stack s; ///< Internal stack storage. /** * @brief Pushes a value onto the top of the stack. @@ -242,7 +240,7 @@ template class StackPlanet : public BasePlanet { */ void pop() override { if (s.empty()) - throw underflow_error("Cannot pop from empty Stack planet"); + throw std::underflow_error("Cannot pop from empty Stack planet"); s.pop(); } @@ -251,7 +249,7 @@ template class StackPlanet : public BasePlanet { * @return Current element count. * @note Complexity: O(1). */ - int size() const override { return s.size(); } + int size() const override { return static_cast(s.size()); } /** * @brief Checks whether the stack is empty. @@ -265,22 +263,22 @@ template class StackPlanet : public BasePlanet { * @return The string literal "Stack". * @note Complexity: O(1). */ - string typeName() const override { return "Stack"; } + std::string typeName() const override { return "Stack"; } /** * @brief Displays all stack elements to stdout (top to bottom). * @note Complexity: O(n) where n is the number of elements. */ void display() const override { - cout << "(" << this->typeName() << ") : "; - stack temp = s; // Copy to preserve original + std::cout << "(" << this->typeName() << ") : "; + std::stack temp = s; // Copy to preserve original if (temp.empty()) - cout << "EMPTY"; + std::cout << "EMPTY"; while (!temp.empty()) { - cout << temp.top() << " "; + std::cout << temp.top() << " "; temp.pop(); } - cout << endl; + std::cout << std::endl; } /** @@ -291,22 +289,22 @@ template class StackPlanet : public BasePlanet { */ void removeBelow(const T &threshold, int &removedCount) override { int beforeSize = static_cast(s.size()); - stack survivors; - stack temp; + std::stack survivors; + std::stack temp; // Reverse into temp to preserve original insertion order while (!s.empty()) { - temp.push(move(s.top())); + temp.push(std::move(s.top())); s.pop(); } // Filter: keep only elements >= threshold while (!temp.empty()) { - T value = move(temp.top()); + T value = std::move(temp.top()); temp.pop(); if (value >= threshold) - survivors.push(move(value)); + survivors.push(std::move(value)); } - s = move(survivors); + s = std::move(survivors); removedCount += beforeSize - static_cast(s.size()); } }; @@ -321,7 +319,7 @@ template class StackPlanet : public BasePlanet { */ template class QueuePlanet : public BasePlanet { public: - queue q; ///< Internal queue storage. + std::queue q; ///< Internal queue storage. /** * @brief Enqueues a value at the back of the queue. @@ -337,7 +335,7 @@ template class QueuePlanet : public BasePlanet { */ void pop() override { if (q.empty()) - throw underflow_error("Cannot pop from empty Queue planet"); + throw std::underflow_error("Cannot pop from empty Queue planet"); q.pop(); } @@ -346,7 +344,7 @@ template class QueuePlanet : public BasePlanet { * @return Current element count. * @note Complexity: O(1). */ - int size() const override { return q.size(); } + int size() const override { return static_cast(q.size()); } /** * @brief Checks whether the queue is empty. @@ -360,22 +358,22 @@ template class QueuePlanet : public BasePlanet { * @return The string literal "Queue". * @note Complexity: O(1). */ - string typeName() const override { return "Queue"; } + std::string typeName() const override { return "Queue"; } /** * @brief Displays all queue elements to stdout (front to back). * @note Complexity: O(n) where n is the number of elements. */ void display() const override { - cout << "(" << this->typeName() << ") : "; - queue temp = q; // Copy to preserve original + std::cout << "(" << this->typeName() << ") : "; + std::queue temp = q; // Copy to preserve original if (temp.empty()) - cout << "EMPTY"; + std::cout << "EMPTY"; while (!temp.empty()) { - cout << temp.front() << " "; + std::cout << temp.front() << " "; temp.pop(); } - cout << endl; + std::cout << std::endl; } /** @@ -386,16 +384,16 @@ template class QueuePlanet : public BasePlanet { */ void removeBelow(const T &threshold, int &removedCount) override { int beforeSize = static_cast(q.size()); - queue survivors; + std::queue survivors; // Drain queue, keeping only elements >= threshold while (!q.empty()) { - T value = move(q.front()); + T value = std::move(q.front()); q.pop(); if (value >= threshold) - survivors.push(move(value)); + survivors.push(std::move(value)); } - q = move(survivors); + q = std::move(survivors); removedCount += beforeSize - static_cast(q.size()); } }; @@ -418,36 +416,36 @@ template class PlanetFactory { * * @param type The PlanetType enum specifying which derived class to * instantiate. - * @return A unique_ptr> owning the newly created planet. + * @return A std::unique_ptr> owning the newly created planet. * @throws std::invalid_argument If the PlanetType is not recognized. * @note Complexity: O(1). */ - static unique_ptr> create(PlanetType type) { + static std::unique_ptr> create(PlanetType type) { switch (type) { case PlanetType::Vector: - return make_unique>(); + return std::make_unique>(); case PlanetType::Stack: - return make_unique>(); + return std::make_unique>(); case PlanetType::Queue: - return make_unique>(); + return std::make_unique>(); } - throw invalid_argument("Unknown PlanetType in factory"); + throw std::invalid_argument("Unknown PlanetType in factory"); } }; /** * @brief Smart Pointer Variety — Future Extension Guide. * - * Current architecture uses unique_ptr> for exclusive ownership. + * Current architecture uses std::unique_ptr> for exclusive ownership. * * For inter-planet interactions (e.g., moons orbiting planets): - * - shared_ptr> — multiple owners share a planet - * - weak_ptr> — non-owning observer (breaks circular + * - std::shared_ptr> — multiple owners share a planet + * - std::weak_ptr> — non-owning observer (breaks circular * references) * * @code - * shared_ptr> planet = make_shared>(); - * weak_ptr> moon_ref = planet; + * std::shared_ptr> planet = std::make_shared>(); + * std::weak_ptr> moon_ref = planet; * if (auto p = moon_ref.lock()) { p->display(); } * @endcode */ diff --git a/include/SolarSystem.hpp b/include/SolarSystem.hpp index fdcd09a..dc7663a 100644 --- a/include/SolarSystem.hpp +++ b/include/SolarSystem.hpp @@ -24,9 +24,9 @@ * All operations are public, const-correct methods providing a controlled * interface. No global state — instances are created locally where needed. * - * RAII Guarantee: All planets are owned via unique_ptr. When a planet is + * RAII Guarantee: All planets are owned via std::unique_ptr. When a planet is * removed (blackHole/removePlanet) or the entire system is reset - * (supernova/resetSystem), unique_ptr destructors automatically deallocate all + * (supernova/resetSystem), std::unique_ptr destructors automatically deallocate all * associated memory. No manual delete is ever required. * * Template class implementations are defined inline in this header file, @@ -46,8 +46,8 @@ template class SolarSystem { }; Star star; ///< System-wide state counters. - vector>> - planets; ///< Polymorphic ownership via unique_ptr (RAII). + std::vector>> + planets; ///< Polymorphic ownership via std::unique_ptr (RAII). public: // ───────────────────────────────────────────── @@ -59,7 +59,7 @@ template class SolarSystem { * @note Complexity: O(1) — direct access to counter variable. */ void elementCount() const { - cout << " Element count :" << star.element_count << endl; + std::cout << " Element count :" << star.element_count << std::endl; } /** @@ -67,7 +67,7 @@ template class SolarSystem { * @note Complexity: O(1) — direct access to counter variable. */ void planetCount() const { - cout << " Planet count :" << star.planet_count << endl; + std::cout << " Planet count :" << star.planet_count << std::endl; } /** @@ -119,7 +119,7 @@ template class SolarSystem { */ void pushToPlanet(int index, const T &value) { if (index < 0 || index >= (int)planets.size()) - throw out_of_range("Planet index " + to_string(index) + + throw std::out_of_range("Planet index " + std::to_string(index) + " is out of range"); planets[index]->push(value); star.element_count++; @@ -137,9 +137,9 @@ template class SolarSystem { */ void addPlanet() { char input; - cout << "Enter the type of Planet" << endl + std::cout << "Enter the type of Planet" << std::endl << "V For Vector- S For Stack- Q For Queue : "; - cin >> input; + std::cin >> input; PlanetType type = parsePlanetType(input); addPlanet(type); @@ -157,14 +157,14 @@ template class SolarSystem { * @note Complexity: O(1) amortized. */ void addPlanet(PlanetType type) { - unique_ptr> p = PlanetFactory::create(type); + std::unique_ptr> p = PlanetFactory::create(type); p->name = p->typeName() + "_Planet"; - p->metadata = planets.size(); + p->metadata = static_cast(planets.size()); - cout << "Planet Id: " << p->metadata << " (" << p->name << ")" << endl; + std::cout << "Planet Id: " << p->metadata << " (" << p->name << ")" << std::endl; - planets.push_back(move(p)); // Move semantics: transfers ownership + planets.push_back(std::move(p)); // Move semantics: transfers ownership star.planet_count++; star.id = star.planet_count - 1; } @@ -172,7 +172,7 @@ template class SolarSystem { /** * @brief Destroys a single planet and reclaims all its memory (Black Hole). * - * RAII Guarantee: When the unique_ptr is erased from the vector, the + * RAII Guarantee: When the std::unique_ptr is erased from the vector, the * derived planet object (and all its internal data) is automatically * deallocated. No manual delete is required. Zero memory remains allocated * for the destroyed planet after this call. @@ -183,14 +183,14 @@ template class SolarSystem { */ void removePlanet(int planetIndex) { if (planetIndex < 0 || planetIndex >= (int)planets.size()) - throw out_of_range("Planet index " + to_string(planetIndex) + + throw std::out_of_range("Planet index " + std::to_string(planetIndex) + " is out of range"); BasePlanet &p = *planets[planetIndex]; int elementsLost = p.size(); - cout << "BLACK HOLE: Planet [" << p.name << "] absorbed! " << elementsLost - << " elements lost." << endl; + std::cout << "BLACK HOLE: Planet [" << p.name << "] absorbed! " << elementsLost + << " elements lost." << std::endl; // unique_ptr automatically deletes the derived object when erased planets.erase(planets.begin() + planetIndex); @@ -209,8 +209,8 @@ template class SolarSystem { /** * @brief Destroys the entire solar system and resets all state (Supernova). * - * RAII Guarantee: planets.clear() destroys every unique_ptr in the vector. - * Each unique_ptr destructor deletes its owned BasePlanet-derived object, + * RAII Guarantee: planets.clear() destroys every std::unique_ptr in the vector. + * Each std::unique_ptr destructor deletes its owned BasePlanet-derived object, * which in turn destroys the internal vector/stack/queue via their own * destructors. After this call, zero heap memory remains allocated for * any planet or element. All Star counters are reset to zero. @@ -240,14 +240,14 @@ template class SolarSystem { * @note Complexity: O(p * m) where p is the number of planets and m is the elements per planet. */ void gravityPull(const T &threshold) { - cout << "GRAVITY PULL: Absorbing elements < " << threshold << endl; + std::cout << "GRAVITY PULL: Absorbing elements < " << threshold << std::endl; int totalPulled = 0; for (size_t i = 0; i < planets.size(); i++) planets[i]->removeBelow(threshold, totalPulled); star.element_count -= totalPulled; - cout << totalPulled << " elements absorbed by gravity." << endl; + std::cout << totalPulled << " elements absorbed by gravity." << std::endl; } /** @@ -262,7 +262,7 @@ template class SolarSystem { */ void deleteElement(int index) { if (index < 0 || index >= (int)planets.size()) - throw out_of_range("Planet index " + to_string(index) + + throw std::out_of_range("Planet index " + std::to_string(index) + " is out of range"); planets[index]->pop(); star.element_count--; @@ -282,7 +282,7 @@ template class SolarSystem { void travelPlanet() const { for (size_t i = 0; i < planets.size(); i++) { BasePlanet &p = *planets[i]; - cout << i + 1 << ". Planet [" << p.name << "] (ID: " << p.metadata + std::cout << i + 1 << ". Planet [" << p.name << "] (ID: " << p.metadata << ") Elements "; p.display(); } diff --git a/src/main.cpp b/src/main.cpp index f57989a..0942f29 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,17 +49,17 @@ int main() { system.resetSystem(); // Supernova: Destroy everything system.travelPlanet(); system.planetCount(); - } catch (const out_of_range &e) { - cerr << "[Out of Range Error] " << e.what() << endl; + } catch (const std::out_of_range &e) { + std::cerr << "[Out of Range Error] " << e.what() << std::endl; return 1; - } catch (const underflow_error &e) { - cerr << "[Underflow Error] " << e.what() << endl; + } catch (const std::underflow_error &e) { + std::cerr << "[Underflow Error] " << e.what() << std::endl; return 2; - } catch (const invalid_argument &e) { - cerr << "[Invalid Argument] " << e.what() << endl; + } catch (const std::invalid_argument &e) { + std::cerr << "[Invalid Argument] " << e.what() << std::endl; return 3; - } catch (const exception &e) { - cerr << "[Unexpected Error] " << e.what() << endl; + } catch (const std::exception &e) { + std::cerr << "[Unexpected Error] " << e.what() << std::endl; return 4; } diff --git a/tests/unit_tests.cpp b/tests/unit_tests.cpp index 9bc971e..7487fec 100644 --- a/tests/unit_tests.cpp +++ b/tests/unit_tests.cpp @@ -82,9 +82,9 @@ TEST_F(SolarSystemTest, PushToAllPlanets) { } TEST_F(SolarSystemTest, PushToInvalidIndexThrows) { - ASSERT_THROW(system.pushToPlanet(-1, 10), out_of_range); - ASSERT_THROW(system.pushToPlanet(3, 10), out_of_range); - ASSERT_THROW(system.pushToPlanet(99, 10), out_of_range); + ASSERT_THROW(system.pushToPlanet(-1, 10), std::out_of_range); + ASSERT_THROW(system.pushToPlanet(3, 10), std::out_of_range); + ASSERT_THROW(system.pushToPlanet(99, 10), std::out_of_range); ASSERT_ELEMENT_COUNT_CONSISTENT(system); } @@ -152,9 +152,9 @@ TEST_F(SolarSystemTest, RemoveAllPlanetsOneByOne) { } TEST_F(SolarSystemTest, RemovePlanetInvalidIndexThrows) { - ASSERT_THROW(system.removePlanet(-1), out_of_range); - ASSERT_THROW(system.removePlanet(3), out_of_range); - ASSERT_THROW(system.removePlanet(99), out_of_range); + ASSERT_THROW(system.removePlanet(-1), std::out_of_range); + ASSERT_THROW(system.removePlanet(3), std::out_of_range); + ASSERT_THROW(system.removePlanet(99), std::out_of_range); } TEST_F(SolarSystemTest, BlackHoleAliasWorks) { @@ -297,7 +297,7 @@ TEST_F(SolarSystemTest, ElementCountConsistentAfterMixedOperations) { } TEST_F(SolarSystemTest, DeleteFromEmptyPlanetThrows) { - ASSERT_THROW(system.deleteElement(0), underflow_error); + ASSERT_THROW(system.deleteElement(0), std::underflow_error); ASSERT_ELEMENT_COUNT_CONSISTENT(system); }