diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp new file mode 100644 index 0000000..52628c3 --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,44 @@ +#include "animal.h" + +animal::animal() {}; +animal::~animal() {}; + +unsigned int bird::legs() { return 2; } +std::wstring sparrow::species() { return L"sparrow"; } + +unsigned int insect::legs() { return 6; } +std::wstring cockroach::species() { return L"cockroach"; } + +unsigned int spider::legs() { return 8; } +std::wstring tarantula::species() { return L"tarantula"; } + +std::wstring leg_counter::add_animal(std::unique_ptr &a) +{ + std::wstring s = a->species(); + animals.push_back(std::move(a)); + return s; +} + +unsigned int leg_counter::legs() const +{ + unsigned int sum_of_legs = 0; + for each (std::unique_ptr const& a in animals) + { + sum_of_legs += a->legs(); + } + return sum_of_legs; +} + +std::unique_ptr animal_factory(int animal_type) +{ + switch (animal_type) { + case 1: + return std::make_unique(); + case 2: + return std::make_unique(); + case 3: + return std::make_unique(); + default: + return nullptr; + } +} \ No newline at end of file diff --git a/exercise/01/animal.h b/exercise/01/animal.h new file mode 100644 index 0000000..0169216 --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,59 @@ +#pragma once +#include +#include + +class animal +{ +public: + animal(); + virtual ~animal() = 0; + virtual std::wstring species() = 0; + virtual unsigned int legs() = 0; +}; + +class bird : public animal +{ +public: + unsigned int legs() override; +}; + +class sparrow : public bird +{ +public: + std::wstring species() override; +}; + +class insect : public animal +{ +public: + unsigned int legs() override; +}; + +class cockroach : public insect +{ +public: + std::wstring species() override; +}; + +class spider : public animal +{ +public: + unsigned int legs() override; +}; + +class tarantula : public spider +{ +public: + std::wstring species() override; +}; + +class leg_counter +{ +private: + std::vector> animals; +public: + std::wstring add_animal(std::unique_ptr &a); + unsigned int legs() const; +}; + +std::unique_ptr animal_factory(int animal_type); \ No newline at end of file diff --git a/exercise/01/animals.sln b/exercise/01/animals.sln new file mode 100644 index 0000000..f535a77 --- /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.27703.2026 +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 = {27694676-7428-406E-901C-2F530B4EA78A} + EndGlobalSection +EndGlobal diff --git a/exercise/01/animals.vcxproj b/exercise/01/animals.vcxproj index 0c14cbf..22e7248 100644 --- a/exercise/01/animals.vcxproj +++ b/exercise/01/animals.vcxproj @@ -1,95 +1,95 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {BB15329E-10D8-4740-94BC-64C50BA4EE12} - Win32Proj - array - 10.0.17134.0 - - - - DynamicLibrary - true - v141 - Unicode - false - - - DynamicLibrary - false - v141 - true - Unicode - false - - - - - - - - - - - - - true - - - true - - - - NotUsing - Level3 - Disabled - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Level3 - NotUsing - MaxSpeed - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {BB15329E-10D8-4740-94BC-64C50BA4EE12} + Win32Proj + array + 10.0.17134.0 + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + true + + + true + + + + NotUsing + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + NotUsing + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + \ No newline at end of file diff --git a/exercise/01/animals.vcxproj.filters b/exercise/01/animals.vcxproj.filters index ad4d9ab..07fc409 100644 --- a/exercise/01/animals.vcxproj.filters +++ b/exercise/01/animals.vcxproj.filters @@ -1,24 +1,24 @@ - - - - - {f2ada0ee-91e7-4bbd-b33d-837567340243} - - - {3cc0fd48-6585-4969-94bd-406daa5f786b} - - - - - test - - - src - - - - - src - - + + + + + {f2ada0ee-91e7-4bbd-b33d-837567340243} + + + {3cc0fd48-6585-4969-94bd-406daa5f786b} + + + + + test + + + src + + + + + src + + \ No newline at end of file diff --git a/exercise/01/test.cpp b/exercise/01/test.cpp index f7f2c1f..cab4a49 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -39,7 +39,7 @@ TEST_CLASS(test_animal_hierarchy) 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()); - } + } TEST_METHOD(legg_counter_same_animal) { leg_counter lc; @@ -47,5 +47,5 @@ TEST_CLASS(test_animal_hierarchy) lc.add_animal(animal_factory(2)); Assert::AreEqual(4u, lc.legs()); } - }; +