one implementation of the exam
This commit is contained in:
5
cpp_module_02/ASpell.cpp
Normal file
5
cpp_module_02/ASpell.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "ASpell.hpp"
|
||||
|
||||
void ASpell::launch(ATarget const & atarget) const {
|
||||
atarget.getHitBySpell(*this);
|
||||
};
|
||||
45
cpp_module_02/ASpell.hpp
Normal file
45
cpp_module_02/ASpell.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef ASPELL_HPP
|
||||
# define ASPELL_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
|
||||
class ATarget;
|
||||
|
||||
class ASpell {
|
||||
private:
|
||||
std::string name;
|
||||
std::string effects;
|
||||
|
||||
public:
|
||||
ASpell() {
|
||||
};
|
||||
ASpell(ASpell const & other) {
|
||||
*this = other;
|
||||
};
|
||||
ASpell & operator=(ASpell const & other) {
|
||||
this->name = other.name;
|
||||
this->effects = other.effects;
|
||||
return (*this);
|
||||
};
|
||||
ASpell(std::string const & name, std::string const & effects) {
|
||||
this->name = name;
|
||||
this->effects = effects;
|
||||
};
|
||||
virtual ~ASpell() {
|
||||
};
|
||||
|
||||
std::string const & getName() const {
|
||||
return (this->name);
|
||||
};
|
||||
std::string const & getEffects() const {
|
||||
return (this->effects);
|
||||
};
|
||||
void launch(ATarget const & atarget) const;
|
||||
|
||||
virtual ASpell * clone() const = 0;
|
||||
};
|
||||
|
||||
#include "ATarget.hpp"
|
||||
|
||||
#endif
|
||||
5
cpp_module_02/ATarget.cpp
Normal file
5
cpp_module_02/ATarget.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "ATarget.hpp"
|
||||
|
||||
void ATarget::getHitBySpell(ASpell const & aspell) const {
|
||||
std::cout << this->type << " has been " << aspell.getEffects() << "!\n";
|
||||
};
|
||||
40
cpp_module_02/ATarget.hpp
Normal file
40
cpp_module_02/ATarget.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef ATARGET_HPP
|
||||
# define ATARGET_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
|
||||
class ASpell;
|
||||
|
||||
class ATarget {
|
||||
private:
|
||||
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
ATarget() {
|
||||
};
|
||||
ATarget(std::string const & type) {
|
||||
this->type = type;
|
||||
};
|
||||
ATarget(ATarget const & other) {
|
||||
*this = other;
|
||||
};
|
||||
ATarget & operator=(ATarget const & other) {
|
||||
this->type = other.type;
|
||||
return (*this);
|
||||
};
|
||||
virtual ~ATarget() {};
|
||||
|
||||
std::string const & getType() const {
|
||||
return (this->type);
|
||||
};
|
||||
|
||||
void getHitBySpell(ASpell const & aspell) const;
|
||||
|
||||
virtual ATarget * clone() const = 0;
|
||||
};
|
||||
|
||||
#include "ASpell.hpp"
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/BrickWall.cpp
Normal file
2
cpp_module_02/BrickWall.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "BrickWall.hpp"
|
||||
|
||||
16
cpp_module_02/BrickWall.hpp
Normal file
16
cpp_module_02/BrickWall.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef BRICKWALL_HPP
|
||||
# define BRICKWALL_HPP
|
||||
|
||||
# include "ATarget.hpp"
|
||||
|
||||
class BrickWall: public ATarget {
|
||||
public:
|
||||
BrickWall(): ATarget("Inconspicuous Red-brick Wall") {};
|
||||
~BrickWall() {};
|
||||
|
||||
virtual ATarget * clone() const {
|
||||
return (new BrickWall());
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/Dummy.cpp
Normal file
2
cpp_module_02/Dummy.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "Dummy.hpp"
|
||||
|
||||
16
cpp_module_02/Dummy.hpp
Normal file
16
cpp_module_02/Dummy.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef DUMMY_HPP
|
||||
# define DUMMY_HPP
|
||||
|
||||
# include "ATarget.hpp"
|
||||
|
||||
class Dummy: public ATarget {
|
||||
public:
|
||||
Dummy(): ATarget("Target Practice Dummy") {};
|
||||
~Dummy() {};
|
||||
|
||||
virtual ATarget * clone() const {
|
||||
return (new Dummy());
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/Fireball.cpp
Normal file
2
cpp_module_02/Fireball.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "Fireball.hpp"
|
||||
|
||||
16
cpp_module_02/Fireball.hpp
Normal file
16
cpp_module_02/Fireball.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef FIREBALL_HPP
|
||||
#define FIREBALL_HPP
|
||||
|
||||
# include "ASpell.hpp"
|
||||
|
||||
class Fireball: public ASpell {
|
||||
public:
|
||||
Fireball(): ASpell("Fireball", "burnt to a crisp") {};
|
||||
~Fireball() {};
|
||||
|
||||
virtual ASpell * clone() const {
|
||||
return (new Fireball());
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/Fwoosh.cpp
Normal file
2
cpp_module_02/Fwoosh.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "Fwoosh.hpp"
|
||||
|
||||
16
cpp_module_02/Fwoosh.hpp
Normal file
16
cpp_module_02/Fwoosh.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef FWOOSH_HPP
|
||||
#define FWOOSH_HPP
|
||||
|
||||
# include "ASpell.hpp"
|
||||
|
||||
class Fwoosh: public ASpell {
|
||||
public:
|
||||
Fwoosh(): ASpell("Fwoosh", "fwooshed") {};
|
||||
~Fwoosh() {};
|
||||
|
||||
virtual ASpell * clone() const {
|
||||
return (new Fwoosh());
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/Polymorph.cpp
Normal file
2
cpp_module_02/Polymorph.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "Polymorph.hpp"
|
||||
|
||||
16
cpp_module_02/Polymorph.hpp
Normal file
16
cpp_module_02/Polymorph.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef POLYMORPH_HPP
|
||||
#define POLYMORPH_HPP
|
||||
|
||||
# include "ASpell.hpp"
|
||||
|
||||
class Polymorph: public ASpell {
|
||||
public:
|
||||
Polymorph(): ASpell("Polymorph", "turned into a critter") {};
|
||||
~Polymorph() {};
|
||||
|
||||
virtual ASpell * clone() const {
|
||||
return (new Polymorph());
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/SpellBook.cpp
Normal file
2
cpp_module_02/SpellBook.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "Warlock.hpp"
|
||||
|
||||
50
cpp_module_02/SpellBook.hpp
Normal file
50
cpp_module_02/SpellBook.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef SPELLBOOK_HPP
|
||||
#define SPELLBOOK_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include "ASpell.hpp"
|
||||
# include <map>
|
||||
|
||||
class SpellBook {
|
||||
private:
|
||||
SpellBook(SpellBook const & other);
|
||||
SpellBook & operator=(SpellBook const & other);
|
||||
|
||||
std::map<std::string, ASpell *> arr;
|
||||
|
||||
public:
|
||||
SpellBook() {};
|
||||
~SpellBook() {
|
||||
std::map<std::string, ASpell *>::iterator it_begin = this->arr.begin();
|
||||
std::map<std::string, ASpell *>::iterator it_end = this->arr.end();
|
||||
while (it_begin != it_end) {
|
||||
delete it_begin->second;
|
||||
++it_begin;
|
||||
}
|
||||
this->arr.clear();
|
||||
};
|
||||
|
||||
void learnSpell(ASpell *aspell) {
|
||||
if (aspell)
|
||||
arr.insert(std::pair<std::string, ASpell *>(
|
||||
aspell->getName(),
|
||||
aspell->clone()
|
||||
));
|
||||
};
|
||||
void forgetSpell(std::string & name) {
|
||||
std::map<std::string, ASpell *>::iterator it = arr.find(name);
|
||||
if (it == arr.end())
|
||||
return;
|
||||
delete it->second;
|
||||
arr.erase(name);
|
||||
};
|
||||
ASpell * createSpell(std::string & name) {
|
||||
std::map<std::string, ASpell *>::iterator it = arr.find(name);
|
||||
if (it == arr.end())
|
||||
return NULL;
|
||||
return arr[name];
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/TargetGenerator.cpp
Normal file
2
cpp_module_02/TargetGenerator.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "TargetGenerator.hpp"
|
||||
|
||||
50
cpp_module_02/TargetGenerator.hpp
Normal file
50
cpp_module_02/TargetGenerator.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef TARGETGENERATOR_HPP
|
||||
#define TARGETGENERATOR_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include "ATarget.hpp"
|
||||
# include <map>
|
||||
|
||||
class TargetGenerator {
|
||||
private:
|
||||
TargetGenerator(TargetGenerator const & other);
|
||||
TargetGenerator & operator=(TargetGenerator const & other);
|
||||
|
||||
std::map<std::string, ATarget *> arr;
|
||||
|
||||
public:
|
||||
TargetGenerator() {
|
||||
};
|
||||
~TargetGenerator() {
|
||||
std::map<std::string, ATarget *>::iterator it_begin = this->arr.begin();
|
||||
std::map<std::string, ATarget *>::iterator it_end = this->arr.end();
|
||||
while (it_begin != it_end) {
|
||||
delete it_begin->second;
|
||||
++it_begin;
|
||||
}
|
||||
this->arr.clear();
|
||||
};
|
||||
|
||||
void learnTargetType(ATarget * atarget) {
|
||||
if (atarget)
|
||||
arr.insert(std::pair<std::string, ATarget *>(
|
||||
atarget->getType(),
|
||||
atarget->clone()
|
||||
));
|
||||
};
|
||||
void forgetTargetType(std::string const & name) {
|
||||
std::map<std::string, ATarget *>::iterator it = arr.find(name);
|
||||
if (it != arr.end())
|
||||
delete it->second;
|
||||
arr.erase(name);
|
||||
};
|
||||
ATarget * createTarget(std::string const & name) {
|
||||
std::map<std::string, ATarget *>::iterator it = arr.find(name);
|
||||
if (it != arr.end())
|
||||
return arr[name];
|
||||
return NULL;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
2
cpp_module_02/Warlock.cpp
Normal file
2
cpp_module_02/Warlock.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "SpellBook.hpp"
|
||||
|
||||
71
cpp_module_02/Warlock.hpp
Normal file
71
cpp_module_02/Warlock.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef WARLOCK_HPP
|
||||
# define WARLOCK_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include "ASpell.hpp"
|
||||
# include "ATarget.hpp"
|
||||
# include "SpellBook.hpp"
|
||||
|
||||
class Warlock {
|
||||
private:
|
||||
Warlock();
|
||||
Warlock(Warlock const & other);
|
||||
Warlock & operator=(Warlock const & other);
|
||||
|
||||
std::string name;
|
||||
std::string title;
|
||||
SpellBook book;
|
||||
|
||||
public:
|
||||
Warlock(std::string const & name, std::string const & title)
|
||||
{
|
||||
this->name = name;
|
||||
this->title = title;
|
||||
std::cout << this->name << ": This looks like another boring day.\n";
|
||||
};
|
||||
~Warlock()
|
||||
{
|
||||
std::cout << this->name << ": My job here is done!\n";
|
||||
};
|
||||
|
||||
std::string const & getName() const
|
||||
{
|
||||
return (this->name);
|
||||
};
|
||||
std::string const & getTitle() const
|
||||
{
|
||||
return (this->title);
|
||||
};
|
||||
|
||||
void setTitle(std::string const & title)
|
||||
{
|
||||
this->title = title;
|
||||
};
|
||||
|
||||
void introduce() const
|
||||
{
|
||||
std::cout << this->name << ": I am " << this->name << ", " << this->title << "!\n";
|
||||
};
|
||||
|
||||
void learnSpell(ASpell *aspell)
|
||||
{
|
||||
book.learnSpell(aspell);
|
||||
};
|
||||
void forgetSpell(std::string name)
|
||||
{
|
||||
book.forgetSpell(name);
|
||||
};
|
||||
void launchSpell(std::string name, ATarget const & atarget)
|
||||
{
|
||||
//ATarget const * test = 0;
|
||||
//if (test == & atarget)
|
||||
// return;
|
||||
|
||||
ASpell * spell = book.createSpell(name);
|
||||
if (spell)
|
||||
spell->launch(atarget);
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
cpp_module_02/a.out
Executable file
BIN
cpp_module_02/a.out
Executable file
Binary file not shown.
30
cpp_module_02/main.cpp
Normal file
30
cpp_module_02/main.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "Warlock.hpp"
|
||||
#include "Fwoosh.hpp"
|
||||
#include "Dummy.hpp"
|
||||
#include "BrickWall.hpp"
|
||||
#include "Polymorph.hpp"
|
||||
#include "TargetGenerator.hpp"
|
||||
#include "Fireball.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
Warlock richard("Richard", "foo");
|
||||
richard.setTitle("Hello, I'm Richard the Warlock!");
|
||||
BrickWall model1;
|
||||
|
||||
Polymorph* polymorph = new Polymorph();
|
||||
TargetGenerator tarGen;
|
||||
|
||||
tarGen.learnTargetType(&model1);
|
||||
richard.learnSpell(polymorph);
|
||||
|
||||
Fireball* fireball = new Fireball();
|
||||
|
||||
richard.learnSpell(fireball);
|
||||
|
||||
ATarget* wall = tarGen.createTarget("Inconspicuous Red-brick Wall");
|
||||
|
||||
richard.introduce();
|
||||
richard.launchSpell("Polymorph", *wall);
|
||||
richard.launchSpell("Fireball", *wall);
|
||||
}
|
||||
86
cpp_module_02/subject.en.txt
Normal file
86
cpp_module_02/subject.en.txt
Normal file
@@ -0,0 +1,86 @@
|
||||
Assignment name : cpp_module_02
|
||||
Expected files : Warlock.cpp Warlock.hpp
|
||||
ASpell.hpp ASpell.cpp
|
||||
ATarget.hpp ATarget.cpp
|
||||
Fwoosh.hpp Fwoosh.cpp
|
||||
Dummy.hpp Dummy.cpp
|
||||
Fireball.hpp Fireball.cpp
|
||||
Polymorph.hpp Polymorph.cpp
|
||||
BrickWall.hpp BrickWall.cpp
|
||||
SpellBook.hpp SpellBook.cpp
|
||||
TargetGenerator.hpp TargetGenerator.cpp
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
In the Warlock, SpellBook and TargetGenerator classes, the switch statement is
|
||||
FORBIDDEN and its use would result in a -42.
|
||||
|
||||
Create the following two spells, on the same model as Fwoosh:
|
||||
|
||||
* Fireball (Name: "Fireball", Effects: "burnt to a crisp")
|
||||
* Polymorph (Name: "Polymorph", Effects: "turned into a critter")
|
||||
|
||||
In addition to this, just so he won't have only dummy to attack, let's make a
|
||||
new target for him, which will be the BrickWall (Type: "Inconspicuous Red-brick Wall").
|
||||
|
||||
Now, make a SpellBook class, in canonical form, that can't be copied or instantiated
|
||||
by copy. It will have the following functions:
|
||||
|
||||
* void learnSpell(ASpell*), that COPIES a spell in the book
|
||||
* void forgetSpell(string const &), that deletes a spell from the book, except
|
||||
if it isn't there
|
||||
* ASpell* createSpell(string const &), that receives a string corresponding to
|
||||
the name of a spell, creates it, and returns it.
|
||||
|
||||
Modify the Warlock, now, make it have a spell book that will be created with
|
||||
him and destroyed with him. Also make his learnSpell and forgetSpell functions
|
||||
call those of the spell book.
|
||||
|
||||
The launchSpell function will have to use the SpellBook to create the spell
|
||||
it's attempting to launch.
|
||||
|
||||
Make a TargetGenerator class, in canonical form, and as before,
|
||||
non-copyable.
|
||||
|
||||
It will have the following functions:
|
||||
|
||||
* void learnTargetType(ATarget*), teaches a target to the generator
|
||||
|
||||
* void forgetTargetType(string const &), that makes the generator forget a
|
||||
target type if it's known
|
||||
|
||||
* ATarget* createTarget(string const &), that creates a target of the
|
||||
specified type
|
||||
|
||||
Phew, that's done. Now here's a test main. It's not very thorough, so make sure
|
||||
to use your own aswell.
|
||||
|
||||
int main()
|
||||
{
|
||||
Warlock richard("Richard", "foo");
|
||||
richard.setTitle("Hello, I'm Richard the Warlock!");
|
||||
BrickWall model1;
|
||||
|
||||
Polymorph* polymorph = new Polymorph();
|
||||
TargetGenerator tarGen;
|
||||
|
||||
tarGen.learnTargetType(&model1);
|
||||
richard.learnSpell(polymorph);
|
||||
|
||||
Fireball* fireball = new Fireball();
|
||||
|
||||
richard.learnSpell(fireball);
|
||||
|
||||
ATarget* wall = tarGen.createTarget("Inconspicuous Red-brick Wall");
|
||||
|
||||
richard.introduce();
|
||||
richard.launchSpell("Polymorph", *wall);
|
||||
richard.launchSpell("Fireball", *wall);
|
||||
}
|
||||
|
||||
~$ ./a.out | cat -e
|
||||
Richard: This looks like another boring day.$
|
||||
Richard: I am Richard, Hello, I'm Richard the Warlock!!$
|
||||
Inconspicuous Red-brick Wall has been turned into a critter!$
|
||||
Inconspicuous Red-brick Wall has been burnt to a crisp!$
|
||||
Richard: My job here is done!$
|
||||
~$
|
||||
Reference in New Issue
Block a user