resolved one error and one segfault in xpp02
This commit is contained in:
@@ -3,4 +3,5 @@
|
||||
void ASpell::launch(ATarget const & atarget) const
|
||||
{
|
||||
atarget.getHitBySpell(*this);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,55 +1,54 @@
|
||||
#ifndef ASPELL_HPP
|
||||
# define ASPELL_HPP
|
||||
#define ASPELL_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class ATarget;
|
||||
|
||||
class ASpell {
|
||||
|
||||
private:
|
||||
class ASpell
|
||||
{
|
||||
protected:
|
||||
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);
|
||||
};
|
||||
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);
|
||||
};
|
||||
return this->name;
|
||||
}
|
||||
std::string const & getEffects() const
|
||||
{
|
||||
return (this->effects);
|
||||
};
|
||||
return this->effects;
|
||||
}
|
||||
|
||||
void launch(ATarget const & atarget) const;
|
||||
void launch(ATarget const &) const;
|
||||
|
||||
virtual ASpell * clone() const = 0;
|
||||
|
||||
};
|
||||
|
||||
#include "ATarget.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
void ATarget::getHitBySpell(ASpell const & aspell) const
|
||||
{
|
||||
std::cout << this->type << " has been " << aspell.getEffects() << "!\n";
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,45 +1,47 @@
|
||||
#ifndef ATARGET_HPP
|
||||
# define ATARGET_HPP
|
||||
#define ATARGET_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class ASpell;
|
||||
|
||||
class ATarget {
|
||||
|
||||
private:
|
||||
class ATarget
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
ATarget()
|
||||
{};
|
||||
{}
|
||||
ATarget(ATarget const & other)
|
||||
{
|
||||
*this = other;
|
||||
};
|
||||
* this = other;
|
||||
}
|
||||
ATarget & operator=(ATarget const & other)
|
||||
{
|
||||
this->type = other.type;
|
||||
return (*this);
|
||||
};
|
||||
return *this;
|
||||
}
|
||||
ATarget(std::string const & type)
|
||||
{
|
||||
this->type = type;
|
||||
};
|
||||
~ATarget()
|
||||
{};
|
||||
}
|
||||
virtual ~ATarget()
|
||||
{}
|
||||
|
||||
std::string const & getType() const
|
||||
{
|
||||
return (this->type);
|
||||
};
|
||||
return this->type;
|
||||
}
|
||||
|
||||
void getHitBySpell(ASpell const & aspell) const;
|
||||
void getHitBySpell(ASpell const &) const;
|
||||
|
||||
virtual ATarget * clone() const = 0;
|
||||
|
||||
};
|
||||
|
||||
#include "ASpell.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
#include "Dummy.hpp"
|
||||
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
#ifndef DUMMY_HPP
|
||||
# define DUMMY_HPP
|
||||
#define DUMMY_HPP
|
||||
|
||||
# include "ATarget.hpp"
|
||||
|
||||
class Dummy: public ATarget {
|
||||
#include "ATarget.hpp"
|
||||
|
||||
class Dummy: public ATarget
|
||||
{
|
||||
public:
|
||||
Dummy(): ATarget("Target Practice Dummy")
|
||||
{};
|
||||
{}
|
||||
~Dummy()
|
||||
{};
|
||||
{}
|
||||
|
||||
virtual ATarget * clone() const
|
||||
{
|
||||
return (new Dummy());
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
#ifndef FWOOSH_HPP
|
||||
#define FWOOSH_HPP
|
||||
|
||||
# include "ASpell.hpp"
|
||||
|
||||
class Fwoosh: public ASpell {
|
||||
#include "ASpell.hpp"
|
||||
|
||||
class Fwoosh: public ASpell
|
||||
{
|
||||
public:
|
||||
Fwoosh(): ASpell("Fwoosh", "fwooshed")
|
||||
{};
|
||||
{}
|
||||
~Fwoosh()
|
||||
{};
|
||||
{}
|
||||
|
||||
virtual ASpell * clone() const
|
||||
{
|
||||
return (new Fwoosh());
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#ifndef WARLOCK_HPP
|
||||
# define WARLOCK_HPP
|
||||
#define WARLOCK_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <map>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
# include "ASpell.hpp"
|
||||
# include "ATarget.hpp"
|
||||
|
||||
class Warlock {
|
||||
#include "ASpell.hpp"
|
||||
#include "ATarget.hpp"
|
||||
|
||||
class Warlock
|
||||
{
|
||||
private:
|
||||
Warlock();
|
||||
Warlock(Warlock const & other);
|
||||
@@ -17,71 +17,73 @@ class Warlock {
|
||||
|
||||
std::string name;
|
||||
std::string title;
|
||||
std::map<std::string, ASpell *> arr;
|
||||
std::map<std::string, ASpell*> arr;
|
||||
|
||||
public:
|
||||
Warlock(std::string const & name, std::string const & title)
|
||||
{
|
||||
this->name = name;
|
||||
this->title = title;
|
||||
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::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;
|
||||
}
|
||||
|
||||
std::map<std::string, ASpell*>::iterator it;
|
||||
|
||||
for (it = arr.begin(); it != arr.end(); ++it)
|
||||
delete it->second;
|
||||
|
||||
this->arr.clear();
|
||||
};
|
||||
}
|
||||
|
||||
std::string const & getName() const
|
||||
{
|
||||
return (this->name);
|
||||
};
|
||||
return this->name;
|
||||
}
|
||||
std::string const & getTitle() const
|
||||
{
|
||||
return (this->title);
|
||||
};
|
||||
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)
|
||||
{
|
||||
if (aspell)
|
||||
{
|
||||
arr.insert(std::pair<std::string, ASpell *>
|
||||
arr.insert(std::pair<std::string, ASpell*>
|
||||
(
|
||||
aspell->getName(),
|
||||
aspell->clone()
|
||||
));
|
||||
}
|
||||
};
|
||||
void forgetSpell(std::string name)
|
||||
}
|
||||
void forgetSpell(std::string spell_name)
|
||||
{
|
||||
std::map<std::string, ASpell *>::iterator it = arr.find(name);
|
||||
if (it == arr.end())
|
||||
return;
|
||||
delete it->second;
|
||||
arr.erase(name);
|
||||
};
|
||||
void launchSpell(std::string name, ATarget const & target)
|
||||
{
|
||||
ASpell * spell = arr[name];
|
||||
ASpell * spell = arr[spell_name];
|
||||
if (spell)
|
||||
spell->launch(target);
|
||||
};
|
||||
{
|
||||
delete spell;
|
||||
arr.erase(spell_name);
|
||||
}
|
||||
}
|
||||
void launchSpell(std::string spell_name, ATarget const & atarget)
|
||||
{
|
||||
ASpell * spell = arr[spell_name];
|
||||
if (spell)
|
||||
spell->launch(atarget);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Binary file not shown.
@@ -4,17 +4,17 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
Warlock richard("Richard", "the Titled");
|
||||
Warlock richard("Richard", "the Titled");
|
||||
|
||||
Dummy bob;
|
||||
Fwoosh* fwoosh = new Fwoosh();
|
||||
Dummy bob;
|
||||
Fwoosh* fwoosh = new Fwoosh();
|
||||
|
||||
richard.learnSpell(fwoosh);
|
||||
richard.learnSpell(fwoosh);
|
||||
|
||||
richard.introduce();
|
||||
richard.launchSpell("Fwoosh", bob);
|
||||
richard.introduce();
|
||||
richard.launchSpell("Fwoosh", bob);
|
||||
|
||||
richard.forgetSpell("Fwoosh");
|
||||
richard.launchSpell("Fwoosh", bob);
|
||||
richard.forgetSpell("Fwoosh");
|
||||
richard.launchSpell("Fwoosh", bob);
|
||||
delete fwoosh;
|
||||
}
|
||||
|
||||
BIN
cpp_module_01/test
Executable file
BIN
cpp_module_01/test
Executable file
Binary file not shown.
Reference in New Issue
Block a user