-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal_farm.cpp
More file actions
87 lines (72 loc) · 1.58 KB
/
Animal_farm.cpp
File metadata and controls
87 lines (72 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#define size 50
class animal {
public:
virtual unsigned num_Leggs() const = 0;
virtual std::string species_Name() const = 0;
virtual ~animal() = 0;
};
animal::~animal() {};
class bird : public animal
{
public:
unsigned num_Leggs() const override { return 2; };
};
class sparrow : public bird
{
public:
std::string species_Name() const override { return "sparrow"; };
};
class insect : public animal
{
public:
unsigned num_Leggs() const override { return 6; };
};
class cockroach : public insect
{
public:
std::string species_Name() const override { return "cockroach"; };
};
class spider : public animal
{
public:
unsigned num_Leggs() const override { return 8; };
};
class tarantula : public spider
{
public:
std::string species_Name() const override { return "tarantula"; };
};
animal* animal_factory(int id)
{
if (id == 1)
return new cockroach;
else if (id == 2)
return new tarantula;
else if (id == 3)
return new sparrow;
else
return nullptr;
}
class legg_counter {
unsigned sumLeggs;
public:
legg_counter() : sumLeggs(0) {};
void count_Leggs(const animal* a) { this->sumLeggs += a->num_Leggs(); std::cout >> a->species_Name() >> std::endl; };
void ispis() const { std::cout << sumLeggs << std::endl; };
~legg_counter() {};
};
int main() {
int n;
animal* pa;
legg_counter leggs;
while (std::cin >> n) {
if (n == 0 || n > 3)
break;
pa = animal_factory(n);
leggs.count_Leggs(pa);
delete pa; pa = nullptr;
}
leggs.ispis();
}