d04 ex02 ok, presque rien a changer ou je me trompe ?

This commit is contained in:
hugogogo
2022-02-26 22:12:55 +01:00
parent ed2bbc7fd2
commit c056db80b7
27 changed files with 597 additions and 68 deletions

View File

@@ -1,23 +0,0 @@
#ifndef WRONG_CAT_HPP
# define WRONG_CAT_HPP
# include "WrongAnimal.hpp"
#include <iostream>
#include <string>
class WrongCat : public WrongAnimal {
public:
WrongCat( void );
WrongCat( WrongCat const & src );
~WrongCat( void );
WrongCat & operator=( WrongCat const & rhs );
void makeSound() const;
};
#endif

Binary file not shown.

View File

@@ -28,14 +28,14 @@ LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = .
D_SRCS = srcs
SRCS = main.cpp \
Animal.cpp \
Dog.cpp \
Cat.cpp \
Brain.cpp
D_HEADERS = .
D_HEADERS = headers
HEADERS = Animal.hpp \
Dog.hpp \
Cat.hpp \

Binary file not shown.

Binary file not shown.

View File

@@ -1,43 +0,0 @@
#include <iostream>
class A {
public:
A & operator=( A const & rhs ) {
std::cout << "A assignation operator\n";
_i = rhs._i;
return *this;}
private:
int _i;
};
class B {
public:
B() {
std::cout << "B default constructor\n";
_a = new A();}
B( A *a ) {
std::cout << "B parameterized constructor\n";
_a = new A();
*_a = *a;}
~B() {
std::cout << "B destructor\n";
delete _a;}
private:
A *_a;
};
int main() {
{
const B * b = new B();
delete b;
}
std::cout << "\n";
{
B * b;
A * a = new A();
b = new B(a);
delete a;
delete b;
}
return 0;
}

82
d04/ex02/Makefile Normal file
View File

