From de708af86973d2af866cada89d194780c09fa5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kvesi=C4=87?= Date: Wed, 14 Nov 2018 20:52:01 +0100 Subject: [PATCH] Finished exercise 01, possible memory leak ? --- exercise/01/animal.cpp | 43 ++++++++++++++++++++++ exercise/01/animal.h | 63 ++++++++++++++++++++++++++++++++ exercise/01/animals.vcxproj.user | 4 ++ exercise/01/test.cpp | 3 +- 4 files changed, 111 insertions(+), 2 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..1c9064f --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,43 @@ +#include "animal.h" + +leg_counter::leg_counter() : number_of_legs (0) +{ +} + +leg_counter::~leg_counter() +{ +} + +animal& leg_counter::add_animal (animal& a) +{ + number_of_legs += a.legs(); + return a; +} + +unsigned int leg_counter::legs() +{ + return number_of_legs; +} + +animal& animal_factory(int id) +{ + animal* a = nullptr; + + /*----------------------------------memory leak ???????------------------------------ + */ + switch (id) + { + case 1: + a = new cockroach; + break; + case 2: + a = new sparrow; + break; + case 3: + a = new tarantula; + break; + } + + return *a; + +} \ No newline at end of file diff --git a/exercise/01/animal.h b/exercise/01/animal.h new file mode 100644 index 0000000..766a93c --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,63 @@ +#pragma once +class animal +{ +public: + virtual animal& species() = 0; + virtual const wchar_t* c_str() = 0; + virtual const unsigned int legs() = 0; +}; + +class insect : public animal +{ +public: + //virtual const wchar_t *c_str() override { return L"insect"; } + animal& species() override { return *this; } +}; + +class bird : public animal +{ +public: + //virtual const wchar_t* c_str() override { return L"bird";} + animal& species() override { return *this; } +}; + +class spider : public animal +{ +public: + //virtual const wchar_t* c_str() override { return L"spider"; } + animal& species() override { return *this; } +}; + +class cockroach : public insect +{ +public: + const wchar_t* c_str() override { return L"cockroach"; } + const unsigned int legs() override { return 6u; } +}; + +class sparrow : public bird +{ +public: + const wchar_t* c_str() override { return L"sparrow"; } + const unsigned int legs() override { return 2u; } +}; + +class tarantula : public spider +{ +public: + const wchar_t* c_str() override { return L"tarantula"; } + const unsigned int legs() override { return 8u; } +}; + +class leg_counter +{ +private: + unsigned int number_of_legs; +public: + leg_counter(); + ~leg_counter(); + animal& add_animal(animal& a); + unsigned int legs(); +}; + +animal& animal_factory(int id); \ 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..efd6cfa 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -47,5 +47,4 @@ TEST_CLASS(test_animal_hierarchy) lc.add_animal(animal_factory(2)); Assert::AreEqual(4u, lc.legs()); } - -}; +}; \ No newline at end of file