Files
42_INT_09_piscine_cpp/d04/ex01/main.cpp

119 lines
2.9 KiB
C++

#include "Animal.hpp"
#include "Dog.hpp"
#include "Cat.hpp"
#include <iostream>
#include <string>
#include "color.h"
int main() {
std::cout << B_YELLOW "\n1rst test :" 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 "\n2nd test :" 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();
delete dog;
delete cat1;
delete brain1;
delete brain2;
}
std::cout << B_YELLOW "\narray 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 "\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;
}