somewhere in ex02
This commit is contained in:
parent
a8791889b7
commit
f55c6faed3
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.vscode/settings.json
|
||||
35
Makefile
Normal file
35
Makefile
Normal file
@ -0,0 +1,35 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# Makefile :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2025/03/21 14:50:00 by whaffman #+# #+# #
|
||||
# Updated: 2025/03/21 14:58:40 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
EX = $(shell find . -maxdepth 1 -type d -name 'ex*' -exec basename {} \;)
|
||||
|
||||
all:
|
||||
for ex in $(EX); do \
|
||||
$(MAKE) -C $$ex; \
|
||||
done
|
||||
|
||||
clean:
|
||||
for ex in $(EX); do \
|
||||
$(MAKE) -C $$ex clean; \
|
||||
done
|
||||
|
||||
fclean:
|
||||
for ex in $(EX); do \
|
||||
$(MAKE) -C $$ex fclean; \
|
||||
done
|
||||
|
||||
re:
|
||||
for ex in $(EX); do \
|
||||
$(MAKE) -C $$ex re; \
|
||||
done
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
35
common.mk
Normal file
35
common.mk
Normal file
@ -0,0 +1,35 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# common.mk :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2025/03/21 15:00:16 by whaffman #+# #+# #
|
||||
# Updated: 2025/03/21 15:04:44 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
INC = -I./inc
|
||||
VPATH = src
|
||||
OBJ = $(SRC:.cpp=.o)
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) $(CFLAGS) $(INC) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
21
ex00/Makefile
Normal file
21
ex00/Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# Makefile :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
||||
# Updated: 2025/04/05 17:33:17 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME= Polymorphism
|
||||
SRC= main.cpp \
|
||||
Animal.cpp \
|
||||
Dog.cpp \
|
||||
Cat.cpp \
|
||||
WrongAnimal.cpp \
|
||||
WrongCat.cpp
|
||||
|
||||
-include ../common.mk
|
||||
30
ex00/inc/Animal.hpp
Normal file
30
ex00/inc/Animal.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Animal.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 16:53:44 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class Animal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
Animal();
|
||||
Animal(const Animal &animal);
|
||||
virtual ~Animal();
|
||||
Animal &operator=(const Animal &animal);
|
||||
|
||||
virtual void makeSound(void) const;
|
||||
std::string getType(void) const;
|
||||
};
|
||||
27
ex00/inc/Cat.hpp
Normal file
27
ex00/inc/Cat.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Cat.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:02:57 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 17:30:47 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include <string>
|
||||
|
||||
class Cat : public Animal
|
||||
{
|
||||
public:
|
||||
Cat();
|
||||
Cat(const Cat &cat);
|
||||
~Cat() override;
|
||||
Cat &operator=(const Cat &cat);
|
||||
|
||||
void makeSound(void) const override;
|
||||
};
|
||||
27
ex00/inc/Dog.hpp
Normal file
27
ex00/inc/Dog.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Dog.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:04:03 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 17:30:41 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include <string>
|
||||
|
||||
class Dog : public Animal
|
||||
{
|
||||
public:
|
||||
Dog();
|
||||
Dog(const Dog &Dog);
|
||||
~Dog() override;
|
||||
Dog &operator=(const Dog &Dog);
|
||||
|
||||
void makeSound(void) const override;
|
||||
};
|
||||
32
ex00/inc/WrongAnimal.hpp
Normal file
32
ex00/inc/WrongAnimal.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* WrongAnimal.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:24:04 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 17:26:34 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class WrongAnimal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
|
||||
public:
|
||||
WrongAnimal();
|
||||
WrongAnimal(const WrongAnimal &animal);
|
||||
virtual ~WrongAnimal();
|
||||
WrongAnimal &operator=(const WrongAnimal &animal);
|
||||
|
||||
virtual void makeSound(void) const;
|
||||
std::string getType(void) const;
|
||||
};
|
||||
28
ex00/inc/WrongCat.hpp
Normal file
28
ex00/inc/WrongCat.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* WrongCat.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:23:37 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 17:30:32 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class WrongCat : public WrongAnimal
|
||||
{
|
||||
public:
|
||||
WrongCat();
|
||||
WrongCat(const WrongCat &cat);
|
||||
~WrongCat() override;
|
||||
WrongCat &operator=(const WrongCat &cat);
|
||||
|
||||
void makeSound(void) const override;
|
||||
};
|
||||
45
ex00/src/Animal.cpp
Normal file
45
ex00/src/Animal.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Animal.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 16:59:26 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
Animal::Animal()
|
||||
{
|
||||
this->type = "Animal";
|
||||
std::cout << "Animal default constructor called" << std::endl;
|
||||
}
|
||||
Animal::Animal(const Animal &animal) : type(animal.type)
|
||||
{
|
||||
std::cout << "Animal copy constructor called" << std::endl;
|
||||
}
|
||||
Animal::~Animal()
|
||||
{
|
||||
std::cout << "Animal destructor called" << std::endl;
|
||||
}
|
||||
Animal &Animal::operator=(const Animal &animal)
|
||||
{
|
||||
std::cout << "Animal assignment operator called" << std::endl;
|
||||
if (this == &animal)
|
||||
return *this;
|
||||
|
||||
type = animal.type;
|
||||
return *this;
|
||||
}
|
||||
void Animal::makeSound(void) const
|
||||
{
|
||||
}
|
||||
std::string Animal::getType(void) const
|
||||
{
|
||||
return type;
|
||||
}
|
||||
44
ex00/src/Cat.cpp
Normal file
44
ex00/src/Cat.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Cat.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:05:34 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Cat.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
Cat::Cat()
|
||||
{
|
||||
this->type = "Cat";
|
||||
std::cout << "Cat default constructor called" << std::endl;
|
||||
}
|
||||
Cat::Cat(const Cat &cat) : Animal(cat)
|
||||
{
|
||||
this->type = cat.type;
|
||||
std::cout << "Cat copy constructor called" << std::endl;
|
||||
}
|
||||
Cat::~Cat()
|
||||
{
|
||||
std::cout << "Cat destructor called" << std::endl;
|
||||
}
|
||||
Cat &Cat::operator=(const Cat &cat)
|
||||
{
|
||||
std::cout << "Cat assignment operator called" << std::endl;
|
||||
if (this == &cat)
|
||||
return *this;
|
||||
|
||||
type = cat.type;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Cat::makeSound(void) const
|
||||
{
|
||||
std::cout << "Meow" << std::endl;
|
||||
}
|
||||
43
ex00/src/Dog.cpp
Normal file
43
ex00/src/Dog.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Dog.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:06:12 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Dog.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
Dog::Dog()
|
||||
{
|
||||
this->type = "Dog";
|
||||
std::cout << "Dog default constructor called" << std::endl;
|
||||
}
|
||||
Dog::Dog(const Dog &dog) : Animal(dog)
|
||||
{
|
||||
this->type = dog.type;
|
||||
std::cout << "Dog copy constructor called" << std::endl;
|
||||
}
|
||||
Dog::~Dog()
|
||||
{
|
||||
std::cout << "Dog destructor called" << std::endl;
|
||||
}
|
||||
Dog &Dog::operator=(const Dog &dog)
|
||||
{
|
||||
std::cout << "Dog assignment operator called" << std::endl;
|
||||
if (this == &dog)
|
||||
return *this;
|
||||
|
||||
type = dog.type;
|
||||
return *this;
|
||||
}
|
||||
void Dog::makeSound(void) const
|
||||
{
|
||||
std::cout << "Woof" << std::endl;
|
||||
}
|
||||
43
ex00/src/WrongAnimal.cpp
Normal file
43
ex00/src/WrongAnimal.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* WrongAnimal.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:26:52 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongAnimal.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
WrongAnimal::WrongAnimal()
|
||||
{
|
||||
this->type = "WrongAnimal";
|
||||
std::cout << "WrongAnimal default constructor called" << std::endl;
|
||||
}
|
||||
WrongAnimal::WrongAnimal(const WrongAnimal &animal) : type(animal.type)
|
||||
{
|
||||
std::cout << "WrongAnimal copy constructor called" << std::endl;
|
||||
}
|
||||
WrongAnimal::~WrongAnimal()
|
||||
{
|
||||
std::cout << "WrongAnimal destructor called" << std::endl;
|
||||
}
|
||||
WrongAnimal &WrongAnimal::operator=(const WrongAnimal &animal)
|
||||
{
|
||||
std::cout << "WrongAnimal assignment operator called" << std::endl;
|
||||
if (this == &animal)
|
||||
return *this;
|
||||
|
||||
type = animal.type;
|
||||
return *this;
|
||||
}
|
||||
void WrongAnimal::makeSound(void) const {}
|
||||
std::string WrongAnimal::getType(void) const
|
||||
{
|
||||
return type;
|
||||
}
|
||||
41
ex00/src/WrongCat.cpp
Normal file
41
ex00/src/WrongCat.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* WrongCat.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:28:51 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "WrongCat.hpp"
|
||||
|
||||
WrongCat::WrongCat()
|
||||
{
|
||||
this->type = "WrongCat";
|
||||
std::cout << "WrongCat default constructor called" << std::endl;
|
||||
}
|
||||
WrongCat::WrongCat(const WrongCat &cat) : WrongAnimal(cat)
|
||||
{
|
||||
this->type = cat.type;
|
||||
std::cout << "WrongCat copy constructor called" << std::endl;
|
||||
}
|
||||
WrongCat::~WrongCat()
|
||||
{
|
||||
std::cout << "WrongCat destructor called" << std::endl;
|
||||
}
|
||||
WrongCat &WrongCat::operator=(const WrongCat &cat)
|
||||
{
|
||||
std::cout << "WrongCat assignment operator called" << std::endl;
|
||||
if (this == &cat)
|
||||
return *this;
|
||||
|
||||
type = cat.type;
|
||||
return *this;
|
||||
}
|
||||
void WrongCat::makeSound(void) const
|
||||
{
|
||||
std::cout << "WrongMeow" << std::endl;
|
||||
}
|
||||
92
ex00/src/main.cpp
Normal file
92
ex00/src/main.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* main.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:06:50 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 18:01:03 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "WrongAnimal.hpp"
|
||||
#include "Dog.hpp"
|
||||
#include "Cat.hpp"
|
||||
#include "WrongCat.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
std::cout << "Construct Animal, Cat and Dog" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "construct an animal:" << std::endl;
|
||||
const Animal* animal = new Animal();
|
||||
std::cout << "construct a cat:" << std::endl;
|
||||
const Animal* cat = new Cat();
|
||||
std::cout << "construct a dog:" << std::endl;
|
||||
const Animal* dog = new Dog();
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the getType function" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "animal is of type : " << animal->getType() << std::endl;
|
||||
std::cout << "cat is of type : " << cat->getType() << std::endl;
|
||||
std::cout << "dog is of type : " << dog->getType() << std::endl;
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the makeSound function" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "animal sound : ";
|
||||
animal->makeSound();
|
||||
std::cout << std::endl;
|
||||
std::cout << "cat sound : ";
|
||||
cat->makeSound();
|
||||
std::cout << "dog sound : ";
|
||||
dog->makeSound();
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the destructor" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "delete animal:" << std::endl;
|
||||
delete animal;
|
||||
std::cout << "delete cat:" << std::endl;
|
||||
delete cat;
|
||||
std::cout << "delete dog:" << std::endl;
|
||||
delete dog;
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "Construct WrongAnimal, WrongCat" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "construct a wrongAnimal:" << std::endl;
|
||||
const WrongAnimal* wrongAnimal = new WrongAnimal();
|
||||
std::cout << "construct a wrongCat:" << std::endl;
|
||||
const WrongAnimal* wrongCat = new WrongCat();
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the getType function" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "wrongAnimal is of type : " << wrongAnimal->getType() << std::endl;
|
||||
std::cout << "wrongCat is of type : " << wrongCat->getType() << std::endl;
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the makeSound function" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "wrongAnimal sound : ";
|
||||
wrongAnimal->makeSound();
|
||||
std::cout << std::endl;
|
||||
std::cout << "wrongCat sound : ";
|
||||
wrongCat->makeSound();
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the destructor" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "delete wrongAnimal:" << std::endl;
|
||||
delete wrongAnimal;
|
||||
std::cout << "delete wrongCat:" << std::endl;
|
||||
delete wrongCat;
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
ex01/Makefile
Normal file
20
ex01/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# Makefile :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
||||
# Updated: 2025/04/05 17:53:27 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME= WithBrains
|
||||
SRC= main.cpp \
|
||||
Animal.cpp \
|
||||
Dog.cpp \
|
||||
Cat.cpp \
|
||||
Brain.cpp
|
||||
|
||||
-include ../common.mk
|
||||
30
ex01/inc/Animal.hpp
Normal file
30
ex01/inc/Animal.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Animal.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 16:53:44 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class Animal
|
||||
{
|
||||
protected:
|
||||
std::string type;
|
||||
|
||||
public:
|
||||
Animal();
|
||||
Animal(const Animal &animal);
|
||||
virtual ~Animal();
|
||||
Animal &operator=(const Animal &animal);
|
||||
|
||||
virtual void makeSound(void) const;
|
||||
std::string getType(void) const;
|
||||
};
|
||||
37
ex01/inc/Brain.hpp
Normal file
37
ex01/inc/Brain.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Brain.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:40:33 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 11:29:20 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <array>
|
||||
|
||||
#define MAX_IDEAS 10
|
||||
|
||||
class Brain
|
||||
{
|
||||
private:
|
||||
std::array<std::string, MAX_IDEAS> ideas;
|
||||
|
||||
public:
|
||||
Brain();
|
||||
Brain(const Brain &brain);
|
||||
~Brain();
|
||||
Brain &operator=(const Brain &brain);
|
||||
|
||||
void setIdea(int index, const std::string &idea);
|
||||
std::string getIdea(int index) const;
|
||||
void clearIdeas();
|
||||
void fillRandomIdeas();
|
||||
void printIdeas() const;
|
||||
void printIdea(int index) const;
|
||||
};
|
||||
32
ex01/inc/Cat.hpp
Normal file
32
ex01/inc/Cat.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Cat.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:02:57 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 17:50:55 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "Brain.hpp"
|
||||
#include <string>
|
||||
|
||||
class Cat : public Animal
|
||||
{
|
||||
private:
|
||||
Brain *brain;
|
||||
|
||||
public:
|
||||
Cat();
|
||||
Cat(const Cat &cat);
|
||||
~Cat() override;
|
||||
Cat &operator=(const Cat &cat);
|
||||
|
||||
void makeSound(void) const override;
|
||||
|
||||
};
|
||||
31
ex01/inc/Dog.hpp
Normal file
31
ex01/inc/Dog.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Dog.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:04:03 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 17:49:51 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "Brain.hpp"
|
||||
#include <string>
|
||||
|
||||
class Dog : public Animal
|
||||
{
|
||||
private:
|
||||
Brain *brain;
|
||||
|
||||
public:
|
||||
Dog();
|
||||
Dog(const Dog &Dog);
|
||||
~Dog() override;
|
||||
Dog &operator=(const Dog &Dog);
|
||||
|
||||
void makeSound(void) const override;
|
||||
};
|
||||
45
ex01/src/Animal.cpp
Normal file
45
ex01/src/Animal.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Animal.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 16:59:26 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
Animal::Animal()
|
||||
{
|
||||
this->type = "Animal";
|
||||
std::cout << "Animal default constructor called" << std::endl;
|
||||
}
|
||||
Animal::Animal(const Animal &animal) : type(animal.type)
|
||||
{
|
||||
std::cout << "Animal copy constructor called" << std::endl;
|
||||
}
|
||||
Animal::~Animal()
|
||||
{
|
||||
std::cout << "Animal destructor called" << std::endl;
|
||||
}
|
||||
Animal &Animal::operator=(const Animal &animal)
|
||||
{
|
||||
std::cout << "Animal assignment operator called" << std::endl;
|
||||
if (this == &animal)
|
||||
return *this;
|
||||
|
||||
type = animal.type;
|
||||
return *this;
|
||||
}
|
||||
void Animal::makeSound(void) const
|
||||
{
|
||||
}
|
||||
std::string Animal::getType(void) const
|
||||
{
|
||||
return type;
|
||||
}
|
||||
81
ex01/src/Brain.cpp
Normal file
81
ex01/src/Brain.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Brain.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:42:41 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 11:27:41 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Brain.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <array>
|
||||
|
||||
Brain::Brain()
|
||||
{
|
||||
std::cout << "Brain default constructor called" << std::endl;
|
||||
}
|
||||
Brain::Brain(const Brain &brain)
|
||||
{
|
||||
std::cout << "Brain copy constructor called" << std::endl;
|
||||
*this = brain;
|
||||
}
|
||||
Brain::~Brain()
|
||||
{
|
||||
std::cout << "Brain destructor called" << std::endl;
|
||||
}
|
||||
|
||||
Brain &Brain::operator=(const Brain &brain)
|
||||
{
|
||||
std::cout << "Brain assignment operator called" << std::endl;
|
||||
if (this == &brain)
|
||||
return *this;
|
||||
|
||||
for (size_t i = 0; i < ideas.size(); ++i)
|
||||
ideas[i] = brain.ideas[i];
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Brain::setIdea(int index, const std::string &idea)
|
||||
{
|
||||
if (index >= 0 && index < 100)
|
||||
ideas[index] = idea;
|
||||
}
|
||||
void Brain::fillRandomIdeas()
|
||||
{
|
||||
for (size_t i = 0; i < ideas.size(); ++i)
|
||||
ideas[i] = "Random idea " + std::to_string(i);
|
||||
}
|
||||
|
||||
std::string Brain::getIdea(int index) const
|
||||
{
|
||||
if (index >= 0 && index < 100)
|
||||
return ideas[index];
|
||||
return "";
|
||||
}
|
||||
|
||||
void Brain::clearIdeas()
|
||||
{
|
||||
for (size_t i = 0; i < ideas.size(); ++i)
|
||||
ideas[i] = "";
|
||||
}
|
||||
void Brain::printIdeas() const
|
||||
{
|
||||
for (size_t i = 0; i < ideas.size(); ++i)
|
||||
{
|
||||
if (!ideas[i].empty())
|
||||
printIdea(i);
|
||||
}
|
||||
}
|
||||
|
||||
void Brain::printIdea(int index) const
|
||||
{
|
||||
if (index >= 0 && index < 100)
|
||||
std::cout << "Idea " << index << ": " << ideas[index] << std::endl;
|
||||
}
|
||||
|
||||
|
||||
50
ex01/src/Cat.cpp
Normal file
50
ex01/src/Cat.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Cat.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:05:34 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Cat.hpp"
|
||||
#include "Brain.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
Cat::Cat() : Animal(), brain(new Brain())
|
||||
{
|
||||
type = "Cat";
|
||||
std::cout << "Cat default constructor called" << std::endl;
|
||||
}
|
||||
Cat::Cat(const Cat &cat) : Animal(cat), brain(new Brain(*cat.brain))
|
||||
{
|
||||
std::cout << "Cat copy constructor called" << std::endl;
|
||||
}
|
||||
|
||||
Cat::~Cat()
|
||||
{
|
||||
delete brain;
|
||||
std::cout << "Cat destructor called" << std::endl;
|
||||
}
|
||||
Cat &Cat::operator=(const Cat &cat)
|
||||
{
|
||||
std::cout << "Cat assignment operator called" << std::endl;
|
||||
if (this == &cat)
|
||||
return *this;
|
||||
|
||||
delete brain;
|
||||
brain = new Brain(*cat.brain);
|
||||
|
||||
type = cat.type;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Cat::makeSound(void) const
|
||||
{
|
||||
std::cout << "Meow" << std::endl;
|
||||
}
|
||||
43
ex01/src/Dog.cpp
Normal file
43
ex01/src/Dog.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Dog.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:06:12 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/06 12:26:31 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Dog.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
Dog::Dog()
|
||||
{
|
||||
this->type = "Dog";
|
||||
std::cout << "Dog default constructor called" << std::endl;
|
||||
}
|
||||
Dog::Dog(const Dog &dog) : Animal(dog)
|
||||
{
|
||||
this->type = dog.type;
|
||||
std::cout << "Dog copy constructor called" << std::endl;
|
||||
}
|
||||
Dog::~Dog()
|
||||
{
|
||||
std::cout << "Dog destructor called" << std::endl;
|
||||
}
|
||||
Dog &Dog::operator=(const Dog &dog)
|
||||
{
|
||||
std::cout << "Dog assignment operator called" << std::endl;
|
||||
if (this == &dog)
|
||||
return *this;
|
||||
|
||||
type = dog.type;
|
||||
return *this;
|
||||
}
|
||||
void Dog::makeSound(void) const
|
||||
{
|
||||
std::cout << "Woof" << std::endl;
|
||||
}
|
||||
60
ex01/src/main.cpp
Normal file
60
ex01/src/main.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* main.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/05 17:06:50 by whaffman #+# #+# */
|
||||
/* Updated: 2025/04/05 18:01:13 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Animal.hpp"
|
||||
#include "Dog.hpp"
|
||||
#include "Cat.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
std::cout << "Construct Animal, Cat and Dog" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "construct an animal:" << std::endl;
|
||||
const Animal* animal = new Animal();
|
||||
std::cout << "construct a cat:" << std::endl;
|
||||
const Animal* cat = new Cat();
|
||||
std::cout << "construct a dog:" << std::endl;
|
||||
const Animal* dog = new Dog();
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the getType function" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "animal is of type : " << animal->getType() << std::endl;
|
||||
std::cout << "cat is of type : " << cat->getType() << std::endl;
|
||||
std::cout << "dog is of type : " << dog->getType() << std::endl;
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the makeSound function" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "animal sound : ";
|
||||
animal->makeSound();
|
||||
std::cout << std::endl;
|
||||
std::cout << "cat sound : ";
|
||||
cat->makeSound();
|
||||
std::cout << "dog sound : ";
|
||||
dog->makeSound();
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
std::cout << "test the destructor" << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
std::cout << "delete animal:" << std::endl;
|
||||
delete animal;
|
||||
std::cout << "delete cat:" << std::endl;
|
||||
delete cat;
|
||||
std::cout << "delete dog:" << std::endl;
|
||||
delete dog;
|
||||
std::cout << "========================================" << std::endl << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
16
ex02/Makefile
Normal file
16
ex02/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# Makefile :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
||||
# Updated: 2025/04/05 16:49:25 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME=
|
||||
SRC= main.cpp
|
||||
|
||||
-include ../common.mk
|
||||
16
ex03/Makefile
Normal file
16
ex03/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# Makefile :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
||||
# Updated: 2025/04/05 16:49:25 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME=
|
||||
SRC= main.cpp
|
||||
|
||||
-include ../common.mk
|
||||
Loading…
Reference in New Issue
Block a user