d04 ex01 en grande partie fait mais segfault qqpart
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user