Animal, Dog and Cat classes created and finished
This commit is contained in:
@@ -40,3 +40,8 @@ void Animal::makeSound() const
|
|||||||
{
|
{
|
||||||
std::cout << "Animal weird sounds..." << std::endl;
|
std::cout << "Animal weird sounds..." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Animal::getType() const
|
||||||
|
{
|
||||||
|
return (this->type);
|
||||||
|
}
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
class Animal
|
class Animal
|
||||||
{
|
{
|
||||||
private:
|
protected:
|
||||||
std::string type;
|
std::string type;
|
||||||
public:
|
public:
|
||||||
Animal();
|
Animal();
|
||||||
@@ -26,7 +26,8 @@ class Animal
|
|||||||
Animal(const Animal &other);
|
Animal(const Animal &other);
|
||||||
Animal& operator=(const Animal &other);
|
Animal& operator=(const Animal &other);
|
||||||
|
|
||||||
virtual void makeSound() const;
|
virtual void makeSound() const;
|
||||||
|
std::string getType() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
43
ex00/Cat/Cat.cpp
Normal file
43
ex00/Cat/Cat.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Cat.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "Cat.hpp"
|
||||||
|
|
||||||
|
Cat::Cat() : Animal()
|
||||||
|
{
|
||||||
|
std::cout << "Cat has been created" << std::endl;
|
||||||
|
this->type = "Cat";
|
||||||
|
}
|
||||||
|
|
||||||
|
Cat::~Cat()
|
||||||
|
{
|
||||||
|
std::cout << "Cat has been destroyed" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cat::Cat(const Cat &other)
|
||||||
|
{
|
||||||
|
std::cout << "Cat copied" << std::endl;
|
||||||
|
*this = other;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cat& Cat::operator=(const Cat &other)
|
||||||
|
{
|
||||||
|
std::cout << "Cat copy assignment called" << std::endl;
|
||||||
|
if (this != &other)
|
||||||
|
this->type = other.type;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cat::makeSound() const
|
||||||
|
{
|
||||||
|
std::cout << "Meow!" << std::endl;
|
||||||
|
}
|
||||||
31
ex00/Cat/Cat.hpp
Normal file
31
ex00/Cat/Cat.hpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Cat.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef CAT_HPP
|
||||||
|
|
||||||
|
# define CAT_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include "../Animal/Animal.hpp"
|
||||||
|
|
||||||
|
class Cat : public Animal
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Cat();
|
||||||
|
~Cat();
|
||||||
|
Cat(const Cat &other);
|
||||||
|
Cat& operator=(const Cat &other);
|
||||||
|
|
||||||
|
void makeSound() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
43
ex00/Dog/Dog.cpp
Normal file
43
ex00/Dog/Dog.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Dog.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "Dog.hpp"
|
||||||
|
|
||||||
|
Dog::Dog() : Animal()
|
||||||
|
{
|
||||||
|
std::cout << "Dog has been created" << std::endl;
|
||||||
|
this->type = "Dog";
|
||||||
|
}
|
||||||
|
|
||||||
|
Dog::~Dog()
|
||||||
|
{
|
||||||
|
std::cout << "Dog has been destroyed" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Dog::Dog(const Dog &other)
|
||||||
|
{
|
||||||
|
std::cout << "Dog copied" << std::endl;
|
||||||
|
*this = other;
|
||||||
|
}
|
||||||
|
|
||||||
|
Dog& Dog::operator=(const Dog &other)
|
||||||
|
{
|
||||||
|
std::cout << "Dog copy assignment called" << std::endl;
|
||||||
|
if (this != &other)
|
||||||
|
this->type = other.type;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Dog::makeSound() const
|
||||||
|
{
|
||||||
|
std::cout << "Woof!" << std::endl;
|
||||||
|
}
|
||||||
32
ex00/Dog/Dog.hpp
Normal file
32
ex00/Dog/Dog.hpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Dog.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/09/06 02:27:27 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2025/09/06 02:28:09 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef DOG_HPP
|
||||||
|
|
||||||
|
# define DOG_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include "../Animal/Animal.hpp"
|
||||||
|
|
||||||
|
class Dog : public Animal
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Dog();
|
||||||
|
~Dog();
|
||||||
|
Dog(const Dog &other);
|
||||||
|
Dog& operator=(const Dog &other);
|
||||||
|
|
||||||
|
void makeSound() const;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
NAME = animals
|
NAME = animals
|
||||||
|
|
||||||
SRCS = main.cpp
|
SRCS = main.cpp Animal/Animal.cpp Dog/Dog.cpp Cat/Cat.cpp
|
||||||
OBJS = $(SRCS:.cpp=.o)
|
OBJS = $(SRCS:.cpp=.o)
|
||||||
|
|
||||||
CC = cc
|
CC = c++
|
||||||
CFLAGS = -Wall -Wextra -Werror -std=c++98
|
CFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|||||||
@@ -3,27 +3,37 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.cpp :+: :+: :+: */
|
/* main.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: hadi <hadi@student.42.fr> +#+ +:+ +#+ */
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/11/08 17:43:10 by hadi #+# #+# */
|
/* Created: 2025/11/08 17:43:10 by hadi #+# #+# */
|
||||||
/* Updated: 2025/11/08 18:01:46 by hadi ### ########.fr */
|
/* Updated: 2025/11/09 21:49:36 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "Animal/Animal.hpp"
|
#include "Animal/Animal.hpp"
|
||||||
|
#include "Dog/Dog.hpp"
|
||||||
|
#include "Cat/Cat.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
std::cout << std::endl << "----- Creating animals ------" << std::endl;
|
||||||
const Animal* meta = new Animal();
|
const Animal* meta = new Animal();
|
||||||
const Animal* j = new Dog();
|
const Animal* j = new Dog();
|
||||||
const Animal* i = new Cat();
|
const Animal* i = new Cat();
|
||||||
|
|
||||||
|
std::cout << std::endl << "----- Getting types ------" << std::endl;
|
||||||
std::cout << j->getType() << " " << std::endl;
|
std::cout << j->getType() << " " << std::endl;
|
||||||
std::cout << i->getType() << " " << std::endl;
|
std::cout << i->getType() << " " << std::endl;
|
||||||
|
|
||||||
|
std::cout << std::endl << "----- Making sounds ------" << std::endl;
|
||||||
i->makeSound();
|
i->makeSound();
|
||||||
j->makeSound();
|
j->makeSound();
|
||||||
meta->makeSound();
|
meta->makeSound();
|
||||||
|
|
||||||
|
std::cout << std::endl << "----- Free all! ------" << std::endl;
|
||||||
|
delete (meta);
|
||||||
|
delete (j);
|
||||||
|
delete (i);
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user