This commit is contained in:
Camille Chauvet 2023-08-09 14:50:56 +00:00
parent 7a64f69133
commit 788f73331e
14 changed files with 326 additions and 0 deletions

25
ex01/Makefile Normal file
View File

@ -0,0 +1,25 @@
CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
OBJDIR = obj
NAME = ex01
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(NAME)
re: fclean all

34
ex01/src/Animal.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "Animal.hpp"
#include <iostream>
#include <string>
Animal::Animal()
{
std::cout << "Animal()" << std::endl;
}
Animal::Animal(const Animal& src)
{
*this = src;
}
Animal& Animal::operator=(const Animal &src)
{
this->type = src.type;
return *this;
}
Animal::~Animal()
{
std::cout << "~Animal()" << std::endl;
}
void Animal::makeSound() const
{
std::cout << "OuGABounga" << std::endl;
}
std::string Animal::getType() const
{
return this->type;
}

19
ex01/src/Animal.hpp Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <string>
class Animal
{
public:
Animal();
Animal(const Animal& src);
Animal &operator=(const Animal& src);
virtual ~Animal();
std::string getType() const;
virtual void makeSound() const;
protected:
std::string type;
};

24
ex01/src/Brain.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "Brain.hpp"
#include <iostream>
Brain& Brain::operator=(const Brain &src)
{
for (size_t i = 0; i < 100; i++)
this->ideas[i] = src.ideas[i];
return *this;
}
Brain::~Brain()
{
std::cout << "~Brain()" << std::endl;
}
Brain::Brain()
{
std::cout << "Brain()" << std::endl;
}
Brain::Brain(const Brain& src)
{
*this = src;
}

14
ex01/src/Brain.hpp Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <string>
class Brain
{
public:
Brain();
Brain(const Brain& src);
~Brain();
Brain& operator=(const Brain& src);
private:
std::string ideas[100];
};

31
ex01/src/Cat.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "Cat.hpp"
#include <iostream>
Cat::Cat()
{
this->brain = new Brain();
this->type = "Cat";
std::cout << "Cat()" << std::endl;
}
Cat& Cat::operator=(const Cat& src)
{
this->type = src.type;
return *this;
}
Cat::~Cat()
{
delete this->brain;
std::cout << "~Cat()" << std::endl;
}
Cat::Cat(const Cat& src): Animal()
{
*this = src;
}
void Cat::makeSound() const
{
std::cout << "Meow Meow !" << std::endl;
}

20
ex01/src/Cat.hpp Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include <string>
#include "./Animal.hpp"
#include "./Brain.hpp"
class Cat : public Animal
{
public:
Cat(const Cat& src);
Cat &operator=(const Cat& src);
Cat();
~Cat();
void makeSound() const;
private:
Brain* brain;
};

32
ex01/src/Dog.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "Dog.hpp"
#include <iostream>
Dog::Dog()
{
this->type = "Dog";
this->brain = new Brain();
std::cout << "Dog()" << std::endl;
}
Dog& Dog::operator=(const Dog& src)
{
this->type = src.type;
return *this;
}
Dog::~Dog()
{
delete this->brain;
std::cout << "~Dog()" << std::endl;
}
Dog::Dog(const Dog& src): Animal()
{
*this = src;
}
void Dog::makeSound() const
{
std::cout << "Ouaf ouaf !" << std::endl;
}

20
ex01/src/Dog.hpp Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include <string>
#include "./Animal.hpp"
#include "./Brain.hpp"
class Dog : public Animal
{
public:
Dog(const Dog& src);
Dog &operator=(const Dog& src);
Dog();
~Dog();
void makeSound() const;
private:
Brain* brain;
};

34
ex01/src/WrongAnimal.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "WrongAnimal.hpp"
#include <iostream>
#include <string>
WrongAnimal::WrongAnimal()
{
std::cout << "WrongAnimal()" << std::endl;
}
WrongAnimal::WrongAnimal(const WrongAnimal& src)
{
*this = src;
}
WrongAnimal& WrongAnimal::operator=(const WrongAnimal &src)
{
this->type = src.type;
return *this;
}
WrongAnimal::~WrongAnimal()
{
std::cout << "~WrongAnimal()" << std::endl;
}
void WrongAnimal::makeSound() const
{
std::cout << "OuGABounga" << std::endl;
}
std::string WrongAnimal::getType() const
{
return this->type;
}

19
ex01/src/WrongAnimal.hpp Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <string>
class WrongAnimal
{
public:
WrongAnimal();
WrongAnimal(const WrongAnimal& src);
WrongAnimal &operator=(const WrongAnimal& src);
virtual ~WrongAnimal();
std::string getType() const;
void makeSound() const;
protected:
std::string type;
};

24
ex01/src/WrongCat.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "WrongCat.hpp"
#include <iostream>
WrongCat::WrongCat()
{
this->type = "WrongCat";
std::cout << "WrongCat()" << std::endl;
}
WrongCat& WrongCat::operator=(const WrongCat& src)
{
this->type = src.type;
return *this;
}
WrongCat::~WrongCat()
{
std::cout << "~WrongCat()" << std::endl;
}
WrongCat::WrongCat(const WrongCat& src): WrongAnimal()
{
*this = src;
}

14
ex01/src/WrongCat.hpp Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <string>
#include "./WrongAnimal.hpp"
class WrongCat : public WrongAnimal
{
public:
WrongCat(const WrongCat& src);
WrongCat &operator=(const WrongCat& src);
WrongCat();
~WrongCat();
};

16
ex01/src/main.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
#include "Cat.hpp"
#include "Dog.hpp"
#include "WrongCat.hpp"
int main()
{
const Animal* meta = new Animal();
delete meta;
const Animal* j = new Dog();
delete j;
const Animal* i = new Cat();
delete i;
return 0;
}