Browse Source

cpp3-5

master
narnaud 3 years ago
parent
commit
42fd870ab0
  1. 4
      CPP03/ex00/ClapTrap.hpp
  2. 4
      CPP03/ex01/ClapTrap.hpp
  3. 11
      CPP03/ex01/ScavTrap.cpp
  4. 4
      CPP03/ex02/ClapTrap.hpp
  5. 19
      CPP03/ex02/FragTrap.cpp
  6. 3
      CPP03/ex02/FragTrap.hpp
  7. 11
      CPP03/ex02/ScavTrap.cpp
  8. 3
      CPP03/ex02/main.cpp
  9. 43
      CPP04/ex00/Animal.cpp
  10. 32
      CPP04/ex00/Animal.hpp
  11. 24
      CPP04/ex00/Cat.cpp
  12. 22
      CPP04/ex00/Cat.hpp
  13. 24
      CPP04/ex00/Dog.cpp
  14. 21
      CPP04/ex00/Dog.hpp
  15. 20
      CPP04/ex00/Makefile
  16. 42
      CPP04/ex00/WrongAnimal.cpp
  17. 32
      CPP04/ex00/WrongAnimal.hpp
  18. 24
      CPP04/ex00/WrongCat.cpp
  19. 22
      CPP04/ex00/WrongCat.hpp
  20. 32
      CPP04/ex00/main.cpp
  21. 43
      CPP04/ex01/Animal.cpp
  22. 32
      CPP04/ex01/Animal.hpp
  23. 33
      CPP04/ex01/Brain.cpp
  24. 28
      CPP04/ex01/Brain.hpp
  25. 26
      CPP04/ex01/Cat.cpp
  26. 25
      CPP04/ex01/Cat.hpp
  27. 26
      CPP04/ex01/Dog.cpp
  28. 24
      CPP04/ex01/Dog.hpp
  29. 20
      CPP04/ex01/Makefile
  30. 32
      CPP04/ex01/main.cpp
  31. 43
      CPP04/ex02/AAnimal.cpp
  32. 32
      CPP04/ex02/AAnimal.hpp
  33. 33
      CPP04/ex02/Brain.cpp
  34. 28
      CPP04/ex02/Brain.hpp
  35. 26
      CPP04/ex02/Cat.cpp
  36. 25
      CPP04/ex02/Cat.hpp
  37. 26
      CPP04/ex02/Dog.cpp
  38. 24
      CPP04/ex02/Dog.hpp
  39. 20
      CPP04/ex02/Makefile
  40. 32
      CPP04/ex02/main.cpp
  41. 57
      CPP05/ex00/Bureaucrat.cpp
  42. 51
      CPP05/ex00/Bureaucrat.hpp
  43. 20
      CPP05/ex00/Makefile
  44. 26
      CPP05/ex00/main.cpp

4
CPP03/ex00/ClapTrap.hpp

