d04 ex00 gestion virtual destructor et ajout messages constructors et destructors
This commit is contained in:
51
d04/ex00/Animal.cpp
Normal file
51
d04/ex00/Animal.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#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 ) {
|
||||
if ( this != &rhs )
|
||||
{
|
||||
type = rhs.getType();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string Animal::getType() const {return type;}
|
||||
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void Animal::makeSound() const {
|
||||
std::cout << "*sound*\n";
|
||||
}
|
||||
|
||||
29
d04/ex00/Animal.hpp
Normal file
29
d04/ex00/Animal.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef ANIMAL_HPP
|
||||
# define ANIMAL_HPP
|
||||
|
||||
# include "color.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Animal {
|
||||
|
||||
public:
|
||||
|
||||
Animal( void );
|
||||
Animal( Animal const & src );
|
||||
virtual ~Animal( void );
|
||||
Animal & operator=( Animal const & rhs );
|
||||
|
||||
virtual void makeSound() const;
|
||||
std::string getType() const;
|
||||
|
||||
protected:
|
||||
|
||||
std::string type;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
47
d04/ex00/Cat.cpp
Normal file
47
d04/ex00/Cat.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "Cat.hpp"
|
||||
|
||||
#define COPLIEN_COLOR B_CYAN
|
||||
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
Cat::Cat() {
|
||||
std::cout << COPLIEN_COLOR "Cat constructor" RESET "\n";
|
||||
type = "cat";
|
||||
return;
|
||||
}
|
||||
|
||||
Cat::Cat( Cat const & src ) {
|
||||
std::cout << COPLIEN_COLOR "Cat copy constructor" RESET "\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
Cat::~Cat() {
|
||||
std::cout << COPLIEN_COLOR "Cat destructor" RESET "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* OPERATORS
|
||||
*********************************************/
|
||||
|
||||
Cat & Cat::operator=( Cat const & rhs ) {
|
||||
Animal::operator=(rhs);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void Cat::makeSound() const {
|
||||
std::cout << "*miaow*\n";
|
||||
}
|
||||
|
||||
28
d04/ex00/Cat.hpp
Normal file
28
d04/ex00/Cat.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef CAT_HPP
|
||||
# define CAT_HPP
|
||||
|
||||
#include "color.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "Animal.hpp"
|
||||
|
||||
class Cat : public Animal {
|
||||
|
||||
public:
|
||||
|
||||
Cat( void );
|
||||
Cat( Cat const & src );
|
||||
~Cat( void );
|
||||
Cat & operator=( Cat const & rhs );
|
||||
|
||||
void makeSound() const;
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
46
d04/ex00/Dog.cpp
Normal file
46
d04/ex00/Dog.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "Dog.hpp"
|
||||
|
||||
#define COPLIEN_COLOR B_CYAN
|
||||
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
Dog::Dog() {
|
||||
std::cout << COPLIEN_COLOR "Dog constructor" RESET "\n";
|
||||
type = "dog";
|
||||
return;
|
||||
}
|
||||
|
||||
Dog::Dog( Dog const & src ) {
|
||||
std::cout << COPLIEN_COLOR "Dog copy constructor" RESET "\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
Dog::~Dog() {
|
||||
std::cout << COPLIEN_COLOR "Dog destructor" RESET "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* OPERATORS
|
||||
*********************************************/
|
||||
|
||||
Dog & Dog::operator=( Dog const & rhs ) {
|
||||
Animal::operator=(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void Dog::makeSound() const {
|
||||
std::cout << "*woof*\n";
|
||||
}
|
||||
|
||||
24
d04/ex00/Dog.hpp
Normal file
24
d04/ex00/Dog.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef DOG_HPP
|
||||
# define DOG_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
|
||||
# include "Animal.hpp"
|
||||
|
||||
class Dog : public Animal {
|
||||
|
||||
public:
|
||||
|
||||
Dog( void );
|
||||
Dog( Dog const & src );
|
||||
~Dog( void );
|
||||
Dog & operator=( Dog const & rhs );
|
||||
|
||||
void makeSound() const;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
55
d04/ex00/WrongAnimal.cpp
Normal file
55
d04/ex00/WrongAnimal.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
#define COPLIEN_COLOR B_CYAN
|
||||
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
WrongAnimal::WrongAnimal() {
|
||||
std::cout << COPLIEN_COLOR "WrongAnimal constructor" RESET "\n";
|
||||
type = "wrong_animal";
|
||||
return;
|
||||
}
|
||||
|
||||
WrongAnimal::WrongAnimal( WrongAnimal const & src ) {
|
||||
std::cout << COPLIEN_COLOR "WrongAnimal copy constructor" RESET "\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
WrongAnimal::~WrongAnimal() {
|
||||
std::cout << COPLIEN_COLOR "WrongAnimal destructor" RESET "\n";
|
||||
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";
|
||||
}
|
||||
|
||||
27
d04/ex00/WrongAnimal.hpp
Normal file
27
d04/ex00/WrongAnimal.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef WRONG_ANIMAL_HPP
|
||||
# define WRONG_ANIMAL_HPP
|
||||
|
||||
#include "color.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class WrongAnimal {
|
||||
|
||||
public:
|
||||
|
||||
WrongAnimal( void );
|
||||
WrongAnimal( WrongAnimal const & src );
|
||||
virtual ~WrongAnimal( void );
|
||||
WrongAnimal & operator=( WrongAnimal const & rhs );
|
||||
|
||||
void makeSound() const;
|
||||
std::string getType() const;
|
||||
|
||||
protected:
|
||||
|
||||
std::string type;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
45
d04/ex00/WrongCat.cpp
Normal file
45
d04/ex00/WrongCat.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
#define COPLIEN_COLOR B_CYAN
|
||||
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
WrongCat::WrongCat() {
|
||||
std::cout << COPLIEN_COLOR "WrongCat constructor" RESET "\n";
|
||||
type = "wrong_cat";
|
||||
return;
|
||||
}
|
||||
|
||||
WrongCat::WrongCat( WrongCat const & src ) {
|
||||
std::cout << COPLIEN_COLOR "WrongCat copy constructor" RESET "\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
WrongCat::~WrongCat() {
|
||||
std::cout << COPLIEN_COLOR "Cat destructor" RESET "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* OPERATORS
|
||||
*********************************************/
|
||||
|
||||
WrongCat & WrongCat::operator=( WrongCat const & rhs ) {
|
||||
WrongAnimal::operator=(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void WrongCat::makeSound() const {
|
||||
std::cout << "*miaow*\n";
|
||||
}
|
||||
24
d04/ex00/WrongCat.hpp
Normal file
24
d04/ex00/WrongCat.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef WRONG_CAT_HPP
|
||||
# define WRONG_CAT_HPP
|
||||
|
||||
#include "color.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
|
||||
class WrongCat : public WrongAnimal {
|
||||
|
||||
public:
|
||||
|
||||
WrongCat( void );
|
||||
WrongCat( WrongCat const & src );
|
||||
~WrongCat( void );
|
||||
WrongCat & operator=( WrongCat const & rhs );
|
||||
|
||||
void makeSound() const;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
24
d04/ex00/color.h
Normal file
24
d04/ex00/color.h
Normal 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
|
||||
63
d04/ex00/main.cpp
Normal file
63
d04/ex00/main.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "Animal.hpp"
|
||||
#include "Dog.hpp"
|
||||
#include "Cat.hpp"
|
||||
#include "WrongAnimal.hpp"
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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();
|
||||
delete meta;
|
||||
delete j;
|
||||
delete i;
|
||||
}
|
||||
|
||||
{
|
||||
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();
|
||||
j->makeSound();
|
||||
delete j;
|
||||
delete i;
|
||||
}
|
||||
|
||||
{
|
||||
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();
|
||||
j->makeSound();
|
||||
delete j;
|
||||
delete i;
|
||||
}
|
||||
|
||||
{
|
||||
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();
|
||||
j->makeSound();
|
||||
delete j;
|
||||
delete i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
d04/ex00/poly
Executable file
BIN
d04/ex00/poly
Executable file
Binary file not shown.
84
d04/ex01/Makefile
Normal file
84
d04/ex01/Makefile
Normal file
@@ -0,0 +1,84 @@
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
# . name = value \ . += append to a variable #
|
||||
# VARIABLES . value . != set result of command #
|
||||
# . name is case sensitive . ?= set if not already set #
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
|
||||
NAME = poly
|
||||
|
||||
#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 = main.cpp \
|
||||
Animal.cpp \
|
||||
Dog.cpp \
|
||||
Cat.cpp \
|
||||
WrongAnimal.cpp \
|
||||
WrongCat.cpp
|
||||
|
||||
D_HEADERS = .
|
||||
HEADERS = Animal.hpp \
|
||||
Dog.hpp \
|
||||
Cat.hpp \
|
||||
WrongAnimal.hpp \
|
||||
WrongCat.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
|
||||
|
||||
23
d04/ex01/WrongCat.hpp
Normal file
23
d04/ex01/WrongCat.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#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/fr.subject.pdf
Normal file
BIN
d04/ex01/fr.subject.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user