@@ -0,0 +1,82 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value \ . += append to a variable #
# VARIABLES . value . != set result of command #
# . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = pure
#TYPE = c
TYPE = cpp
ifeq "$(TYPE)" "c"
CC = c
EXT = c
else ifeq "$(TYPE)" "cpp"
CC = c++
EXT = cpp
endif
CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
ifeq "$(TYPE)" "cpp"
CFLAGS += -std=c++98
endif
VPATH = $(D_SRCS)
LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = srcs
SRCS = main.cpp \
Animal.cpp \
Dog.cpp \
Cat.cpp \
Brain.cpp
D_HEADERS = headers
HEADERS = Animal.hpp \
Dog.hpp \
Cat.hpp \
Brain.hpp
D_OBJS = builds
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
ifeq "$(D_OBJS)" "."
RM_OBJS = rm -f $(OBJS)
else
RM_OBJS = rm -rf $(D_OBJS)
endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
# RULES . recipe . $< : 1st prerequisite #
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
mkdir $@
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
clean:
$(RM_OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY : all clean fclean re

View File

@@ -0,0 +1,27 @@
#ifndef ANIMAL_HPP
# define ANIMAL_HPP
# include "color.h"
#include <iostream>
#include <string>
class Animal {
public:
Animal();
Animal( Animal const & src );
virtual ~Animal( void );
Animal & operator=( Animal const & rhs );
virtual void makeSound() const = 0;
std::string getType() const;
protected:
std::string type;
};
#endif

View File

@@ -0,0 +1,30 @@
#ifndef BRAIN_HPP
# define BRAIN_HPP
#include "color.h"
#include <iostream>
#include <string>
#define SIZE_IDEAS 100
class Brain {
public:
Brain( void );
Brain( Brain const & src );
~Brain( void );
Brain & operator=( Brain const & rhs );
void printIdea(int pos);
void putIdea(int pos, std::string idea);
void printIdeas();
void putIdeas(std::string idea);
private:
std::string _ideas[SIZE_IDEAS];
};
#endif

34
d04/ex02/headers/Cat.hpp Normal file
View File

@@ -0,0 +1,34 @@
#ifndef CAT_HPP
# define CAT_HPP
# include "color.h"
# include <iostream>
# include <string>
# include "Animal.hpp"
# include "Brain.hpp"
class Cat : public Animal {
public:
Cat();
Cat( Brain *brain );
// Cat( Brain *brain = new Brain() );
Cat( Cat const & src );
~Cat();
Cat & operator=( Cat const & rhs );
void makeSound() const;
void printBrain() const;
void printBrain(int pos) const;
private:
Brain *_brain;
};
#endif

30
d04/ex02/headers/Dog.hpp Normal file
View File

@@ -0,0 +1,30 @@
#ifndef DOG_HPP
# define DOG_HPP
# include "color.h"
# include <iostream>
# include <string>
# include "Animal.hpp"
# include "Brain.hpp"
class Dog : public Animal {
public:
Dog();
Dog( Brain *brain );
Dog( Dog const & src );
~Dog();
Dog & operator=( Dog const & rhs );
void makeSound() const;
private:
Brain *_brain;
};
#endif

24
d04/ex02/headers/color.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef COLOR_H
# define COLOR_H
# define GRAY "\e[0;30m"
# define RED "\e[0;31m"
# define GREEN "\e[0;32m"
# define YELLOW "\e[0;33m"
# define BLUE "\e[0;34m"
# define PURPLE "\e[0;35m"
# define CYAN "\e[0;36m"
# define WHITE "\e[0;37m"
# define B_GRAY "\e[1;30m"
# define B_RED "\e[1;31m"
# define B_GREEN "\e[1;32m"
# define B_YELLOW "\e[1;33m"
# define B_BLUE "\e[1;34m"
# define B_PURPLE "\e[1;35m"
# define B_CYAN "\e[1;36m"
# define B_WHITE "\e[1;37m"
# define RESET "\e[0m"
#endif

123
d04/ex02/main.cpp Normal file
View File

@@ -0,0 +1,123 @@
#include "Animal.hpp"
#include "Dog.hpp"
#include "Cat.hpp"
#include <iostream>
#include <string>
#include "color.h"
#define N_TEST "7"
int main() {
std::cout << B_YELLOW "\n[1/" N_TEST "] test subject :" RESET "\n";
{
const Animal* j = new Dog();
const Animal* i = new Cat();
delete j;//should not create a leak
delete i;
}
std::cout << B_YELLOW "\n[2/" N_TEST "] test with brain :" RESET "\n";
{
Dog * dog;
Cat * cat1;
Cat cat2;
Brain * brain1 = new Brain();
Brain * brain2 = new Brain();
std::cout << B_BLUE "fill brain1 with \"giraffe\" :" RESET "\n";
brain1->putIdeas("giraffe");
std::cout << B_BLUE "print brain1 :" RESET "\n";
brain1->printIdeas();
std::cout << B_BLUE "print brain2 :" RESET "\n";
brain2->printIdeas();
std::cout << B_BLUE "brain2 copy brain1 :" RESET "\n";
*brain2 = *brain1;
std::cout << B_BLUE "fill brain1 with \"hippopotamus\" :" RESET "\n";
brain1->putIdeas("hippopotamus");
std::cout << B_BLUE "print brain1 :" RESET "\n";
brain1->printIdeas();
std::cout << B_BLUE "print brain2 :" RESET "\n";
brain2->printIdeas();
std::cout << B_BLUE "create new dog with brain1 :" RESET "\n";
dog = new Dog(brain1);
std::cout << B_BLUE "create new cat with brain1 :" RESET "\n";
cat1 = new Cat(brain1);
std::cout << B_BLUE "cat2 copy cat1 :" RESET "\n";
cat2 = *cat1;
std::cout << B_BLUE "fill brain1 with \"zebra\" :" RESET "\n";
brain1->putIdeas("zebra");
std::cout << B_BLUE "print cat1 :" RESET "\n";
cat1->printBrain();
std::cout << B_BLUE "print cat2 :" RESET "\n";
cat2.printBrain();
std::cout << B_BLUE "delete dog :" RESET "\n";
delete dog;
std::cout << B_BLUE "delete cat1 :" RESET "\n";
delete cat1;
std::cout << B_BLUE "delete brain1 :" RESET "\n";
delete brain1;
std::cout << B_BLUE "delete brain2 :" RESET "\n";
delete brain2;
}
std::cout << B_YELLOW "\n[3/" N_TEST "] array animal test :" RESET "\n";
{
Animal *animals[10];
int i;
for (i = 0 ; i < 5 ; ++i)
animals[i] = new Cat();
for ( ; i < 10 ; ++i)
animals[i] = new Dog();
for (i = 0 ; i < 10 ; ++i)
animals[i]->makeSound();
for (i = 0 ; i < 10 ; ++i)
delete animals[i];
}
std::cout << B_YELLOW "\n[4/" N_TEST "] copy constructor test1/2 :" RESET "\n";
{
std::cout << B_BLUE "Cat a_cat :" RESET "\n";
Cat a_cat;
std::cout << B_BLUE "Cat a_cpy_cat(a_cat) :" RESET "\n";
Cat a_cpy_cat(a_cat);
}
std::cout << B_YELLOW "\n[5/" N_TEST "] copy constructor test2/2 :" RESET "\n";
{
std::cout << B_BLUE "Cat a_cat :" RESET "\n";
Cat a_cat;
std::cout << B_BLUE "Cat a_cpy_cat = a_cat :" RESET "\n";
Cat a_cpy_cat = a_cat;
}
std::cout << B_YELLOW "\n[6/" N_TEST "] assignation operator test1 :" RESET "\n";
{
std::cout << B_BLUE "Cat a_cat :" RESET "\n";
Cat a_cat;
std::cout << B_BLUE "Cat a_cpy_cat :" RESET "\n";
Cat a_cpy_cat;
std::cout << B_BLUE "a_cpy_cat = a_cat :" RESET "\n";
a_cpy_cat = a_cat;
}
std::cout << B_YELLOW "\n[7/" N_TEST "] assignation operator test2 :" RESET "\n";
{
std::cout << B_BLUE "const Cat *a_cat :" RESET "\n";
const Cat *a_cat = new Cat();
std::cout << B_BLUE "Cat a_cpy_cat :" RESET "\n";
Cat a_cpy_cat;
std::cout << B_BLUE "a_cpy_cat = *a_cat :" RESET "\n";
a_cpy_cat = *a_cat;
delete a_cat;
}
return 0;
}

42
d04/ex02/srcs/Animal.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include "Animal.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Animal::Animal() {
std::cout << COPLIEN_COLOR "Animal constructor" RESET "\n";
type = "animal";
return;
}
Animal::Animal( Animal const & src ) {
std::cout << COPLIEN_COLOR "Animal copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Animal::~Animal() {
std::cout << COPLIEN_COLOR "Animal destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Animal & Animal::operator=( Animal const & rhs ) {
std::cout << COPLIEN_COLOR "Animal assignator" RESET "\n";
if ( this != &rhs )
type = rhs.getType();
return *this;
}
std::string Animal::getType() const {return type;}

