ex00: it works!

This commit is contained in:
Khaïs COLIN 2025-05-30 08:14:26 +02:00
commit 6d4b5788d7
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
16 changed files with 318 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*.o
*.d
.cache
compile_commands.json

1
ex00/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
ex00

29
ex00/Animal.cpp Normal file
View file

@ -0,0 +1,29 @@
#include "Animal.hpp"
#include <iostream>
Animal::Animal() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
Animal::Animal(const Animal &other) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
type = other.type;
}
Animal::~Animal() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
Animal &Animal::operator=(const Animal &other) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
this->type = other.type;
return *this;
}
std::string Animal::getType() const { return type; }
void Animal::makeSound() const {
if (type == "Cat") {
std::cout << "[Meow]" << std::endl;
} else if (type == "Dog") {
std::cout << "[Bark]" << std::endl;
} else {
std::cout << "[Generic Animal Sound]" << std::endl;
}
}

20
ex00/Animal.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef ANIMAL_HPP
#define ANIMAL_HPP
#include <string>
class Animal {
public:
Animal();
Animal(const Animal &other);
~Animal();
Animal &operator=(const Animal &other);
std::string getType() const;
void makeSound() const;
protected:
std::string type;
};
#endif

20
ex00/Cat.cpp Normal file
View file

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

14
ex00/Cat.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef CAT_HPP
#define CAT_HPP
#include "Animal.hpp"
class Cat : public Animal {
public:
Cat();
Cat(const Cat &other);
~Cat();
Cat &operator=(const Cat &other);
};
#endif

20
ex00/Dog.cpp Normal file
View file

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

14
ex00/Dog.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef DOG_HPP
#define DOG_HPP
#include "Animal.hpp"
class Dog : public Animal {
public:
Dog();
Dog(const Dog &other);
~Dog();
Dog &operator=(const Dog &other);
};
#endif

49
ex00/Makefile Normal file
View file

@ -0,0 +1,49 @@
NAME = ex00
ifeq ($(CPPFLAGS),)
CPPFLAGS = -Wall -Wextra -Werror -std=c++98 -ggdb -fsanitize=address,undefined
endif
ifeq ($(CXX),)
CXX = c++
endif
# g++ is the default on 42 computers
ifeq ($(CXX),g++)
CXX = c++
endif
srcs = \
Animal.cpp \
Cat.cpp \
Dog.cpp \
WrongAnimal.cpp \
WrongDog.cpp \
WrongCat.cpp \
main_objs = main.o $(srcs:.cpp=.o)
all_objs = $(main_objs)
deps = $(all_objs:.o=.d)
.PHONY: all clean fclean re run
all: $(NAME)
-include $(deps)
$(NAME): $(main_objs)
$(CXX) $(CPPFLAGS) -o $@ $^
%.o: %.cpp
$(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp
$(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d
clean:
find . -name '*.o' -print -delete
find . -name '*.d' -print -delete
fclean: clean
rm -f $(NAME)
re:
+make fclean
+make all
run: $(NAME)
./$(NAME)

29
ex00/WrongAnimal.cpp Normal file
View file

@ -0,0 +1,29 @@
#include "WrongAnimal.hpp"
#include <iostream>
WrongAnimal::WrongAnimal() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
WrongAnimal::WrongAnimal(const WrongAnimal &other) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
type = other.type;
}
WrongAnimal::~WrongAnimal() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
WrongAnimal &WrongAnimal::operator=(const WrongAnimal &other) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
this->type = other.type;
return *this;
}
std::string WrongAnimal::getType() const { return type; }
void WrongAnimal::makeSound() const {
if (type == "WrongCat") {
std::cout << "[Eldritch Meow]" << std::endl;
} else if (type == "WrongDog") {
std::cout << "[Eldritch Bark]" << std::endl;
} else {
std::cout << "[Eldritch WrongAnimal Sound]" << std::endl;
}
}

20
ex00/WrongAnimal.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef WRONG_ANIMAL_HPP
#define WRONG_ANIMAL_HPP
#include <string>
class WrongAnimal {
public:
WrongAnimal();
WrongAnimal(const WrongAnimal &other);
~WrongAnimal();
WrongAnimal &operator=(const WrongAnimal &other);
std::string getType() const;
void makeSound() const;
protected:
std::string type;
};
#endif

8
ex00/WrongCat.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "WrongCat.hpp"
#include <iostream>
WrongCat::WrongCat() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
type = "WrongCat";
}

14
ex00/WrongCat.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef WRONG_CAT_HPP
#define WRONG_CAT_HPP
#include "WrongAnimal.hpp"
class WrongCat : public WrongAnimal {
public:
WrongCat();
WrongCat(const WrongCat &other);
~WrongCat();
WrongCat &operator=(const WrongCat &other);
};
#endif

7
ex00/WrongDog.cpp Normal file
View file

@ -0,0 +1,7 @@
#include "WrongDog.hpp"
#include <iostream>
WrongDog::WrongDog() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
type = "WrongDog";
}

14
ex00/WrongDog.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef WRONG_DOG_HPP
#define WRONG_DOG_HPP
#include "WrongAnimal.hpp"
class WrongDog : public WrongAnimal {
public:
WrongDog();
WrongDog(const WrongDog &other);
~WrongDog();
WrongDog &operator=(const WrongDog &other);
};
#endif

55
ex00/main.cpp Normal file
View file

@ -0,0 +1,55 @@
#include "Animal.hpp"
#include "Cat.hpp"
#include "Dog.hpp"
#include "WrongAnimal.hpp"
#include "WrongCat.hpp"
#include "WrongDog.hpp"
#include <iostream>
int main() {
{
Animal animal1 = Animal();
Animal animal2 = animal1;
Animal animal3;
animal3 = animal1;
}
std::cout << std::endl;
{
const Animal *meta = new Animal();
const Animal *j = new Dog();
const Animal *i = new Cat();
std::cout << j->getType() << " " << std::endl;
std::cout << i->getType() << " " << std::endl;
i->makeSound();
j->makeSound();
meta->makeSound();
delete meta;
delete j;
delete i;
}
std::cout << std::endl;
{
const WrongAnimal *meta = new WrongAnimal();
const WrongAnimal *j = new WrongDog();
const WrongAnimal *i = new WrongCat();
std::cout << j->getType() << " " << std::endl;
std::cout << i->getType() << " " << std::endl;
i->makeSound();
j->makeSound();
meta->makeSound();
delete meta;
delete j;
delete i;
}
std::cout << std::endl;
{
const Dog *dog = new Dog();
dog->makeSound();
delete dog;
}
return 0;
}