C++ - Module 04: Subtype Polymorphism, Abstract Classes, Interfaces
C++ - Module 04: Subtype Polymorphism, Abstract Classes, Interfaces
Summary: This document contains the subject for Module 04 of the C++ modules.
Version: 10
Contents
I General rules 2
1
Chapter I
General rules
For the C++ modules you will use and learn C++98 only. The goal is for you to learn
the basic of an object oriented programming language. We know modern C++ is way
different in a lot of aspects so if you want to become a proficient C++, developer you
will need modern standard C++ later on. This will be the starting point of your C++
journey it’s up to you to go further after the 42 Common Core!
• Any function implemented in a header (except in the case of templates), and any
unprotected header means 0 to the exercise.
• Every output goes to the standard output and will be ended by a newline, unless
specified otherwise.
• The imposed filenames must be followed to the letter, as well as class names, func-
tion names, and method names.
◦ The following functions are FORBIDDEN, and their use will be punished by
a 0, no questions asked: *alloc, *printf and free.
◦ You are allowed to use everything in the standard library. HOWEVER, it
would be smart to try and use the C++-ish versions of the functions you are
used to in C, instead of just keeping to what you know, this is a new language
after all. And NO, you are not allowed to use the STL until you are supposed
to (that is, until module 08). That means no vectors/lists/maps/etc... or
anything that requires an include <algorithm> until then.
• Actually, the use of any explicitly forbidden function or mechanic will be punished
by a 0, no questions asked.
• Also note that unless otherwise stated, the C++ keywords "using namespace" and
"friend" are forbidden. Their use will be punished by a -42, no questions asked.
2
C++ - Module 04 Subtype polymorphism, abstract classes, interfaces
• You must read the examples thoroughly. They can contain requirements that are
not obvious in the exercise’s description.
• Since you are allowed to use the C++ tools you learned about since the beginning,
you are not allowed to use any external library. And before you ask, that also means
no C++11 and derivatives, nor Boost or anything else.
• You may be required to turn in an important number of classes. This can seem
tedious unless you’re able to script your favorite text editor.
• Your code has to be compiled with the following flags : -Wall -Wextra -Werror.
• In case you’re wondering, no coding style is enforced during in C++. You can use
any style you like, no restrictions. But remember that a code your peer-evaluator
can’t read is a code they can’t grade.
• Important stuff now: You will NOT be graded by a program, unless explicitly stated
in the subject. Therefore, you are afforded a certain amount of freedom in how you
choose to do the exercises. However, be mindful of the constraints of each exercise,
and DO NOT be lazy, you would miss a LOT of what they have to offer
• It’s not a problem to have some extraneous files in what you turn in, you may
choose to separate your code in more files than what’s asked of you. Feel free, as
long as the result is not graded by a program.
• Even if the subject of an exercise is short, it’s worth spending some time on it to
be sure you understand what’s expected of you, and that you did it in the best
possible way.
3
Chapter II
Exercise : 00
Polymorphism
Turn-in directory : ex00/
Files to turn in : Makefile, main.cpp, Every other files you need
Forbidden functions : None
int main()
{
const Animal* meta = new Animal();
const Animal* j = new Dog();
const Animal* i = new Cat();
4
C++ - Module 04 Subtype polymorphism, abstract classes, interfaces
...
}
This should output the specific makeSound of the Dog and cat class, not the animal
one.
To be sure you will create a WrongCat class that inherits a WrongAnimal class that will
output the WrongAnimal makeSound() when test under the same conditions.
5
Chapter III
Exercise : 01
Upon construction Dog and Cat will initialize their Brain* with a new Brain();
Upon destruction Dog and Cat will delete their Brain.
Your main will create and fill an Array of Animal half of it will be Dog and the other
half will be Cat.
Before exit, your main will loop over this array and delete every Animal.
You must delete directly Cat and Dog as an Animal.
6
C++ - Module 04 Subtype polymorphism, abstract classes, interfaces
int main()
{
const Animal* j = new Dog();
const Animal* i = new Cat();
7
Chapter IV
Exercise : 02
8
Chapter V
Exercise : 03
There’s no interface in C++98 (not even in C++20) but it’s common to call pure abstract
class Interface. So for this last exercise let’s try interfaces and recap everything!
Complete the definition of the following AMateria class, and implement the neces-
sary member functions.
class AMateria
{
protected:
[...]
public:
AMateria(std::string const & type);
[...]
Create the concrete Materias Ice and Cure . Their type will be their name in
lowercase ("ice" for Ice, etc...).
Their clone() method will, of course, return a new instance of the real Materia’s
type.
9
C++ - Module 04 Subtype polymorphism, abstract classes, interfaces
(Of course, replace NAME by the name of the Character given as parameter.)
Create the Character class, which will implement the following interface:
class ICharacter
{
public:
virtual ~ICharacter() {}
virtual std::string const & getName() const = 0;
virtual void equip(AMateria* m) = 0;
virtual void unequip(int idx) = 0;
virtual void use(int idx, ICharacter& target) = 0;
};
The Character possesses an inventory of 4 Materia at most, empty at the start. He’ll
equip the Materia in slots 0 to 3, in this order.
The use(int, ICharacter&) method will have to use the Materia at the idx slot,
and pass target as parameter to the AMateria::use method.
Your Character must have a constructor taking its name as a parameter. Copy or
assignation of a Character must be deep, of course. The old Materia of a Character
must be deleted. Same upon the destruction of a Character .
10
C++ - Module 04 Subtype polymorphism, abstract classes, interfaces
Create the MateriaSource class, which will have to implement the following inter-
face:
class IMateriaSource
{
public:
virtual ~IMateriaSource() {}
virtual void learnMateria(AMateria*) = 0;
virtual AMateria* createMateria(std::string const & type) = 0;
};
learnMateria must copy the Materia passed as a parameter, and store it in memory
to be cloned later. Much in the same way as for Character , the Source can know at
most 4 Materia, which are not necessarily unique.
In a nutshell, your Source must be able to learn "templates" of Materia and re-create
them on demand. You’ll then be able to create a Materia without knowing its "real" type,
just a string identifying it.
11
C++ - Module 04 Subtype polymorphism, abstract classes, interfaces
int main()
{
IMateriaSource* src = new MateriaSource();
src->learnMateria(new Ice());
src->learnMateria(new Cure());
AMateria* tmp;
tmp = src->createMateria("ice");
me->equip(tmp);
tmp = src->createMateria("cure");
me->equip(tmp);
me->use(0, *bob);
me->use(1, *bob);
delete bob;
delete me;
delete src;
return 0;
}
Output:
12