one implementation of the exam

This commit is contained in:
hugogogo
2022-12-01 15:47:19 +01:00
parent 16dcd06da6
commit c91d23a5d4
43 changed files with 925 additions and 0 deletions

5
cpp_module_01/ASpell.cpp Normal file
View File

@@ -0,0 +1,5 @@
#include "ASpell.hpp"
void ASpell::launch(ATarget const & atarget) const {
atarget.getHitBySpell(*this);
};

45
cpp_module_01/ASpell.hpp Normal file
View 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

View File

@@ -0,0 +1,5 @@
#include "ATarget.hpp"
void ATarget::getHitBySpell(ASpell const & aspell) const {
std::cout << this->type << " has been " << aspell.getEffects() << "!\n";
};

38
cpp_module_01/ATarget.hpp Normal file
View File

@@ -0,0 +1,38 @@
#ifndef ATARGET_HPP
# define ATARGET_HPP
# include <iostream>
# include <string>
class ASpell;
class ATarget {
private:
std::string type;
public:
ATarget() {
};
ATarget(ATarget const & other) {
*this = other;
};
ATarget & operator=(ATarget const & other) {
this->type = other.type;
return (*this);
};
ATarget(std::string const & type) {
this->type = type;
};
~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_01/Dummy.cpp Normal file
View File

@@ -0,0 +1,2 @@
#include "Dummy.hpp"

16
cpp_module_01/Dummy.hpp Normal file
View 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_01/Fwoosh.cpp Normal file
View File

@@ -0,0 +1,2 @@
#include "Fwoosh.hpp"

16
cpp_module_01/Fwoosh.hpp Normal file
View 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

View File

@@ -0,0 +1,2 @@
#include "Warlock.hpp"

67
cpp_module_01/Warlock.hpp Normal file
View File

@@ -0,0 +1,67 @@
#ifndef WARLOCK_HPP
# define WARLOCK_HPP
# include <iostream>
# include <string>
# include "ASpell.hpp"
# include "ATarget.hpp"
# include <map>
class Warlock {
private:
Warlock();
Warlock(Warlock const & other);
Warlock & operator=(Warlock const & other);
std::string name;
std::string title;
std::map<std::string, ASpell *> arr;
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::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();
};
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) {
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);
};
void launchSpell(std::string name, ATarget const & target) {
ASpell * spell = arr[name];
if (spell)
spell->launch(target);
};
};
#endif

BIN
cpp_module_01/a.out Executable file

Binary file not shown.

20
cpp_module_01/main.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "Warlock.hpp"
#include "Fwoosh.hpp"
#include "Dummy.hpp"
int main()
{
Warlock richard("Richard", "the Titled");
Dummy bob;
Fwoosh* fwoosh = new Fwoosh();
richard.learnSpell(fwoosh);
richard.introduce();
richard.launchSpell("Fwoosh", bob);
richard.forgetSpell("Fwoosh");
richard.launchSpell("Fwoosh", bob);
delete fwoosh;
}

View File

@@ -0,0 +1,94 @@
Assignment name : cpp01_02
Expected files : Warlock.cpp Warlock.hpp
ASpell.hpp ASpell.cpp
ATarget.hpp ATarget.cpp
Fwoosh.hpp Fwoosh.cpp
Dummy.hpp Dummy.cpp
--------------------------------------------------------------------------------
In the Warlock class, the switch statement is FORBIDDEN and its use would
result in a -42.
Create an abstract class called ASpell, in Coplien's form, that has the
following protected attributes:
* name (string)
* effects (string)
Both will have getters (getName and getEffects) that return strings.
Also add a clone pure method that returns a pointer to ASpell.
All these functions can be called on a constant object.
ASpell has a constructor that takes its name and its effects, in that order.
Now you will create an ATarget abstract class, in Coplien's form. It has a type
attribute, which is a string, and its associated getter, getType, that return a
reference to constant string.
In much the same way as ASpell, it has a clone() pure method.
All these functions can be called on a constant object.
It has a constructor that takes its type.
Now, add to your ATarget a getHitBySpell function that takes a reference to
constant ASpell.
It will display :
<TYPE> has been <EFFECTS>!
<TYPE> is the ATarget's type, and <EFFECTS> is the return of the ASpell's
getEffects function.
Finally, add to your ASpell class a launch function that takes a reference to
constant ATarget.
This one will simply call the getHitBySpell of the passed object, passing the
current instance as parameter.
When all this is done, create an implementation of ASpell called Fwoosh. Its
default constructor will set the name to "Fwoosh" and the effects to
"fwooshed". You will, of course, implement the clone() method. In the case of
Fwoosh, it will return a pointer to a new Fwoosh object.
In the same way, create a concrete ATarget called Dummy, the type of which
is "Target Practice Dummy". You must also implement its clone() method.
Add to the Warlock the following member functions:
* learnSpell, takes a pointer to ASpell, that makes the Warlock learn a spell
* forgetSpell, takes a string corresponding a to a spell's name, and makes the
Warlock forget it. If it's not a known spell, does nothing.
* launchSpell, takes a string (a spell name) and a reference to ATarget, that
launches the spell on the selected target. If it's not a known spell, does
nothing.
You will need a new attribute to store the spells your Warlock knows. Several
types fit the bill, it's up to you to choose the best one.
Below is a possible test main and its expected output:
int main()
{
Warlock richard("Richard", "the Titled");
Dummy bob;
Fwoosh* fwoosh = new Fwoosh();
richard.learnSpell(fwoosh);
richard.introduce();
richard.launchSpell("Fwoosh", bob);
richard.forgetSpell("Fwoosh");
richard.launchSpell("Fwoosh", bob);
}
~$ ./a.out | cat -e
Richard: This looks like another boring day.$
Richard: I am Richard, the Titled!$
Target Practice Dummy has been fwooshed!$
Richard: My job here is done!$

BIN
cpp_module_01/testt Executable file

Binary file not shown.