From d416dccb2a59529a29fae201bcbc01204a2f2fda Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Wed, 14 Nov 2018 17:00:01 +0100 Subject: [PATCH 1/2] Implementation, missing legs() for multiple animals --- exercise/01/animal.cpp | 38 +++++++++++++ exercise/01/animal.h | 87 +++++++++++++++++++++++++++++ exercise/01/animals.vcxproj | 6 +- exercise/01/animals.vcxproj.filters | 14 ++--- exercise/01/animals.vcxproj.user | 4 ++ exercise/01/test.cpp | 4 +- 6 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 exercise/01/animal.cpp create mode 100644 exercise/01/animal.h create mode 100644 exercise/01/animals.vcxproj.user diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp new file mode 100644 index 0000000..3f04098 --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,38 @@ +#include "animal.h" + +unsigned insect::legs() const +{ + return legCount; +} + +std::wstring cockroach::species() const +{ + return speciesName; +} + +unsigned bird::legs() const +{ + return legCount; + +} + +std::wstring sparrow::species() const +{ + return speciesName; +} + +unsigned spider::legs() const +{ + return legCount; + +} + +std::wstring tarantula::species() const +{ + return speciesName; +} + +std::wstring leg_counter::add_animal(animal* animal) const +{ + return animal->species(); +} diff --git a/exercise/01/animal.h b/exercise/01/animal.h new file mode 100644 index 0000000..5cbc01a --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,87 @@ +#pragma once +#include +#include + +class animal +{ +public: + virtual ~animal() = default; + virtual std::wstring species() const = 0; + virtual unsigned legs() const = 0; +}; + +class insect : public animal +{ +public: + unsigned legs() const override; +protected: + static const unsigned legCount = 6; +}; + +class cockroach : public insect +{ +public: + std::wstring species() const override; +protected: + const std::wstring speciesName = L"cockroach"; +}; + +class bird : public animal +{ +public: + unsigned legs() const override; +protected: + static const unsigned legCount = 2; +}; + +class sparrow : public bird +{ +public: + std::wstring species() const override; +protected: + const std::wstring speciesName = L"sparrow"; +}; + +class spider : public animal +{ +public: + unsigned legs() const override; +protected: + static const unsigned legCount = 8; +}; + +class tarantula : public spider +{ +public: + std::wstring species() const override; +protected: + const std::wstring speciesName = L"tarantula"; +}; + +class leg_counter +{ +public: + std::wstring add_animal(animal*) const; +private: + std::vector animals; +}; + +enum Animals { Cockroach = 1, Sparrow = 2, Tarantula = 3 }; + +inline animal* animal_factory(int num) +{ + if (num == Animals::Cockroach) + { + return new cockroach; + } + if (num == Animals::Sparrow) + { + return new sparrow; + } + if (num == Animals::Tarantula) + { + return new tarantula; + } + + return nullptr; +} \ No newline at end of file diff --git a/exercise/01/animals.vcxproj b/exercise/01/animals.vcxproj index 0c14cbf..42f52e7 100644 --- a/exercise/01/animals.vcxproj +++ b/exercise/01/animals.vcxproj @@ -82,13 +82,13 @@ $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - + + + diff --git a/exercise/01/animals.vcxproj.filters b/exercise/01/animals.vcxproj.filters index ad4d9ab..d9e01c3 100644 --- a/exercise/01/animals.vcxproj.filters +++ b/exercise/01/animals.vcxproj.filters @@ -1,24 +1,20 @@  - - {f2ada0ee-91e7-4bbd-b33d-837567340243} - {3cc0fd48-6585-4969-94bd-406daa5f786b} + + {5bbc7edf-5c58-4e05-91a8-f558bea5d453} + test - - src - + - - src - + \ No newline at end of file diff --git a/exercise/01/animals.vcxproj.user b/exercise/01/animals.vcxproj.user new file mode 100644 index 0000000..be25078 --- /dev/null +++ b/exercise/01/animals.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/exercise/01/test.cpp b/exercise/01/test.cpp index f7f2c1f..609d1d8 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -38,14 +38,14 @@ TEST_CLASS(test_animal_hierarchy) Assert::AreEqual(L"cockroach", lc.add_animal(animal_factory(1)).c_str()); Assert::AreEqual(L"sparrow", lc.add_animal(animal_factory(2)).c_str()); Assert::AreEqual(L"tarantula", lc.add_animal(animal_factory(3)).c_str()); - Assert::AreEqual(16u, lc.legs()); + //Assert::AreEqual(16u, lc.legs()); } TEST_METHOD(legg_counter_same_animal) { leg_counter lc; lc.add_animal(animal_factory(2)); lc.add_animal(animal_factory(2)); - Assert::AreEqual(4u, lc.legs()); + //Assert::AreEqual(4u, lc.legs()); } }; From 841da1c3e64b4b814fa04b70ac571aaece3529d1 Mon Sep 17 00:00:00 2001 From: Lucano Zganec Date: Thu, 15 Nov 2018 13:27:40 +0100 Subject: [PATCH 2/2] Finished exercises --- exercise/01/animal.cpp | 29 ++++++++++++++++++++++++++++- exercise/01/animal.h | 26 ++++---------------------- exercise/01/test.cpp | 4 ++-- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp index 3f04098..b581a6f 100644 --- a/exercise/01/animal.cpp +++ b/exercise/01/animal.cpp @@ -32,7 +32,34 @@ std::wstring tarantula::species() const return speciesName; } -std::wstring leg_counter::add_animal(animal* animal) const +std::wstring leg_counter::add_animal(std::unique_ptr animal) { + animalCount += animal->legs(); return animal->species(); } + +unsigned leg_counter::legs() const +{ + return animalCount; +} + + +enum Animals { Cockroach = 1, Sparrow = 2, Tarantula = 3 }; + +std::unique_ptr animal_factory(int num) +{ + if (num == Animals::Cockroach) + { + return std::make_unique(); + } + if (num == Animals::Sparrow) + { + return std::make_unique(); + } + if (num == Animals::Tarantula) + { + return std::make_unique(); + } + + return nullptr; +} \ No newline at end of file diff --git a/exercise/01/animal.h b/exercise/01/animal.h index 5cbc01a..1ca3dce 100644 --- a/exercise/01/animal.h +++ b/exercise/01/animal.h @@ -1,6 +1,5 @@ #pragma once #include -#include class animal { @@ -61,27 +60,10 @@ class tarantula : public spider class leg_counter { public: - std::wstring add_animal(animal*) const; + std::wstring add_animal(std::unique_ptr); + unsigned legs() const; private: - std::vector animals; + unsigned animalCount = 0; }; -enum Animals { Cockroach = 1, Sparrow = 2, Tarantula = 3 }; - -inline animal* animal_factory(int num) -{ - if (num == Animals::Cockroach) - { - return new cockroach; - } - if (num == Animals::Sparrow) - { - return new sparrow; - } - if (num == Animals::Tarantula) - { - return new tarantula; - } - - return nullptr; -} \ No newline at end of file +std::unique_ptr animal_factory(int num); \ No newline at end of file diff --git a/exercise/01/test.cpp b/exercise/01/test.cpp index 609d1d8..f7f2c1f 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -38,14 +38,14 @@ TEST_CLASS(test_animal_hierarchy) Assert::AreEqual(L"cockroach", lc.add_animal(animal_factory(1)).c_str()); Assert::AreEqual(L"sparrow", lc.add_animal(animal_factory(2)).c_str()); Assert::AreEqual(L"tarantula", lc.add_animal(animal_factory(3)).c_str()); - //Assert::AreEqual(16u, lc.legs()); + Assert::AreEqual(16u, lc.legs()); } TEST_METHOD(legg_counter_same_animal) { leg_counter lc; lc.add_animal(animal_factory(2)); lc.add_animal(animal_factory(2)); - //Assert::AreEqual(4u, lc.legs()); + Assert::AreEqual(4u, lc.legs()); } };