Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions exercise/01/animal.cpp
Original file line number Diff line number Diff line change
@@ -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;

}
63 changes: 63 additions & 0 deletions exercise/01/animal.h
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 4 additions & 0 deletions exercise/01/animals.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
3 changes: 1 addition & 2 deletions exercise/01/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ TEST_CLASS(test_animal_hierarchy)
lc.add_animal(animal_factory(2));
Assert::AreEqual(4u, lc.legs());
}

};
};