65
d04/ex02/srcs/Brain.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "Brain.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Brain::Brain() {
std::cout << COPLIEN_COLOR "Brain constructor" RESET "\n";
return;
}
Brain::Brain( Brain const & src ) {
std::cout << COPLIEN_COLOR "Brain copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Brain::~Brain() {
std::cout << COPLIEN_COLOR "Brain destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Brain & Brain::operator=( Brain const & rhs ) {
std::cout << COPLIEN_COLOR "Brain assignator" RESET "\n";
if ( this != &rhs )
for (int i = 0; i < SIZE_IDEAS; i++)
_ideas[i] = rhs._ideas[i];
return *this;
}
/*********************************************
* ACCESSORS
*********************************************/
void Brain::printIdea(int pos) {
if (pos < SIZE_IDEAS)
std::cout << _ideas[pos] << "\n";
}
void Brain::putIdea(int pos, std::string idea) {
if (pos < SIZE_IDEAS)
_ideas[pos] = idea;
}
void Brain::printIdeas() {
for (int i = 0; i < SIZE_IDEAS; i++)
std::cout << _ideas[i] << " - ";
std::cout << "\n";
}
void Brain::putIdeas(std::string idea) {
for (int i = 0; i < SIZE_IDEAS; i++)
_ideas[i] = idea;
}

73
d04/ex02/srcs/Cat.cpp Normal file
View File

@@ -0,0 +1,73 @@
#include "Cat.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
/*
* default arguments in default constructor : https://stackoverflow.com/questions/187640/default-parameters-with-c-constructors
* in this cas it doesn't work i think, since both constructors don't act exactly the same
*/
Cat::Cat() {
std::cout << COPLIEN_COLOR "Cat default constructor" RESET "\n";
type = "cat";
_brain = new Brain();
return;
}
Cat::Cat( Brain * brain ) {
std::cout << COPLIEN_COLOR "Cat parameters constructor" RESET "\n";
type = "cat";
_brain = new Brain();
*_brain = *brain;
return;
}
/*
* error: base class class Animal should be explicitly initialized in the copy constructor [-Werror=extra]
* Cat::Cat( Cat const & src ) {
* ^~~
* answer : https://stackoverflow.com/questions/43612772/base-class-class-a-should-be-explicitly-initialized-in-the-copy-constructor
*/
Cat::Cat( Cat const & src ) : Animal(src) {
std::cout << COPLIEN_COLOR "Cat copy constructor" RESET "\n";
_brain = new Brain();
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Cat::~Cat() {
std::cout << COPLIEN_COLOR "Cat destructor" RESET "\n";
delete _brain;
return;
}
/*********************************************
* OPERATORS
*********************************************/
Cat & Cat::operator=( Cat const & rhs ) {
std::cout << COPLIEN_COLOR "Cat assignator" RESET "\n";
Animal::operator=(rhs);
*_brain = *rhs._brain;
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void Cat::makeSound() const {
std::cout << "*miaow*\n";
}
void Cat::printBrain() const {
_brain->printIdeas();
}
void Cat::printBrain(int pos) const {
_brain->printIdea(pos);
}

65
d04/ex02/srcs/Dog.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "Dog.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Dog::Dog() {
std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n";
type = "dog";
_brain = new Brain();
return;
}
Dog::Dog( Brain *brain ) {
std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n";
type = "dog";
_brain = new Brain();
*_brain = *brain;
return;
}
/*
error: base class class Animal should be explicitly initialized in the copy constructor [-Werror=extra]
Dog::Dog( Dog const & src ) {
^~~
answer : https://stackoverflow.com/questions/43612772/base-class-class-a-should-be-explicitly-initialized-in-the-copy-constructor
*/
Dog::Dog( Dog const & src ) : Animal() {
std::cout << COPLIEN_COLOR "Dog copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Dog::~Dog() {
std::cout << COPLIEN_COLOR "Dog destructor" RESET "\n";
delete _brain;
return;
}
/*********************************************
* OPERATORS
*********************************************/
Dog & Dog::operator=( Dog const & rhs ) {
Animal::operator=(rhs);
_brain = new Brain();
if (this != &rhs)
_brain = rhs._brain;
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void Dog::makeSound() const {
std::cout << "*woof*\n";
}