Day 9
Day 9
All output goes to the standard output, and must be ended by a newline, unless specified oth-
erwise.
None of your files must contain a main function, unless specified otherwise. We will use our
own main functions to compile and test your code. It will include your header files.
There are no subdirectories to create for each exercice. Every file must be at the root of
the repository.
Read the examples CAREFULLY. They might require things that weren’t mentioned in the
subject. . .
The *alloc, free, *printf, open and fopen functions, as well as the using namespace keyword, are for-
bidden in C++. By the way, friend is forbidden too, as well as any library except the standard
one.
Unit Tests
It is highly recommended to test your functions as you implement them. It is common practice
to create and use what are called unit tests.
From now on, we expect you to write unit tests for your functions (when possible). To do so, please
follow the instructions in the “How to write Unit Tests” document on the intranet, available
here.
For them to be executed and evaluated, put a Makefile at the root of your directory with the tests
_runrule as mentionned in the documentation linked above.
1
Exercise 0 - The Peasant
int attack()
int special()
void rest()
2
As strong as they are, heroes can also take damage :
void damage ( int damage )
If a Peasant takes too much damage and its health points reach 0, the peasant screams :
[ name ] is out of combat .
∇ Terminal - + x
~/B-PDG-300> g++ -std=c++20 -Wall -Wextra -Werror *.cpp && ./a.out | cat -e
Gildas goes for an adventure.$
Gildas takes 50 damage.$
Gildas is out of combat.$
Gildas is out of combat.$
Gildas is out of combat.$
Gildas is back to his crops.$
3
Exercise 1 - The Knight
int attack()
int special()
void rest()
4
Here is a sample main function and its expected output :
int main ( void )
{
Knight knight ( " Arthur " , 20) ;
knight . attack () ;
knight . special () ;
knight . rest () ;
knight . special () ;
knight . damage (50) ;
}
∇ Terminal - + x
~/B-PDG-300> g++ -std=c++20 -Wall -Wextra -Werror *.cpp && ./a.out | cat -e
Arthur goes for an adventure.$
Arthur vows to protect the kingdom.$
Arthur strikes with his sword.$
Arthur is out of power.$
Arthur eats.$
Arthur impales his enemy.$
Arthur takes 50 damage.$
Arthur takes off his armor.$
Arthur is back to his crops.$
5
Exercise 2 - The Enchanter
int attack()
int special()
void rest()
6
Here is a sample main function and its expected output :
int main ( void )
{
Enchanter enchanter ( " Merlin " , 20) ;
enchanter . attack () ;
enchanter . special () ;
enchanter . rest () ;
enchanter . special () ;
enchanter . damage (50) ;
}
∇ Terminal - + x
~/B-PDG-300> g++ -std=c++20 -Wall -Wextra -Werror *.cpp && ./a.out | cat -e
Merlin goes for an adventure.$
Merlin learns magic from his spellbook.$
Merlin doesn’t know how to fight.$
Merlin is out of power.$
Merlin meditates.$
Merlin casts a fireball.$
Merlin takes 50 damage.$
Merlin closes his spellbook.$
Merlin is back to his crops.$
7
Exercise 3 - The Priest
void rest()
8
Here is a sample main function and its expected output :
int main ( void )
{
Priest priest ( " Trichelieu " , 20) ;
priest . attack () ;
priest . special () ;
priest . rest () ;
priest . damage (50) ;
}
∇ Terminal - + x
~/B-PDG-300> g++ -std=c++20 -Wall -Wextra -Werror *.cpp && ./a.out | cat -e
Trichelieu goes for an adventure.$
Trichelieu learns magic from his spellbook.$
Trichelieu enters in the order.$
Trichelieu doesn’t know how to fight.$
Trichelieu is out of power.$
Trichelieu prays.$
Trichelieu takes 50 damage.$
Trichelieu finds peace.$
Trichelieu closes his spellbook.$
Trichelieu is back to his crops.$
9
Exercise 4 - The Paladin
Create a Paladin class that inherits from both the Knight and Priest class.
Just like a Peasant, a Paladin must be constructed with a name and power points.
A Paladin uses technique from various classes :
10
Here is a sample main function and its expected output :
int main ( void )
{
Paladin paladin ( " Uther " , 99) ;
paladin . attack () ;
paladin . special () ;
paladin . rest () ;
paladin . damage (50) ;
}
∇ Terminal - + x
~/B-PDG-300> g++ -std=c++20 -Wall -Wextra -Werror *.cpp && ./a.out | cat -e
Uther goes for an adventure.$
Uther vows to protect the kingdom.$
Uther learns magic from his spellbook.$
Uther enters in the order.$
Uther fights for the light.$
Uther strikes with his sword.$
Uther casts a fireball.$
Uther prays.$
Uther takes 50 damage.$
Uther is blessed.$
Uther finds peace.$
Uther closes his spellbook.$
Uther takes off his armor.$
Uther is back to his crops.$
11
Exercise 5 - The Character Interface
delete peasant ;
delete knight ;
delete enchanter ;
delete priest ;
delete paladin ;
}
12
∇ Terminal - + x
~/B-PDG-300> g++ -std=c++20 -Wall -Wextra -Werror *.cpp && ./a.out | cat -e
Gildas goes for an adventure.$
Arthur goes for an adventure.$
Arthur vows to protect the kingdom.$
Merlin goes for an adventure.$
Merlin learns magic from his spellbook.$
Trichelieu goes for an adventure.$
Trichelieu learns magic from his spellbook.$
Trichelieu enters in the order.$
Uther goes for an adventure.$
Uther vows to protect the kingdom.$
Uther learns magic from his spellbook.$
Uther enters in the order.$
Uther fights for the light.$
Gildas tosses a stone.$
Arthur is out of power.$
Merlin meditates.$
Trichelieu takes 21 damage.$
Uther: 100 health points, 99 power points.$
Gildas is back to his crops.$
Arthur takes off his armor.$
Arthur is back to his crops.$
Merlin closes his spellbook.$
Merlin is back to his crops.$
Trichelieu finds peace.$
Trichelieu closes his spellbook.$
Trichelieu is back to his crops.$
Uther is blessed.$
Uther finds peace.$
Uther closes his spellbook.$
Uther takes off his armor.$
Uther is back to his crops.$
13
Exercise 6 - The Potions
Characters can drink a potion even when out of combat. Potions can be used multiple
times.
14
Here is a sample main function and its expected output :
int main ( void )
{
ICharacter * peasant = new Peasant ( " Gildas " , 42) ;
PoisonPotion poison_potion ;
HealthPotion health_potion ;
IPotion & potion = health_potion ;
std :: cout << peasant - > getName () << " : " << peasant - > getHp () << " HP , "
<< peasant - > getPower () << " PP . " << std :: endl ;
peasant - > drink ( poison_potion ) ;
std :: cout << peasant - > getName () << " : " << peasant - > getHp () << " HP , "
<< peasant - > getPower () << " PP . " << std :: endl ;
peasant - > drink ( potion ) ;
std :: cout << peasant - > getName () << " : " << peasant - > getHp () << " HP , "
<< peasant - > getPower () << " PP . " << std :: endl ;
delete peasant ;
}
∇ Terminal - + x
~/B-PDG-300> g++ -std=c++20 -Wall -Wextra -Werror *.cpp && ./a.out | cat -e
Gildas goes for an adventure.$
Gildas: 100HP, 42 PP.$
Gildas has been poisoned.$
Gildas: 50HP, 42 PP.$
Gildas drinks a mysterious potion.$
Gildas: 100HP, 42 PP.$
Gildas is back to his crops.$
15
v3