This commit is contained in:
Hugo LAMY
2022-02-24 17:12:17 +01:00
parent 0d5993dd5a
commit ba3b88f0bf
15 changed files with 507 additions and 8 deletions

View File

@@ -6,8 +6,21 @@
NAME = megaphone
CC = c++
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
#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)
@@ -22,11 +35,12 @@ D_SRCS = .
SRCS = megaphone.cpp
D_OBJS = builds
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
RM_D_OBJS = rm -rf $(D_OBJS)
ifeq "$(D_OBJS)" "."
RM_D_OBJS =
RM_OBJS = rm -f $(OBJS)
else
RM_OBJS = rm -rf $(D_OBJS)
endif
@@ -38,7 +52,7 @@ endif
all: $(NAME)
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
@@ -50,11 +64,10 @@ $(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
clean:
rm -f $(OBJS)
$(RM_OBJS)
fclean: clean
rm -f $(NAME)
$(RM_D_OBJS)
re: fclean all

BIN
d00/ex00/megaphone Executable file

Binary file not shown.

46
d04/Animal.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "Animal.hpp"
/*********************************************
* CONSTRUCTORS
*********************************************/
Animal::Animal() {
type = "animal";
return;
}
Animal::Animal( Animal const & src ) {
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Animal::~Animal() {
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";
}

28
d04/Animal.hpp Normal file
View File

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

42
d04/Cat.cpp Normal file
View File

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

27
d04/Cat.hpp Normal file
View File

@@ -0,0 +1,27 @@
#ifndef CAT_HPP
# define CAT_HPP
# include "Animal.hpp"
#include <iostream>
#include <string>
class Cat : public Animal {
public:
Cat( void );
Cat( Cat const & src );
~Cat( void );
Cat & operator=( Cat const & rhs );
void makeSound() const;
protected:
private:
};
#endif

41
d04/Dog.cpp Normal file
View File

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

27
d04/Dog.hpp Normal file
View File

@@ -0,0 +1,27 @@
#ifndef DOG_HPP
# define DOG_HPP
# include "Animal.hpp"
# include <iostream>
# include <string>
class Dog : public Animal {
public:
Dog( void );
Dog( Dog const & src );
~Dog( void );
Dog & operator=( Dog const & rhs );
void makeSound() const;
protected:
private:
};
#endif

84
d04/Makefile Normal file
View 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

50
d04/WrongAnimal.cpp Normal file
View File

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

26
d04/WrongAnimal.hpp Normal file
View File

@@ -0,0 +1,26 @@
#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

40
d04/WrongCat.cpp Normal file
View File

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

23
d04/WrongCat.hpp Normal file
View 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

52
d04/main.cpp Normal file
View File

@@ -0,0 +1,52 @@
#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();
{
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();
}
{
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();
}
{
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();
}
return 0;
}

BIN
d04/poly Executable file

Binary file not shown.