@ -6,14 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */
/* Updated: 2022/06/23 17:21:48 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:50:53 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#define CT_HEALTH 10
#define CT_ENERGY 10
#define CT_DMG 1
#define CT_DMG 0
#include <string>
#include <iostream>

4
CPP03/ex01/ClapTrap.hpp

@ -6,14 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */
/* Updated: 2022/06/23 17:21:48 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:51:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#define CT_HEALTH 10
#define CT_ENERGY 10
#define CT_DMG 1
#define CT_DMG 0
#include <string>
#include <iostream>

11
CPP03/ex01/ScavTrap.cpp

@ -6,21 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */
/* Updated: 2022/06/23 17:55:34 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:49:17 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
ScavTrap::ScavTrap (std::string name, size_t hp, size_t ep, size_t atk):ClapTrap(name, hp, ep, atk){
if (ScavTrap::amount >= 128)
{
delete ScavTrap::registery[amount % 128];
cout << "There are already too many Traps" << endl;
}
cout << "ScavTrap " << name << " was created" << endl;
ScavTrap::registery[ScavTrap::amount % 128] = this;
ScavTrap::amount++;
cout << "ClapTrap " << name << " evolved to ScavTrap" << endl;
}
ScavTrap::~ScavTrap (void) {

4
CPP03/ex02/ClapTrap.hpp

@ -6,14 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 10:54:07 by narnaud #+# #+# */
/* Updated: 2022/06/23 17:21:48 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:51:21 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#define CT_HEALTH 10
#define CT_ENERGY 10
#define CT_DMG 1
#define CT_DMG 0
#include <string>
#include <iostream>

19
CPP03/ex02/FragTrap.cpp

@ -6,21 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */
/* Updated: 2022/06/23 18:04:05 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:46:44 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "FragTrap.hpp"
FragTrap::FragTrap (std::string name, size_t hp, size_t ep, size_t atk):ClapTrap(name, hp, ep, atk){
if (FragTrap::amount >= 128)
{
delete FragTrap::registery[amount % 128];
cout << "There are already too many Traps" << endl;
}
cout << "FragTrap " << name << " was created" << endl;
FragTrap::registery[FragTrap::amount % 128] = this;
FragTrap::amount++;
cout << "ClapTrap " << name << " evolved to FragTrap" << endl;
}
FragTrap::~FragTrap (void) {
@ -37,3 +30,11 @@ void FragTrap::attack(const std::string& target) {
_energy--;
}
}
void FragTrap::highFivesGuys(void) {
for (int i = 0; i < 128; i++)
{
if (ClapTrap::registery[i])
cout << _name << " high five " << ClapTrap::registery[i]->getName() << endl;
}
}

3
CPP03/ex02/FragTrap.hpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 15:17:41 by narnaud #+# #+# */
/* Updated: 2022/06/23 17:59:52 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:33:33 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,4 +24,5 @@ class FragTrap : public ClapTrap {
size_t ep = FT_ENERGY, size_t atk = FT_DMG);
~FragTrap (void);
void attack(const std::string& target);
void highFivesGuys(void);
};

11
CPP03/ex02/ScavTrap.cpp

@ -6,21 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */
/* Updated: 2022/06/23 17:55:34 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:47:54 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScavTrap.hpp"
ScavTrap::ScavTrap (std::string name, size_t hp, size_t ep, size_t atk):ClapTrap(name, hp, ep, atk){
if (ScavTrap::amount >= 128)
{
delete ScavTrap::registery[amount % 128];
cout << "There are already too many Traps" << endl;
}
cout << "ScavTrap " << name << " was created" << endl;
ScavTrap::registery[ScavTrap::amount % 128] = this;
ScavTrap::amount++;
cout << "ClapTrap " << name << " evolved to ScavTrap" << endl;
}
ScavTrap::~ScavTrap (void) {

3
CPP03/ex02/main.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 10:53:16 by narnaud #+# #+# */
/* Updated: 2022/06/23 17:59:14 by narnaud ### ########.fr */
/* Updated: 2022/06/24 07:41:01 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,4 +22,5 @@ int main(void)
a.attack("B");
b.attack("C");
c.attack("B");
c.highFivesGuys();
}

43
CPP04/ex00/Animal.cpp

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:22:10 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Animal.hpp"
Animal::Animal (void) {
cout << "Animal default constructor called " << endl;
}
Animal::Animal (string type):_type(type) {
cout << "Animal parameter constructor called" << endl;
}
Animal::Animal (Animal const & src) {
(void)src;
cout << "Animal copy constructor called" << endl;
}
Animal & Animal::operator= (Animal const & src) {
(void)src;
cout << "Animal assignment operator called" << endl;
return (*this);
}
Animal::~Animal (void) {
cout << "Animal default destructor called" << endl;
}
string Animal::getType(void) const {
return (_type);
}
void Animal::makeSound(void) const {
cout << "..." << endl;
}

32
CPP04/ex00/Animal.hpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:10 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:07:53 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Animal{
public:
Animal(void);
Animal(string type);
Animal(Animal const & src);
virtual ~Animal(void);
Animal & operator= (Animal const & src);
string getType(void) const;
virtual void makeSound(void) const;
protected:
string _type;
};

24
CPP04/ex00/Cat.cpp

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:08:55 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Cat.hpp"
Cat::Cat (void):Animal("Cat") {
cout << "Cat default constructor called" << endl;
}
Cat::~Cat (void) {
cout << "Cat default destructor called" << endl;
}
void Cat::makeSound(void) const {
cout << "Miaou" << endl;
}

22
CPP04/ex00/Cat.hpp

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:09:35 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Animal.hpp"
class Cat: public Animal {
public:
Cat(void);
~Cat(void);
void makeSound(void) const;
};

24
CPP04/ex00/Dog.cpp

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:36 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:10:00 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Dog.hpp"
Dog::Dog (void):Animal("Dog") {
cout << "Dog default constructor called" << endl;
}
Dog::~Dog (void) {
cout << "Dog default destructor called" << endl;
}
void Dog::makeSound(void) const {
cout << "Waouf" << endl;
}

21
CPP04/ex00/Dog.hpp

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:36 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:09:29 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Animal.hpp"
class Dog: public Animal {
public:
Dog(void);
~Dog(void);
void makeSound(void) const;
};

20
CPP04/ex00/Makefile

@ -0,0 +1,20 @@
NAME = dumb_animals
SRCS = main.cpp Animal.cpp Dog.cpp Cat.cpp WrongAnimal.cpp WrongCat.cpp
OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME) : $(OBJS)
c++ $(SRCS) -o $(NAME)
all : $(NAME)
clean :
rm -rf $(OBJS)
fclean : clean
rm -rf $(NAME)
re : fclean all
.PHONY : all clean fclean re

42
CPP04/ex00/WrongAnimal.cpp

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongAnimal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:22:03 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "WrongAnimal.hpp"
WrongAnimal::WrongAnimal (void) {
cout << "WrongAnimal default constructor called " << endl;
}
WrongAnimal::WrongAnimal (string type):_type(type) {
cout << "WrongAnimal parameter constructor called" << endl;
}
WrongAnimal::WrongAnimal (WrongAnimal const & src) {
(void)src;
cout << "WrongAnimal copy constructor called" << endl;
}
WrongAnimal & WrongAnimal::operator= (WrongAnimal const & src) {
(void)src;
cout << "WrongAnimal assignment operator called" << endl;
return (*this);
}
WrongAnimal::~WrongAnimal (void) {
cout << "WrongAnimal default destructor called" << endl;
}
string WrongAnimal::getType(void) const {
return (_type);
}
void WrongAnimal::makeSound(void) const {
cout << "..." << endl;
}

32
CPP04/ex00/WrongAnimal.hpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongAnimal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:10 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:15:37 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class WrongAnimal{
public:
WrongAnimal(void);
WrongAnimal(string type);
WrongAnimal(WrongAnimal const & src);
virtual ~WrongAnimal(void);
WrongAnimal & operator= (WrongAnimal const & src);
string getType(void) const;
void makeSound(void) const;
protected:
string _type;
};

24
CPP04/ex00/WrongCat.cpp

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongCat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:15:11 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "WrongCat.hpp"
WrongCat::WrongCat (void):WrongAnimal("WrongCat") {
cout << "WrongCat default constructor called" << endl;
}
WrongCat::~WrongCat (void) {
cout << "WrongCat default destructor called" << endl;
}
void WrongCat::makeSound(void) const {
cout << "Miaou" << endl;
}

22
CPP04/ex00/WrongCat.hpp

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* WrongCat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:13:28 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "WrongAnimal.hpp"
class WrongCat: public WrongAnimal {
public:
WrongCat(void);
~WrongCat(void);
void makeSound(void) const;
};

32
CPP04/ex00/main.cpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 08:28:40 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:16:31 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Dog.hpp"
#include "Cat.hpp"
#include "WrongCat.hpp"
int main(void)
{
const Animal* meta = new Animal();
const Animal* i = new Cat();
const Animal* j = new Dog();
const WrongAnimal *k = new WrongCat();
std::cout << i->getType() << " " << std::endl;
std::cout << j->getType() << " " << std::endl;
i->makeSound(); //will output the cat sound!
j->makeSound();
meta->makeSound();
k->makeSound();
return (0);
}

43
CPP04/ex01/Animal.cpp

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:23:19 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Animal.hpp"
Animal::Animal (void) {
cout << "Animal default constructor called " << endl;
}
Animal::Animal (string type):_type(type) {
cout << "Animal parameter constructor called" << endl;
}
Animal::Animal (Animal const & src) {
(void)src;
cout << "Animal copy constructor called" << endl;
}
Animal & Animal::operator= (Animal const & src) {
(void)src;
cout << "Animal assignment operator called" << endl;
return (*this);
}
Animal::~Animal (void) {
cout << "Animal default destructor called" << endl;
}
string Animal::getType(void) const {
return (_type);
}
void Animal::makeSound(void) const {
cout << "..." << endl;
}

32
CPP04/ex01/Animal.hpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Animal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:10 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:07:53 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Animal{
public:
Animal(void);
Animal(string type);
Animal(Animal const & src);
virtual ~Animal(void);
Animal & operator= (Animal const & src);
string getType(void) const;
virtual void makeSound(void) const;
protected:
string _type;
};

33
CPP04/ex01/Brain.cpp

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:57:44 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Brain.hpp"
Brain::Brain (void) {
cout << "Brain default constructor called " << endl;
}
Brain::Brain (Brain const & src) {
(void)src;
cout << "Brain copy constructor called" << endl;
}
Brain & Brain::operator= (Brain const & src) {
(void)src;
cout << "Brain assignment operator called" << endl;
return (*this);
}
Brain::~Brain (void) {
cout << "Brain default destructor called" << endl;
}

28
CPP04/ex01/Brain.hpp

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:56:58 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
class Brain{
string ideas[100];
public:
Brain(void);
Brain(Brain const & src);
virtual ~Brain(void);
Brain & operator= (Brain const & src);
};

26
CPP04/ex01/Cat.cpp

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:47:13 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Cat.hpp"
Cat::Cat (void):Animal("Cat") {
_brain = new Brain();
cout << "Cat default constructor called" << endl;
}
Cat::~Cat (void) {
delete _brain;
cout << "Cat default destructor called" << endl;
}
void Cat::makeSound(void) const {
cout << "Miaou" << endl;
}

25
CPP04/ex01/Cat.hpp

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:46:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Animal.hpp"
#include "Brain.hpp"
class Cat: public Animal {
Brain *_brain;
public:
Cat(void);
~Cat(void);
void makeSound(void) const;
};

26
CPP04/ex01/Dog.cpp

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:36 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:47:22 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Dog.hpp"
Dog::Dog (void):Animal("Dog") {
_brain = new Brain();
cout << "Dog default constructor called" << endl;
}
Dog::~Dog (void) {
delete _brain;
cout << "Dog default destructor called" << endl;
}
void Dog::makeSound(void) const {
cout << "Waouf" << endl;
}

24
CPP04/ex01/Dog.hpp

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:36 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:46:17 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Animal.hpp"
#include "Brain.hpp"
class Dog: public Animal {
Brain *_brain;
public:
Dog(void);
~Dog(void);
void makeSound(void) const;
};

20
CPP04/ex01/Makefile

@ -0,0 +1,20 @@
NAME = smart_animals
SRCS = main.cpp Animal.cpp Dog.cpp Cat.cpp Brain.cpp
OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME) : $(OBJS)
c++ $(SRCS) -o $(NAME)
all : $(NAME)
clean :
rm -rf $(OBJS)
fclean : clean
rm -rf $(NAME)
re : fclean all
.PHONY : all clean fclean re

32
CPP04/ex01/main.cpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 08:28:40 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:00:44 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Dog.hpp"
#include "Cat.hpp"
#define NB_ANIMALS 4
int main(void)
{
Animal *animals[NB_ANIMALS];
int i = 0;
while (i < NB_ANIMALS / 2)
animals[i++] = new Dog();
while (i < NB_ANIMALS)
animals[i++] = new Cat();
i = 0;
while (i < NB_ANIMALS / 2)
delete animals[i++];
while (i < NB_ANIMALS)
delete animals[i++];
return (0);
}

43
CPP04/ex02/AAnimal.cpp

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AAnimal.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:10:54 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "AAnimal.hpp"
AAnimal::AAnimal (void) {
cout << "AAnimal default constructor called " << endl;
}
AAnimal::AAnimal (string type):_type(type) {
cout << "AAnimal parameter constructor called" << endl;
}
AAnimal::AAnimal (AAnimal const & src) {
(void)src;
cout << "AAnimal copy constructor called" << endl;
}
AAnimal & AAnimal::operator= (AAnimal const & src) {
(void)src;
cout << "AAnimal assignment operator called" << endl;
return (*this);
}
AAnimal::~AAnimal (void) {
cout << "AAnimal default destructor called" << endl;
}
string AAnimal::getType(void) const {
return (_type);
}
void AAnimal::makeSound(void) const {
cout << "..." << endl;
}

32
CPP04/ex02/AAnimal.hpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AAnimal.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:10 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:14:54 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class AAnimal{
public:
AAnimal(void);
AAnimal(string type);
AAnimal(AAnimal const & src);
virtual ~AAnimal(void);
AAnimal & operator= (AAnimal const & src);
string getType(void) const;
virtual void makeSound(void) const = 0;
protected:
string _type;
};

33
CPP04/ex02/Brain.cpp

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:57:44 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Brain.hpp"
Brain::Brain (void) {
cout << "Brain default constructor called " << endl;
}
Brain::Brain (Brain const & src) {
(void)src;
cout << "Brain copy constructor called" << endl;
}
Brain & Brain::operator= (Brain const & src) {
(void)src;
cout << "Brain assignment operator called" << endl;
return (*this);
}
Brain::~Brain (void) {
cout << "Brain default destructor called" << endl;
}

28
CPP04/ex02/Brain.hpp

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Brain.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:56:58 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
class Brain{
string ideas[100];
public:
Brain(void);
Brain(Brain const & src);
virtual ~Brain(void);
Brain & operator= (Brain const & src);
};

26
CPP04/ex02/Cat.cpp

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:10:57 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Cat.hpp"
Cat::Cat (void):AAnimal("Cat") {
_brain = new Brain();
cout << "Cat default constructor called" << endl;
}
Cat::~Cat (void) {
delete _brain;
cout << "Cat default destructor called" << endl;
}
void Cat::makeSound(void) const {
cout << "Miaou" << endl;
}

25
CPP04/ex02/Cat.hpp

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:47 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:11:30 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "AAnimal.hpp"
#include "Brain.hpp"
class Cat: public AAnimal {
Brain *_brain;
public:
Cat(void);
~Cat(void);
void makeSound(void) const;
};

26
CPP04/ex02/Dog.cpp

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:36 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:10:57 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Dog.hpp"
Dog::Dog (void):AAnimal("Dog") {
_brain = new Brain();
cout << "Dog default constructor called" << endl;
}
Dog::~Dog (void) {
delete _brain;
cout << "Dog default destructor called" << endl;
}
void Dog::makeSound(void) const {
cout << "Waouf" << endl;
}

24
CPP04/ex02/Dog.hpp

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Dog.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:36 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:11:18 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "AAnimal.hpp"
#include "Brain.hpp"
class Dog: public AAnimal {
Brain *_brain;
public:
Dog(void);
~Dog(void);
void makeSound(void) const;
};

20
CPP04/ex02/Makefile

@ -0,0 +1,20 @@
NAME = abstract_animals
SRCS = main.cpp AAnimal.cpp Dog.cpp Cat.cpp Brain.cpp
OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME) : $(OBJS)
c++ $(SRCS) -o $(NAME)
all : $(NAME)
clean :
rm -rf $(OBJS)
fclean : clean
rm -rf $(NAME)
re : fclean all
.PHONY : all clean fclean re

32
CPP04/ex02/main.cpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 08:28:40 by narnaud #+# #+# */
/* Updated: 2022/06/24 12:10:57 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Dog.hpp"
#include "Cat.hpp"
#define NB_ANIMALS 4
int main(void)
{
AAnimal *animals[NB_ANIMALS];
int i = 0;
while (i < NB_ANIMALS / 2)
animals[i++] = new Dog();
while (i < NB_ANIMALS)
animals[i++] = new Cat();
i = 0;
while (i < NB_ANIMALS / 2)
delete animals[i++];
while (i < NB_ANIMALS)
delete animals[i++];
return (0);
}

57
CPP05/ex00/Bureaucrat.cpp

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */
/* Updated: 2022/06/24 17:42:27 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat (void) {
cout << "Bureaucrat default constructor called " << endl;
}
Bureaucrat::Bureaucrat (string name, size_t grade) throw (char*): _name(name){
if (grade > 150)
throw Bureaucrat::GradeTooHighException("");
_grade = grade;
cout << "Bureaucrat parameter constructor called" << endl;
}
Bureaucrat::Bureaucrat (Bureaucrat const & src) {
(void)src;
cout << "Bureaucrat copy constructor called" << endl;
}
Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) {
(void)src;
cout << "Bureaucrat assignment operator called" << endl;
return (*this);
}
Bureaucrat::~Bureaucrat (void) {
cout << "Bureaucrat default destructor called" << endl;
}
string Bureaucrat::getName(void) {
return (_name);
}
size_t Bureaucrat::getGrade(void) {
return (_grade);
}
GTHE::GTHE(void) {
}
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) {
out << b._name << ", bureaucrat grade " << b._grade << endl;
}

51
CPP05/ex00/Bureaucrat.hpp

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */
/* Updated: 2022/06/24 16:57:34 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
#include <iostream>
#include <stdexcept>
using std::cout;
using std::endl;
using std::string;
class Bureaucrat{
string _name;
size_t _grade;
public:
Bureaucrat(void);
Bureaucrat(string name, size_t grade) throw (char*);
Bureaucrat(Bureaucrat const & src);
virtual ~Bureaucrat(void);
Bureaucrat & operator= (Bureaucrat const & src);
string getName(void);
size_t getGrade(void);
typedef class GradeTooHighException GTHE;
class GradeTooHighException: virtual public std::exception
{
GTHE(void);
~GTHE(void);
const char *GTHE::what(void);
};
typedef class GradeTooLowException GTLE;
class GradeTooLowException: virtual public std::exception
{
GTLE(void);
~GTLE(void);
const char *GTLE::what(void);
};
};
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b);

20
CPP05/ex00/Makefile

@ -0,0 +1,20 @@
NAME = bureau
SRCS = main.cpp Bureaucrat.cpp
OBJS= $(SRCS:.cpp=.o)
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME) : $(OBJS)
c++ $(OBJS) -o $(NAME)
all : $(NAME)
clean :
rm -rf $(OBJS)
fclean : clean
rm -rf $(NAME)
re : fclean all
.PHONY : all clean fclean re

26
CPP05/ex00/main.cpp

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */
/* Updated: 2022/06/24 14:38:36 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
int main(void)
{
try {
Bureaucrat crat__one("first", 1);
Bureaucrat crat__two("last", 150);
Bureaucrat crat__three("trash", 151);
Bureaucrat crat__four("god", -55);
}
catch (std::exception & e) {
cout << "Issue :" << e.what();
}
}
Loading…
Cancel
Save