d04 ex01 ok change affichage + delete brain + constructors with default arguments

This commit is contained in:
hugogogo
2022-02-26 19:20:49 +01:00
parent 12f4c72e77
commit ed2bbc7fd2
8 changed files with 116 additions and 29 deletions

View File

@@ -6,15 +6,33 @@
* CONSTRUCTORS
*********************************************/
Cat::Cat( Brain * brain ) {
std::cout << COPLIEN_COLOR "Cat constructor" RESET "\n";
/*
* 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 = brain;
_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;
}
Cat::Cat( Cat const & src ) {
/*
* 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;
}
@@ -25,6 +43,7 @@ Cat::Cat( Cat const & src ) {
Cat::~Cat() {
std::cout << COPLIEN_COLOR "Cat destructor" RESET "\n";
delete _brain;
return;
}

View File

@@ -12,7 +12,9 @@ class Cat : public Animal {
public:
Cat( Brain *brain = new Brain() );
Cat();
Cat( Brain *brain );
// Cat( Brain *brain = new Brain() );
Cat( Cat const & src );
~Cat();
Cat & operator=( Cat const & rhs );

View File

@@ -6,14 +6,28 @@
* CONSTRUCTORS
*********************************************/
Dog::Dog( Brain *brain ) {
Dog::Dog() {
std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n";
type = "dog";
_brain = brain;
_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;
@@ -25,6 +39,7 @@ Dog::Dog( Dog const & src ) {
Dog::~Dog() {
std::cout << COPLIEN_COLOR "Dog destructor" RESET "\n";
delete _brain;
return;
}
@@ -34,7 +49,9 @@ Dog::~Dog() {
Dog & Dog::operator=( Dog const & rhs ) {
Animal::operator=(rhs);
_brain = rhs._brain;
_brain = new Brain();
if (this != &rhs)
_brain = rhs._brain;
return *this;
}

View File

@@ -12,7 +12,8 @@ class Dog : public Animal {
public:
Dog( Brain *brain = new Brain() );
Dog();
Dog( Brain *brain );
Dog( Dog const & src );
~Dog();
Dog & operator=( Dog const & rhs );

BIN
d04/ex01/a.out Executable file

Binary file not shown.

Binary file not shown.

View File

@@ -6,9 +6,10 @@
#include <string>
#include "color.h"
#define N_TEST "7"
int main() {
std::cout << B_YELLOW "\n1rst test :" RESET "\n";
std::cout << B_YELLOW "\n[1/" N_TEST "] test subject :" RESET "\n";
{
const Animal* j = new Dog();
const Animal* i = new Cat();
@@ -16,7 +17,7 @@ int main() {
delete i;
}
std::cout << B_YELLOW "\n2nd test :" RESET "\n";
std::cout << B_YELLOW "\n[2/" N_TEST "] test with brain :" RESET "\n";
{
Dog * dog;
Cat * cat1;
@@ -24,42 +25,46 @@ int main() {
Brain * brain1 = new Brain();
Brain * brain2 = new Brain();
std::cout << B_BLUE "fill brain1 with \"giraffe\" :" RESET "\n";
std::cout << B_BLUE "fill brain1 with \"giraffe\" :" RESET "\n";
brain1->putIdeas("giraffe");
std::cout << B_BLUE "print brain1 :" RESET "\n";
std::cout << B_BLUE "print brain1 :" RESET "\n";
brain1->printIdeas();
std::cout << B_BLUE "print brain2 :" RESET "\n";
std::cout << B_BLUE "print brain2 :" RESET "\n";
brain2->printIdeas();
std::cout << B_BLUE "brain2 copy brain1 :" RESET "\n";
std::cout << B_BLUE "brain2 copy brain1 :" RESET "\n";
*brain2 = *brain1;
std::cout << B_BLUE "fill brain1 with \"hippopotamus\" :" RESET "\n";
std::cout << B_BLUE "fill brain1 with \"hippopotamus\" :" RESET "\n";
brain1->putIdeas("hippopotamus");
std::cout << B_BLUE "print brain1 :" RESET "\n";
std::cout << B_BLUE "print brain1 :" RESET "\n";
brain1->printIdeas();
std::cout << B_BLUE "print brain2 :" RESET "\n";
std::cout << B_BLUE "print brain2 :" RESET "\n";
brain2->printIdeas();
std::cout << B_BLUE "create new dog with brain1 :" RESET "\n";
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";
std::cout << B_BLUE "create new cat with brain1 :" RESET "\n";
cat1 = new Cat(brain1);
std::cout << B_BLUE "cat2 copy cat1 :" RESET "\n";
std::cout << B_BLUE "cat2 copy cat1 :" RESET "\n";
cat2 = *cat1;
std::cout << B_BLUE "fill brain1 with \"zebra\" :" RESET "\n";
std::cout << B_BLUE "fill brain1 with \"zebra\" :" RESET "\n";
brain1->putIdeas("zebra");
std::cout << B_BLUE "print cat1 :" RESET "\n";
std::cout << B_BLUE "print cat1 :" RESET "\n";
cat1->printBrain();
std::cout << B_BLUE "print cat2 :" RESET "\n";
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 "\narray animal test :" RESET "\n";
std::cout << B_YELLOW "\n[3/" N_TEST "] array animal test :" RESET "\n";
{
Animal *animals[10];
int i;
@@ -74,7 +79,7 @@ int main() {
delete animals[i];
}
std::cout << B_YELLOW "\ncopy constructor test1 :" RESET "\n";
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;
@@ -82,7 +87,7 @@ int main() {
Cat a_cpy_cat(a_cat);
}
std::cout << B_YELLOW "\ncopy constructor test2 :" RESET "\n";
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;
@@ -90,7 +95,7 @@ int main() {
Cat a_cpy_cat = a_cat;
}
std::cout << B_YELLOW "\nassignation operator test1 :" RESET "\n";
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;
@@ -100,7 +105,7 @@ int main() {
a_cpy_cat = a_cat;
}
std::cout << B_YELLOW "\nassignation operator test2 :" RESET "\n";
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();

43
d04/ex01/test.cpp Normal file
View File

@@ -0,0 +1,43 @@
#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;
}