diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp new file mode 100644 index 0000000..8391d95 --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,46 @@ +#include "animal.h" + +animal::~animal() +{ +} + +unsigned int insect::legs() const +{ + return 6u; +} + +unsigned int bird::legs() const +{ + return 2u; +} + +unsigned int spider::legs() const +{ + return 8u; +} + +std::wstring cockroach::species() const +{ + return std::wstring(L"cockroach"); +} + +std::wstring sparrow::species() const +{ + return std::wstring(L"sparrow"); +} + +std::wstring tarantula::species() const +{ + return std::wstring(L"tarantula"); +} + +std::unique_ptr animal_factory(unsigned int id) +{ + switch (id) + { + case 1: return std::make_unique(); + case 2: return std::make_unique(); + case 3: return std::make_unique(); + default: return nullptr; + } +} diff --git a/exercise/01/animal.h b/exercise/01/animal.h new file mode 100644 index 0000000..f51d0ac --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,49 @@ +#pragma once + +#include + +class animal +{ +public: + virtual std::wstring species() const = 0; + virtual unsigned int legs() const = 0; + virtual ~animal() = 0; +}; + +class insect : public animal +{ +public: + unsigned int legs() const override; +}; + +class bird : public animal +{ +public: + unsigned int legs() const override; +}; + +class spider : public animal +{ +public: + unsigned int legs() const override; +}; + +class cockroach : public insect +{ +public: + std::wstring species() const override; +}; + +class sparrow : public bird +{ +public: + std::wstring species() const override; +}; + +class tarantula : public spider +{ +public: + std::wstring species() const override; +}; + +std::unique_ptr animal_factory(unsigned int id); diff --git a/exercise/01/animals.sln b/exercise/01/animals.sln new file mode 100644 index 0000000..1e13d1f --- /dev/null +++ b/exercise/01/animals.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2050 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "animals", "animals.vcxproj", "{BB15329E-10D8-4740-94BC-64C50BA4EE12}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.ActiveCfg = Debug|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.Build.0 = Debug|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.ActiveCfg = Release|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {728A8247-7589-4D4A-BC24-4C2972DB8D3D} + EndGlobalSection +EndGlobal diff --git a/exercise/01/animals.vcxproj b/exercise/01/animals.vcxproj index 0c14cbf..a7c1193 100644 --- a/exercise/01/animals.vcxproj +++ b/exercise/01/animals.vcxproj @@ -82,13 +82,15 @@ $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - + + + + + diff --git a/exercise/01/animals.vcxproj.filters b/exercise/01/animals.vcxproj.filters index ad4d9ab..af13239 100644 --- a/exercise/01/animals.vcxproj.filters +++ b/exercise/01/animals.vcxproj.filters @@ -15,10 +15,16 @@ src + + src + 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/leg_counter.cpp b/exercise/01/leg_counter.cpp new file mode 100644 index 0000000..a6c6414 --- /dev/null +++ b/exercise/01/leg_counter.cpp @@ -0,0 +1,16 @@ +#include "leg_counter.h" + +std::wstring leg_counter::add_animal(std::unique_ptr animal) +{ + if (animal != nullptr) + { + count += animal->legs(); + return animal->species(); + } + return std::wstring(); +} + +unsigned int leg_counter::legs() const +{ + return count; +} diff --git a/exercise/01/leg_counter.h b/exercise/01/leg_counter.h new file mode 100644 index 0000000..ce227eb --- /dev/null +++ b/exercise/01/leg_counter.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include "animal.h" + +class leg_counter +{ +private: + unsigned int count; +public: + leg_counter() : count(0) {}; + std::wstring add_animal(std::unique_ptr animal); + unsigned int legs() const; +}; + diff --git a/exercise/01/test.cpp b/exercise/01/test.cpp index f7f2c1f..dad45e8 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -2,6 +2,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #include "animal.h" +#include "leg_counter.h" TEST_CLASS(test_animal_hierarchy) {