d04 ex01 en grande partie fait mais segfault qqpart

This commit is contained in:
Hugo LAMY
2022-02-25 14:19:24 +01:00
parent 3cdc4ec436
commit 12f4c72e77
16 changed files with 271 additions and 196 deletions

View File

@@ -1,15 +1,19 @@
#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;
}
@@ -19,6 +23,7 @@ Animal::Animal( Animal const & src ) {
*********************************************/
Animal::~Animal() {
std::cout << COPLIEN_COLOR "Animal destructor" RESET "\n";
return;
}
@@ -27,6 +32,7 @@ Animal::~Animal() {
*********************************************/
Animal & Animal::operator=( Animal const & rhs ) {
std::cout << COPLIEN_COLOR "Animal assignator" RESET "\n";
if ( this != &rhs )
{
type = rhs.getType();

View File

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

65
d04/ex01/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;
}

30
d04/ex01/Brain.hpp Normal file
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

View File

@@ -1,15 +1,20 @@
#include "Cat.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Cat::Cat() {
Cat::Cat( Brain * brain ) {
std::cout << COPLIEN_COLOR "Cat constructor" RESET "\n";
type = "cat";
_brain = brain;
return;
}
Cat::Cat( Cat const & src ) {
std::cout << COPLIEN_COLOR "Cat copy constructor" RESET "\n";
*this = src;
return;
}
@@ -19,6 +24,7 @@ Cat::Cat( Cat const & src ) {
*********************************************/
Cat::~Cat() {
std::cout << COPLIEN_COLOR "Cat destructor" RESET "\n";
return;
}
@@ -27,8 +33,9 @@ Cat::~Cat() {
*********************************************/
Cat & Cat::operator=( Cat const & rhs ) {
std::cout << COPLIEN_COLOR "Cat assignator" RESET "\n";
Animal::operator=(rhs);
*_brain = *rhs._brain;
return *this;
}
@@ -39,4 +46,9 @@ Cat & Cat::operator=( Cat const & rhs ) {
void Cat::makeSound() const {
std::cout << "*miaow*\n";
}
void Cat::printBrain() const {
_brain->printIdeas();
}
void Cat::printBrain(int pos) const {
_brain->printIdea(pos);
}

View File

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

View File

@@ -1,15 +1,20 @@
#include "Dog.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Dog::Dog() {
Dog::Dog( Brain *brain ) {
std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n";
type = "dog";
_brain = brain;
return;
}
Dog::Dog( Dog const & src ) {
std::cout << COPLIEN_COLOR "Dog copy constructor" RESET "\n";
*this = src;
return;
}
@@ -19,6 +24,7 @@ Dog::Dog( Dog const & src ) {
*********************************************/
Dog::~Dog() {
std::cout << COPLIEN_COLOR "Dog destructor" RESET "\n";
return;
}
@@ -28,6 +34,7 @@ Dog::~Dog() {
Dog & Dog::operator=( Dog const & rhs ) {
Animal::operator=(rhs);
_brain = rhs._brain;
return *this;
}

View File

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

View File

@@ -4,7 +4,7 @@
# . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = poly
NAME = burn
#TYPE = c
TYPE = cpp
@@ -33,15 +33,13 @@ SRCS = main.cpp \
Animal.cpp \
Dog.cpp \
Cat.cpp \
WrongAnimal.cpp \
WrongCat.cpp
Brain.cpp
D_HEADERS = .
HEADERS = Animal.hpp \
Dog.hpp \
Cat.hpp \
WrongAnimal.hpp \
WrongCat.hpp
Brain.hpp
D_OBJS = builds
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)

View File

@@ -1,50 +0,0 @@
#include "WrongAnimal.hpp"
/*********************************************
* CONSTRUCTORS
*********************************************/
WrongAnimal::WrongAnimal() {
type = "wrong_animal";
return;
}
WrongAnimal::WrongAnimal( WrongAnimal const & src ) {
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
WrongAnimal::~WrongAnimal() {
return;
}
/*********************************************
* OPERATORS
*********************************************/
WrongAnimal & WrongAnimal::operator=( WrongAnimal const & rhs ) {
if ( this != &rhs )
{
type = rhs.getType();
}
return *this;
}
/*********************************************
* ACCESSORS
*********************************************/
std::string WrongAnimal::getType() const {return type;}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void WrongAnimal::makeSound() const {
std::cout << "*sound*\n";
}

View File

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

View File

@@ -1,40 +0,0 @@
#include "WrongCat.hpp"
/*********************************************
* CONSTRUCTORS
*********************************************/
WrongCat::WrongCat() {
type = "wrong_cat";
return;
}
WrongCat::WrongCat( WrongCat const & src ) {
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
WrongCat::~WrongCat() {
return;
}
/*********************************************
* OPERATORS
*********************************************/
WrongCat & WrongCat::operator=( WrongCat const & rhs ) {
WrongAnimal::operator=(rhs);
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void WrongCat::makeSound() const {
std::cout << "*miaow*\n";
}

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

BIN
d04/ex01/burn Executable file

Binary file not shown.

24
d04/ex01/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

View File

@@ -1,52 +1,118 @@
#include "Animal.hpp"
#include "Dog.hpp"
#include "Cat.hpp"
#include "WrongAnimal.hpp"
#include "WrongCat.hpp"
#include <iostream>
#include <string>
#include "color.h"
int main() {
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(); //will output the cat sound!
j->makeSound();
meta->makeSound();
std::cout << B_YELLOW "\n1rst test :" RESET "\n";
{
std::cout << std::endl;
const Cat* i = new Cat();
const Animal* j = new Cat();
std::cout << j->getType() << " " << std::endl;
std::cout << i->getType() << " " << std::endl;
i->makeSound(); //will output the cat sound!
j->makeSound();
const Animal* j = new Dog();
const Animal* i = new Cat();
delete j;//should not create a leak
delete i;
}
std::cout << B_YELLOW "\n2nd test :" RESET "\n";
{
std::cout << std::endl;
const WrongAnimal* i = new WrongAnimal();
const WrongAnimal* j = new WrongCat();
std::cout << i->getType() << " " << std::endl;
std::cout << j->getType() << " " << std::endl;
i->makeSound(); //will output the cat sound!
j->makeSound();
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();
delete dog;
delete cat1;
delete brain1;
delete brain2;
}
std::cout << B_YELLOW "\narray animal test :" RESET "\n";
{
std::cout << std::endl;
const WrongCat* i = new WrongCat();
const WrongAnimal* j = new WrongCat();
std::cout << j->getType() << " " << std::endl;
std::cout << i->getType() << " " << std::endl;
i->makeSound(); //will output the cat sound!
j->makeSound();
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 "\ncopy constructor test1 :" 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 "\ncopy constructor test2 :" 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 "\nassignation 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 "\nassignation 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;
}