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
63 changes: 63 additions & 0 deletions exercise/01/animal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "animal.h"
#include <exception>

animal::~animal() { }

const std::wstring cockroach::type = L"cockroach";

std::wstring cockroach::species() const
{
return type;
}

unsigned int insect::legs() const
{
return number_of_legs;
}

const std::wstring sparrow::type = L"sparrow";

std::wstring sparrow::species() const
{
return type;
}

unsigned int bird::legs() const
{
return number_of_legs;
}

const std::wstring tarantula::type = L"tarantula";

std::wstring tarantula::species() const
{
return type;
}

unsigned int spider::legs() const
{
return number_of_legs;
}

std::wstring leg_counter::add_animal(const animal_factory & factory)
{
std::unique_ptr<animal> animal = factory.create_animal();
number_of_legs += animal->legs();
return animal->species();
}

unsigned int leg_counter::legs() const
{
return number_of_legs;
}

std::unique_ptr<animal> animal_factory::create_animal() const
{
switch (animal_num)
{
case 1: return std::make_unique<cockroach>();
case 2: return std::make_unique<sparrow>();
case 3: return std::make_unique<tarantula>();
}
throw std::invalid_argument("There is no animal with that number!");
}
84 changes: 84 additions & 0 deletions exercise/01/animal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once
#include <string>

class animal
{
public:
virtual std::wstring species() const = 0;
virtual unsigned int legs() const = 0;
virtual ~animal() = 0;
};

class insect : public animal
{
public:
~insect() {};
unsigned int legs() const override;
private:
static const unsigned int number_of_legs = 6;
};

class bird : public animal
{
public:
~bird() {};
unsigned int legs() const override;
private:
static const unsigned int number_of_legs = 2;
};

class spider : public animal
{
public:
~spider() {};
unsigned int legs() const override;
private:
static const unsigned int number_of_legs = 8;
};

class cockroach : public insect
{
public:
~cockroach() {};
std::wstring species() const override;
private:
static const std::wstring type;
};

class sparrow : public bird
{
public:
~sparrow() {};
std::wstring species() const override;
private:
static const std::wstring type;
};

class tarantula : public spider
{
public:
~tarantula() {};
std::wstring species() const override;
private:
static const std::wstring type;
};

class animal_factory
{
public:
animal_factory(unsigned int animal_num) : animal_num(animal_num) {};
std::unique_ptr<animal> create_animal() const;

private:
unsigned int animal_num;
};

class leg_counter
{
public:
std::wstring add_animal(const animal_factory& factory);
unsigned int legs() const;

private:
unsigned int number_of_legs = 0;
};