From 2119491aeb26e8d14f70d3aae7420d5a1d9febed Mon Sep 17 00:00:00 2001 From: jadranko lucic Date: Wed, 14 Nov 2018 21:02:41 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Jadranko=20Lu=C4=8Di=C4=87,=20Day3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercise/01/animal.cpp | 43 +++++++ exercise/01/animal.h | 59 +++++++++ exercise/01/animals.sln | 25 ++++ exercise/01/animals.vcxproj | 188 ++++++++++++++-------------- exercise/01/animals.vcxproj.filters | 46 +++---- exercise/01/test.cpp | 4 +- 6 files changed, 246 insertions(+), 119 deletions(-) create mode 100644 exercise/01/animal.cpp create mode 100644 exercise/01/animal.h create mode 100644 exercise/01/animals.sln diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp new file mode 100644 index 0000000..94e2e88 --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,43 @@ +#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(animal* a) +{ + animals.push_back(a); + return a->species(); +} + +unsigned int leg_counter::legs() const +{ + unsigned int sum_of_legs = 0; + for each (animal* a in animals) + { + sum_of_legs += a->legs(); + } + return sum_of_legs; +} + +animal* animal_factory(int animal_type) +{ + switch (animal_type) { + case 1: + return new cockroach; + case 2: + return new sparrow; + case 3: + return new tarantula; + 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..85a7055 --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,59 @@ +#pragma once +#include +#include + +class animal +{ +public: + animal();//int legs); + 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(animal* a); + unsigned int legs() const; +}; + +animal* 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()); } - }; + From 32e5d7909a591710d53e21fc8d178d3231a15651 Mon Sep 17 00:00:00 2001 From: jadranko lucic Date: Mon, 19 Nov 2018 10:46:29 +0100 Subject: [PATCH 2/2] some fixes --- exercise/01/animal.cpp | 21 +++++++++++---------- exercise/01/animal.h | 8 ++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp index 94e2e88..52628c3 100644 --- a/exercise/01/animal.cpp +++ b/exercise/01/animal.cpp @@ -12,32 +12,33 @@ 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(animal* a) +std::wstring leg_counter::add_animal(std::unique_ptr &a) { - animals.push_back(a); - return a->species(); + 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 (animal* a in animals) - { + for each (std::unique_ptr const& a in animals) + { sum_of_legs += a->legs(); } return sum_of_legs; } -animal* animal_factory(int animal_type) +std::unique_ptr animal_factory(int animal_type) { switch (animal_type) { case 1: - return new cockroach; + return std::make_unique(); case 2: - return new sparrow; + return std::make_unique(); case 3: - return new tarantula; - default: + 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 index 85a7055..0169216 100644 --- a/exercise/01/animal.h +++ b/exercise/01/animal.h @@ -5,7 +5,7 @@ class animal { public: - animal();//int legs); + animal(); virtual ~animal() = 0; virtual std::wstring species() = 0; virtual unsigned int legs() = 0; @@ -50,10 +50,10 @@ class tarantula : public spider class leg_counter { private: - std::vector animals; + std::vector> animals; public: - std::wstring add_animal(animal* a); + std::wstring add_animal(std::unique_ptr &a); unsigned int legs() const; }; -animal* animal_factory(int animal_type); \ No newline at end of file +std::unique_ptr animal_factory(int animal_type); \ No newline at end of file