Skip to content

Commit 6314fd0

Browse files
committed
Interfaces
1 parent e79537d commit 6314fd0

File tree

8 files changed

+93
-2
lines changed

8 files changed

+93
-2
lines changed

08_CatsAndDogs/08_CatsAndDogs.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@
157157
<ItemGroup>
158158
<ClInclude Include="Cat.hpp" />
159159
<ClInclude Include="Dog.hpp" />
160+
<ClInclude Include="IInteractable.hpp" />
160161
<ClInclude Include="Pet.hpp" />
162+
<ClInclude Include="Toy.hpp" />
161163
</ItemGroup>
162164
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
163165
<ImportGroup Label="ExtensionTargets">

08_CatsAndDogs/08_CatsAndDogs.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@
3232
<ClInclude Include="Dog.hpp">
3333
<Filter>Header Files</Filter>
3434
</ClInclude>
35+
<ClInclude Include="IInteractable.hpp">
36+
<Filter>Header Files</Filter>
37+
</ClInclude>
38+
<ClInclude Include="Toy.hpp">
39+
<Filter>Header Files</Filter>
40+
</ClInclude>
3541
</ItemGroup>
3642
</Project>

08_CatsAndDogs/Cat.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace PetManager
66
{
7-
class Cat : public Pet
7+
class Cat :
8+
public Pet,
9+
public Pet::ToyInteractable
810
{
911
public:
1012
using Pet::Pet;
@@ -13,5 +15,17 @@ namespace PetManager
1315
{
1416
return "Cat";
1517
}
18+
19+
std::string Interact(Toy& toy) override
20+
{
21+
if (dynamic_cast<ToyMouse*>(&toy))
22+
{
23+
return "Jump!";
24+
}
25+
else
26+
{
27+
return "Looks confused...";
28+
}
29+
}
1630
};
1731
}

08_CatsAndDogs/Dog.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace PetManager
66
{
7-
class Dog : public Pet
7+
class Dog :
8+
public Pet,
9+
public Pet::ToyInteractable
810
{
911
public:
1012
using Pet::Pet;
@@ -13,5 +15,19 @@ namespace PetManager
1315
{
1416
return "Dog";
1517
}
18+
19+
std::string Interact(Toy& toy) override
20+
{
21+
if (dynamic_cast<ToyMouse*>(&toy))
22+
{
23+
return "Barks!";
24+
}
25+
else if (dynamic_cast<ToyBone*>(&toy))
26+
{
27+
return "Chewing...";
28+
}
29+
30+
return "Looks confused...";
31+
}
1632
};
1733
}

08_CatsAndDogs/IInteractable.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace PetManager
6+
{
7+
template<typename Return, typename... Args>
8+
class IInteractable
9+
{
10+
public:
11+
virtual ~IInteractable() = default;
12+
13+
virtual Return Interact(Args... args) = 0;
14+
};
15+
}

08_CatsAndDogs/Pet.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include "Toy.hpp"
4+
#include "IInteractable.hpp"
5+
36
#include <string>
47
#include <ostream>
58
#include <string_view>
@@ -11,6 +14,10 @@ namespace PetManager
1114
{
1215
class Pet
1316
{
17+
public:
18+
using ToyInteractable = IInteractable<std::string, Toy&>;
19+
using WaterInteractable = IInteractable<std::string, float /* Water speed */, float /* Water depth */>;
20+
1421
public:
1522
Pet(const std::string_view& name);
1623
virtual ~Pet() = default;

08_CatsAndDogs/Toy.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
namespace PetManager
4+
{
5+
struct Toy
6+
{
7+
virtual ~Toy() = default;
8+
9+
float apeal = 1.0f;
10+
};
11+
12+
struct ToyBone : Toy
13+
{
14+
float durability = 3.0f;
15+
};
16+
17+
struct ToyMouse : Toy
18+
{
19+
float durability = 1.5f;
20+
};
21+
}

08_CatsAndDogs/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ int main()
1010
Cat tabby("Tabby");
1111
Dog oscar("Oscar");
1212

13+
ToyBone bone;
14+
ToyMouse mouse;
15+
16+
std::cout << "Playing with our pets: " << std::endl;
17+
std::cout << oscar.GetName() << " gets the bone: " << oscar.Interact(bone) << std::endl;
18+
std::cout << oscar.GetName() << " gets the mouse: " << oscar.Interact(mouse) << std::endl;
19+
std::cout << tabby.GetName() << " gets the bone: " << tabby.Interact(bone) << std::endl;
20+
std::cout << tabby.GetName() << " gets the mouse: " << tabby.Interact(mouse) << std::endl;
21+
std::cout << std::endl;
22+
1323
tabby.Lived(); oscar.Lived();
1424
tabby.Lived(); oscar.Lived();
1525
tabby.Lived(); oscar.Lived();

0 commit comments

Comments
 (0)