├── CPP00 ├── ex00 │ ├── Makefile │ └── megaphone.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ ├── Contact.hpp │ │ └── PhoneBook.hpp │ ├── main.cpp │ └── srcs │ │ ├── Contact.cpp │ │ └── PhoneBook.cpp └── ex02 │ ├── Makefile │ ├── includes │ └── Account.hpp │ ├── srcs │ └── Account.cpp │ └── tests.cpp ├── CPP01 ├── ex00 │ ├── Makefile │ ├── includes │ │ └── Zombie.hpp │ ├── main.cpp │ └── srcs │ │ ├── Zombie.cpp │ │ ├── newZombie.cpp │ │ └── randomChump.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ └── Zombie.hpp │ ├── main.cpp │ └── srcs │ │ ├── Zombie.cpp │ │ └── zombieHorde.cpp ├── ex02 │ ├── Makefile │ └── main.cpp ├── ex03 │ ├── Makefile │ ├── includes │ │ ├── HumanA.hpp │ │ ├── HumanB.hpp │ │ └── Weapon.hpp │ ├── main.cpp │ └── srcs │ │ ├── HumanA.cpp │ │ ├── HumanB.cpp │ │ └── Weapon.cpp ├── ex04 │ ├── Makefile │ ├── main.cpp │ └── test.txt ├── ex05 │ ├── Makefile │ ├── includes │ │ └── Harl.hpp │ ├── main.cpp │ └── srcs │ │ └── Harl.cpp └── ex06 │ ├── Makefile │ ├── includes │ └── Harl.hpp │ ├── main.cpp │ └── srcs │ └── Harl.cpp ├── CPP02 ├── ex00 │ ├── Makefile │ ├── includes │ │ └── Fixed.hpp │ ├── main.cpp │ └── srcs │ │ └── Fixed.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ └── Fixed.hpp │ ├── main.cpp │ └── srcs │ │ └── Fixed.cpp ├── ex02 │ ├── Makefile │ ├── includes │ │ └── Fixed.hpp │ ├── main.cpp │ └── srcs │ │ └── Fixed.cpp └── ex03 │ ├── Makefile │ ├── includes │ ├── BSP.hpp │ ├── Fixed.hpp │ └── Point.hpp │ ├── main.cpp │ └── srcs │ ├── Fixed.cpp │ ├── Point.cpp │ └── bsp.cpp ├── CPP03 ├── ex00 │ ├── Makefile │ ├── includes │ │ └── ClapTrap.hpp │ ├── main.cpp │ └── srcs │ │ └── ClapTrap.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ ├── ClapTrap.hpp │ │ └── ScavTrap.hpp │ ├── main.cpp │ └── srcs │ │ ├── ClapTrap.cpp │ │ └── ScavTrap.cpp ├── ex02 │ ├── Makefile │ ├── includes │ │ ├── ClapTrap.hpp │ │ ├── FragTrap.hpp │ │ └── ScavTrap.hpp │ ├── main.cpp │ └── srcs │ │ ├── ClapTrap.cpp │ │ ├── FragTrap.cpp │ │ └── ScavTrap.cpp └── ex03 │ ├── Makefile │ ├── includes │ ├── ClapTrap.hpp │ ├── DiamondTrap.hpp │ ├── FragTrap.hpp │ └── ScavTrap.hpp │ ├── main.cpp │ └── srcs │ ├── ClapTrap.cpp │ ├── DiamondTrap.cpp │ ├── FragTrap.cpp │ └── ScavTrap.cpp ├── CPP04 ├── ex00 │ ├── Makefile │ ├── includes │ │ ├── Animal.hpp │ │ ├── Cat.hpp │ │ ├── Dog.hpp │ │ ├── WrongAnimal.hpp │ │ └── WrongCat.hpp │ ├── main.cpp │ └── srcs │ │ ├── Animal.cpp │ │ ├── Cat.cpp │ │ ├── Dog.cpp │ │ ├── WrongAnimal.cpp │ │ └── WrongCat.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ ├── Animal.hpp │ │ ├── Brain.hpp │ │ ├── Cat.hpp │ │ ├── Dog.hpp │ │ ├── WrongAnimal.hpp │ │ └── WrongCat.hpp │ ├── main.cpp │ └── srcs │ │ ├── Animal.cpp │ │ ├── Brain.cpp │ │ ├── Cat.cpp │ │ ├── Dog.cpp │ │ ├── WrongAnimal.cpp │ │ └── WrongCat.cpp └── ex02 │ ├── Makefile │ ├── includes │ ├── AAnimal.hpp │ ├── Brain.hpp │ ├── Cat.hpp │ ├── Dog.hpp │ ├── WrongAnimal.hpp │ └── WrongCat.hpp │ ├── main.cpp │ └── srcs │ ├── AAnimal.cpp │ ├── Brain.cpp │ ├── Cat.cpp │ ├── Dog.cpp │ ├── WrongAnimal.cpp │ └── WrongCat.cpp ├── CPP05 ├── ex00 │ ├── Makefile │ ├── includes │ │ └── Bureaucrat.hpp │ ├── main.cpp │ └── srcs │ │ └── Bureaucrat.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ ├── Bureaucrat.hpp │ │ └── Form.hpp │ ├── main.cpp │ └── srcs │ │ ├── Bureaucrat.cpp │ │ └── Form.cpp ├── ex02 │ ├── Makefile │ ├── includes │ │ ├── AForm.hpp │ │ ├── Bureaucrat.hpp │ │ ├── PresidentialPardonForm.hpp │ │ ├── RobotomyRequestForm.hpp │ │ └── ShrubberyCreationForm.hpp │ ├── main.cpp │ └── srcs │ │ ├── AForm.cpp │ │ ├── Bureaucrat.cpp │ │ ├── PresidentialPardonForm.cpp │ │ ├── RobotomyRequestForm.cpp │ │ └── ShrubberyCreationForm.cpp └── ex03 │ ├── Makefile │ ├── includes │ ├── AForm.hpp │ ├── Bureaucrat.hpp │ ├── Intern.hpp │ ├── PresidentialPardonForm.hpp │ ├── RobotomyRequestForm.hpp │ └── ShrubberyCreationForm.hpp │ ├── main.cpp │ └── srcs │ ├── AForm.cpp │ ├── Bureaucrat.cpp │ ├── Intern.cpp │ ├── PresidentialPardonForm.cpp │ ├── RobotomyRequestForm.cpp │ └── ShrubberyCreationForm.cpp ├── CPP06 ├── ex00 │ ├── Makefile │ ├── includes │ │ └── ScalarConverter.hpp │ ├── main.cpp │ └── srcs │ │ └── ScalarConverter.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ ├── Data.hpp │ │ └── Serialization.hpp │ ├── main.cpp │ └── srcs │ │ └── Serialization.cpp └── ex02 │ ├── Makefile │ ├── includes │ ├── A.hpp │ ├── B.hpp │ ├── Base.hpp │ └── C.hpp │ ├── main.cpp │ └── srcs │ ├── Base.cpp │ └── functions.cpp ├── CPP07 ├── ex00 │ ├── Makefile │ ├── includes │ │ └── whatever.hpp │ └── main.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ └── iter.hpp │ └── main.cpp └── ex02 │ ├── Makefile │ ├── includes │ └── Array.hpp │ └── main.cpp ├── CPP08 ├── ex00 │ ├── Makefile │ ├── includes │ │ └── easyfind.hpp │ └── main.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ └── Span.hpp │ ├── main.cpp │ └── srcs │ │ └── Span.cpp └── ex02 │ ├── Makefile │ ├── includes │ └── MutantStack.hpp │ └── main.cpp ├── CPP09 ├── ex00 │ ├── Makefile │ ├── data.csv │ ├── includes │ │ └── BitcoinExchange.hpp │ ├── input.txt │ ├── main.cpp │ └── srcs │ │ └── BitcoinExchange.cpp ├── ex01 │ ├── Makefile │ ├── includes │ │ └── RPN.hpp │ ├── main.cpp │ └── srcs │ │ └── RPN.cpp └── ex02 │ ├── Makefile │ ├── includes │ └── PmergeMe.hpp │ ├── main.cpp │ └── srcs │ └── PmergeMe.cpp └── README.md /CPP00/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 3 | RM = rm -rf 4 | NAME = megaphone 5 | 6 | SRCS = megaphone.cpp 7 | OBJS = $(SRCS:.cpp=.o) 8 | 9 | all: $(NAME) 10 | 11 | $(NAME): $(OBJS) 12 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 13 | 14 | %.o: %.cpp 15 | ${CC} ${CFLAGS} -c $< -o $@ 16 | 17 | clean: 18 | $(RM) $(OBJS) 19 | 20 | fclean: clean 21 | $(RM) $(NAME) 22 | 23 | re: fclean all 24 | 25 | run: all clean 26 | ./$(NAME) "test" 27 | 28 | .PHONY: all clean fclean re run 29 | 30 | #.SILENT: -------------------------------------------------------------------------------- /CPP00/ex00/megaphone.cpp: -------------------------------------------------------------------------------- 1 | #include // cout, endl 2 | #include // EXIT_* macros 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | std::string string_to_upper(std::string arg) 8 | { 9 | for (size_t i = 0; i < arg.length(); i += 1) 10 | arg[i] = toupper(arg[i]); 11 | return arg; 12 | } 13 | 14 | int main(int argc, char **argv) 15 | { 16 | if (argc == 1) 17 | { 18 | cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << endl; 19 | return EXIT_SUCCESS; 20 | } 21 | for (int i = 1; i < argc; i += 1) 22 | cout << string_to_upper(argv[i]); 23 | cout << endl; 24 | return EXIT_SUCCESS; 25 | } -------------------------------------------------------------------------------- /CPP00/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -I./includes -fsanitize=address 3 | RM = rm -rf 4 | NAME = pb 5 | 6 | SRCS = srcs/Contact.cpp\ 7 | srcs/PhoneBook.cpp\ 8 | main.cpp 9 | OBJS = $(SRCS:.cpp=.o) 10 | 11 | all: $(NAME) 12 | 13 | $(NAME): $(OBJS) 14 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 15 | 16 | %.o: %.cpp 17 | ${CC} ${CFLAGS} -c $< -o $@ 18 | 19 | clean: 20 | $(RM) $(OBJS) 21 | 22 | fclean: clean 23 | $(RM) $(NAME) 24 | 25 | re: fclean all 26 | 27 | run: all clean 28 | ./$(NAME) 29 | 30 | .PHONY: all clean fclean re run 31 | 32 | #.SILENT: -------------------------------------------------------------------------------- /CPP00/ex01/includes/Contact.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONTACT_HPP 2 | # define CONTACT_HPP 3 | 4 | # include 5 | 6 | class Contact { 7 | public: 8 | void setFirstName(std::string str); 9 | void setLastName(std::string str); 10 | void setNickName(std::string str); 11 | void setPhoneNumber(std::string str); 12 | void setDarkestSecret(std::string str); 13 | 14 | const std::string& getFirstName(void) const; 15 | const std::string& getLastName(void) const; 16 | const std::string& getNickname(void) const; 17 | const std::string& getPhoneNumber(void) const; 18 | const std::string& getDarkestSecret(void) const; 19 | 20 | private: 21 | std::string first_name; 22 | std::string last_name; 23 | std::string nickname; 24 | std::string phone_number; 25 | std::string darkest_secret; 26 | }; 27 | 28 | #endif // CONTACT_HPP -------------------------------------------------------------------------------- /CPP00/ex01/includes/PhoneBook.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PHONEBOOK_HPP 2 | # define PHONEBOOK_HPP 3 | 4 | # include "Contact.hpp" 5 | 6 | # define MAX_CONTACTS 8 7 | # define MAX_CONTACT_INDEX 7 8 | 9 | class PhoneBook { 10 | public: 11 | /* Default constructor */ 12 | PhoneBook(); 13 | 14 | void addContact(void); 15 | void searchContact(void); 16 | 17 | private: 18 | size_t count; 19 | Contact contacts[MAX_CONTACTS]; 20 | }; 21 | 22 | #endif // PHONEBOOK_HPP -------------------------------------------------------------------------------- /CPP00/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include // cin, cout, endl 2 | #include // string class 3 | #include // exit() && EXIT_* macros 4 | #include // clearerr() 5 | #include "PhoneBook.hpp" 6 | 7 | using std::cin; 8 | using std::cout; 9 | using std::endl; 10 | 11 | static inline bool isInputValid(const std::string& input) { 12 | return input == "ADD" || input == "SEARCH" || input == "EXIT"; 13 | } 14 | 15 | void printWelcome(void) 16 | { 17 | cout << "*-------------------------------------------*\n" 18 | "| |\n" 19 | "| Kuninoto |\n" 20 | "| Phone |\n" 21 | "| Book |\n" 22 | "| |\n" 23 | "| ADD - Add a new contact |\n" 24 | "| SEARCH - Display a contact |\n" 25 | "| EXIT - Exit PhoneBook |\n" 26 | "| |\n" 27 | "*-------------------------------------------*" << endl; 28 | } 29 | 30 | std::string getInput(void) 31 | { 32 | std::string input; 33 | 34 | while (1) 35 | { 36 | cout << "pb> "; 37 | getline(cin, input); 38 | if (cin.eof() == true) 39 | { 40 | cin.clear(); 41 | clearerr(stdin); 42 | } 43 | if (!input.empty() && isInputValid(input)) 44 | break; 45 | cin.clear(); 46 | cout << "Please insert a valid input" << endl; 47 | } 48 | return input; 49 | } 50 | 51 | int main(void) 52 | { 53 | PhoneBook pb; 54 | std::string input; 55 | 56 | printWelcome(); 57 | while (1) 58 | { 59 | input = getInput(); 60 | if (input == "ADD") 61 | pb.addContact(); 62 | else if (input == "SEARCH") 63 | pb.searchContact(); 64 | else 65 | { 66 | cout << "See ya later aligator! 🦖" << endl; 67 | return EXIT_SUCCESS; 68 | } 69 | } 70 | return EXIT_SUCCESS; 71 | } 72 | -------------------------------------------------------------------------------- /CPP00/ex01/srcs/Contact.cpp: -------------------------------------------------------------------------------- 1 | #include "Contact.hpp" 2 | 3 | void Contact::setFirstName(std::string str) { 4 | this->first_name = str; 5 | } 6 | 7 | void Contact::setLastName(std::string str) { 8 | this->last_name = str; 9 | } 10 | 11 | void Contact::setNickName(std::string str) { 12 | this->nickname = str; 13 | } 14 | 15 | void Contact::setPhoneNumber(std::string str) { 16 | this->phone_number = str; 17 | } 18 | 19 | void Contact::setDarkestSecret(std::string str) { 20 | this->darkest_secret = str; 21 | } 22 | 23 | const std::string& Contact::getFirstName(void) const { 24 | return this->first_name; 25 | } 26 | 27 | const std::string& Contact::getLastName(void) const { 28 | return this->last_name; 29 | } 30 | 31 | const std::string& Contact::getNickname(void) const { 32 | return this->nickname; 33 | } 34 | 35 | const std::string& Contact::getPhoneNumber(void) const { 36 | return this->phone_number; 37 | } 38 | 39 | const std::string& Contact::getDarkestSecret(void) const { 40 | return this->darkest_secret; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /CPP00/ex01/srcs/PhoneBook.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "PhoneBook.hpp" 5 | 6 | using std::cout; 7 | using std::cin; 8 | using std::endl; 9 | 10 | std::string getFormattedField(const std::string& field) 11 | { 12 | if (field.length() > 10) 13 | return field.substr(0,9) + "."; 14 | return field; 15 | } 16 | 17 | void display_contact_info(Contact contact) 18 | { 19 | cout << endl; 20 | cout << "First name: " << contact.getFirstName() << endl; 21 | cout << "Last name: " << contact.getLastName() << endl; 22 | cout << "Nickname: " << contact.getNickname() << endl; 23 | cout << "Phone number: " << contact.getPhoneNumber() << endl; 24 | cout << "Darkest secret: " << contact.getDarkestSecret() << endl; 25 | cout << endl; 26 | } 27 | 28 | // Default constructor 29 | PhoneBook::PhoneBook() : count(0) {}; 30 | 31 | void PhoneBook::addContact(void) 32 | { 33 | Contact new_contact; 34 | std::string input; 35 | 36 | cout << "First name: "; 37 | getline(cin, input); 38 | new_contact.setFirstName(input); 39 | 40 | cout << "Last name: "; 41 | getline(cin, input); 42 | new_contact.setLastName(input); 43 | 44 | cout << "Nickname: "; 45 | getline(cin, input); 46 | new_contact.setNickName(input); 47 | 48 | while (1) 49 | { 50 | cout << "Phone number 📞: "; 51 | getline(cin, input); 52 | if (!input.empty() && input.find_first_not_of("0123456789") != std::string::npos) 53 | { 54 | cin.clear(); 55 | cout << "Phone number must be fully composed by numbers" << endl; 56 | continue; 57 | } 58 | new_contact.setPhoneNumber(input); 59 | break; 60 | } 61 | 62 | cout << "Darkest secret 🕵️‍♂️ (yes, we promise not to tell anyone 🤭): "; 63 | getline(cin, input); 64 | new_contact.setDarkestSecret(input); 65 | 66 | if (count > MAX_CONTACT_INDEX) 67 | this->contacts[count - 1] = new_contact; 68 | else 69 | { 70 | this->count += 1; 71 | this->contacts[count - 1] = new_contact; 72 | } 73 | cout << "Contact added successfully ✅" << endl; 74 | } 75 | 76 | void PhoneBook::searchContact(void) 77 | { 78 | if (count == 0) 79 | { 80 | cout << "There are no contacts to display! ❌" << endl; 81 | return ; 82 | } 83 | cout << "\n| Index|First Name| Last Name| Nickname|" << endl; 84 | for (size_t i = 0; i < this->count; i += 1) 85 | { 86 | cout << "|" << std::right << std::setw(10) << i << "|"; 87 | cout << std::right << std::setw(10) << getFormattedField(contacts[i].getFirstName()) << "|" ; 88 | cout << std::right << std::setw(10) << getFormattedField(contacts[i].getLastName()) << "|"; 89 | cout << std::right << std::setw(10) << getFormattedField(contacts[i].getNickname()) << "|"; 90 | cout << endl; 91 | } 92 | 93 | std::string index; 94 | int conv_index; 95 | 96 | while (1) 97 | { 98 | cout << "Please insert the index of the contact you'd like to display: "; 99 | getline(cin, index); 100 | if (!index.empty() && index.find_first_not_of("0123456789") == std::string::npos) 101 | { 102 | conv_index = atoi(index.c_str()); 103 | if (conv_index <= MAX_CONTACT_INDEX) 104 | break; 105 | } 106 | cin.clear(); 107 | cout << "Invalid input" << endl; 108 | } 109 | if ((size_t)conv_index > (count - 1)) 110 | cout << "There's no contact with that index 📘" << endl; 111 | else 112 | display_contact_info(this->contacts[conv_index]); 113 | } 114 | -------------------------------------------------------------------------------- /CPP00/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -I./includes 3 | RM = rm -rf 4 | NAME = payday 5 | 6 | SRCS = srcs/Account.cpp\ 7 | tests.cpp 8 | OBJS = $(SRCS:.cpp=.o) 9 | 10 | all: $(NAME) 11 | 12 | $(NAME): $(OBJS) 13 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 14 | 15 | %.o: %.cpp 16 | ${CC} ${CFLAGS} -c $< -o $@ 17 | 18 | clean: 19 | $(RM) $(OBJS) 20 | 21 | fclean: clean 22 | $(RM) $(NAME) 23 | 24 | re: fclean all 25 | 26 | run: all clean 27 | ./$(NAME) 28 | 29 | .PHONY: all clean fclean re run 30 | 31 | #.SILENT: -------------------------------------------------------------------------------- /CPP00/ex02/includes/Account.hpp: -------------------------------------------------------------------------------- 1 | // ************************************************************************** // 2 | // // 3 | // Account.hpp for GlobalBanksters United // 4 | // Created on : Thu Nov 20 19:43:15 1989 // 5 | // Last update : Wed Jan 04 14:54:06 1992 // 6 | // Made by : Brad "Buddy" McLane // 7 | // // 8 | // ************************************************************************** // 9 | 10 | 11 | #pragma once 12 | #ifndef __ACCOUNT_H__ 13 | #define __ACCOUNT_H__ 14 | 15 | // ************************************************************************** // 16 | // Account Class // 17 | // ************************************************************************** // 18 | 19 | class Account { 20 | 21 | 22 | public: 23 | 24 | typedef Account t; 25 | 26 | static int getNbAccounts( void ); 27 | static int getTotalAmount( void ); 28 | static int getNbDeposits( void ); 29 | static int getNbWithdrawals( void ); 30 | static void displayAccountsInfos( void ); 31 | 32 | Account( int initial_deposit ); 33 | ~Account( void ); 34 | 35 | void makeDeposit( int deposit ); 36 | bool makeWithdrawal( int withdrawal ); 37 | int checkAmount( void ) const; 38 | void displayStatus( void ) const; 39 | 40 | 41 | private: 42 | 43 | static int _nbAccounts; 44 | static int _totalAmount; 45 | static int _totalNbDeposits; 46 | static int _totalNbWithdrawals; 47 | 48 | static void _displayTimestamp( void ); 49 | 50 | int _accountIndex; 51 | int _amount; 52 | int _nbDeposits; 53 | int _nbWithdrawals; 54 | 55 | Account( void ); 56 | 57 | }; 58 | 59 | 60 | 61 | // ************************************************************************** // 62 | // vim: set ts=4 sw=4 tw=80 noexpandtab: // 63 | // -*- indent-tabs-mode:t; -*- 64 | // -*- mode: c++-mode; -*- 65 | // -*- fill-column: 75; comment-column: 75; -*- 66 | // ************************************************************************** // 67 | 68 | 69 | #endif /* __ACCOUNT_H__ */ -------------------------------------------------------------------------------- /CPP00/ex02/tests.cpp: -------------------------------------------------------------------------------- 1 | // ************************************************************************** // 2 | // // 3 | // tests.cpp for GlobalBanksters United // 4 | // Created on : Thu Nov 20 23:45:02 1989 // 5 | // Last update : Wed Jan 04 09:23:52 1992 // 6 | // Made by : Brad "Buddy" McLane // 7 | // // 8 | // ************************************************************************** // 9 | 10 | #include 11 | #include 12 | #include 13 | #include "Account.hpp" 14 | 15 | 16 | int main( void ) { 17 | 18 | typedef std::vector accounts_t; 19 | typedef std::vector ints_t; 20 | typedef std::pair acc_int_t; 21 | 22 | int const amounts[] = { 42, 54, 957, 432, 1234, 0, 754, 16576 }; 23 | size_t const amounts_size( sizeof(amounts) / sizeof(int) ); 24 | accounts_t accounts( amounts, amounts + amounts_size ); 25 | accounts_t::iterator acc_begin = accounts.begin(); 26 | accounts_t::iterator acc_end = accounts.end(); 27 | 28 | int const d[] = { 5, 765, 564, 2, 87, 23, 9, 20 }; 29 | size_t const d_size( sizeof(d) / sizeof(int) ); 30 | ints_t deposits( d, d + d_size ); 31 | ints_t::iterator dep_begin = deposits.begin(); 32 | ints_t::iterator dep_end = deposits.end(); 33 | 34 | int const w[] = { 321, 34, 657, 4, 76, 275, 657, 7654 }; 35 | size_t const w_size( sizeof(w) / sizeof(int) ); 36 | ints_t withdrawals( w, w + w_size ); 37 | ints_t::iterator wit_begin = withdrawals.begin(); 38 | ints_t::iterator wit_end = withdrawals.end(); 39 | 40 | Account::displayAccountsInfos(); 41 | std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); 42 | 43 | for ( acc_int_t it( acc_begin, dep_begin ); 44 | it.first != acc_end && it.second != dep_end; 45 | ++(it.first), ++(it.second) ) { 46 | 47 | (*(it.first)).makeDeposit( *(it.second) ); 48 | } 49 | 50 | Account::displayAccountsInfos(); 51 | std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); 52 | 53 | for ( acc_int_t it( acc_begin, wit_begin ); 54 | it.first != acc_end && it.second != wit_end; 55 | ++(it.first), ++(it.second) ) { 56 | 57 | (*(it.first)).makeWithdrawal( *(it.second) ); 58 | } 59 | 60 | Account::displayAccountsInfos(); 61 | std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); 62 | 63 | return 0; 64 | } 65 | 66 | 67 | // ************************************************************************** // 68 | // vim: set ts=4 sw=4 tw=80 noexpandtab: // 69 | // -*- indent-tabs-mode:t; -*- 70 | // -*- mode: c++-mode; -*- 71 | // -*- fill-column: 75; comment-column: 75; -*- 72 | // ************************************************************************** // 73 | -------------------------------------------------------------------------------- /CPP01/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Zombie newZombie randomChump\ 8 | 9 | NAME = ex00 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP01/ex00/includes/Zombie.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ZOMBIE_HPP 2 | # define ZOMBIE_HPP 3 | 4 | # include 5 | 6 | class Zombie { 7 | public: 8 | /* Destructor */ 9 | ~Zombie(void); 10 | 11 | void setName(std::string name); 12 | 13 | const std::string& getName(void) const; 14 | 15 | void announce(void); 16 | private: 17 | std::string name; 18 | }; 19 | 20 | /* Creates a Zombie instance, fill its name 21 | private attribute, and return a pointer to it */ 22 | Zombie *newZombie(std::string name); 23 | 24 | /* Creates a Zombie instance on randomChump() stack, 25 | fill its name private attribute, the zombie announces itself 26 | and then the zombie instance is dropped */ 27 | void randomChump(std::string name); 28 | 29 | #endif // ZOMBIE_HPP -------------------------------------------------------------------------------- /CPP01/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include // EXIT_* MACROS 2 | #include // std::cin, std::cout, std::endl 3 | #include 4 | #include 5 | #include "Zombie.hpp" 6 | 7 | using std::cin; 8 | using std::cout; 9 | using std::endl; 10 | 11 | int main(void) 12 | { 13 | std::string input; 14 | 15 | /* STACK */ 16 | cout << "Name the stacked Zombie 🧟‍♂️: "; 17 | getline(cin, input); 18 | if (cin.eof() == true) 19 | { 20 | cin.clear(); 21 | clearerr(stdin); 22 | cout << endl; 23 | } 24 | 25 | randomChump(input); 26 | 27 | /* HEAP ZOMBIE */ 28 | cout << "Name the heap Zombie 🧟‍♂️: "; 29 | getline(cin, input); 30 | if (cin.eof() == true) 31 | { 32 | cin.clear(); 33 | clearerr(stdin); 34 | cout << endl; 35 | } 36 | 37 | Zombie *new_zombie = newZombie(input); 38 | /* Upon main() exit, if new_zombie is not deleted 39 | the destructor is not called, causing a memory leak */ 40 | delete new_zombie; 41 | 42 | return EXIT_SUCCESS; 43 | } 44 | -------------------------------------------------------------------------------- /CPP01/ex00/srcs/Zombie.cpp: -------------------------------------------------------------------------------- 1 | #include "Zombie.hpp" 2 | #include 3 | 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | 8 | Zombie::~Zombie(void) { 9 | cout << "Zombie " << this->getName() << " died 💀" << endl; 10 | } 11 | 12 | void Zombie::setName(std::string name) { 13 | this->name = name; 14 | } 15 | 16 | const std::string& Zombie::getName(void) const { 17 | return this->name; 18 | } 19 | 20 | void Zombie::announce(void) { 21 | cout << this->getName() << ": BraiiiiiiinnnzzzZ..." << endl; 22 | } 23 | -------------------------------------------------------------------------------- /CPP01/ex00/srcs/newZombie.cpp: -------------------------------------------------------------------------------- 1 | #include "Zombie.hpp" 2 | 3 | Zombie *newZombie(std::string name) 4 | { 5 | Zombie *new_zombie = new Zombie; 6 | 7 | new_zombie->setName(name); 8 | return new_zombie; 9 | } 10 | -------------------------------------------------------------------------------- /CPP01/ex00/srcs/randomChump.cpp: -------------------------------------------------------------------------------- 1 | #include "Zombie.hpp" 2 | 3 | void randomChump(std::string name) 4 | { 5 | Zombie new_zombie; 6 | 7 | new_zombie.setName(name); 8 | new_zombie.announce(); 9 | } 10 | -------------------------------------------------------------------------------- /CPP01/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Zombie zombieHorde 8 | 9 | NAME = ex01 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP01/ex01/includes/Zombie.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ZOMBIE_HPP 2 | # define ZOMBIE_HPP 3 | 4 | # include 5 | 6 | class Zombie { 7 | public: 8 | /* Destructor */ 9 | ~Zombie(void); 10 | 11 | void setName(std::string name); 12 | 13 | const std::string& getName(void) const; 14 | 15 | void announce(void); 16 | private: 17 | std::string name; 18 | }; 19 | 20 | /* Allocate N Zombie objects in a single allocation 21 | and sets all their names to */ 22 | Zombie *zombieHorde(int N, std::string name); 23 | 24 | #endif // ZOMBIE_HPP -------------------------------------------------------------------------------- /CPP01/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include // EXIT_* MACROS 2 | #include // std::cin, std::cout, std::endl 3 | #include 4 | #include 5 | #include "Zombie.hpp" 6 | 7 | using std::cin; 8 | using std::cout; 9 | using std::endl; 10 | 11 | int main(void) 12 | { 13 | std::string zombies_name; 14 | int n; 15 | 16 | while (1) 17 | { 18 | cout << "Name the Zombies 🧟‍♂️: "; 19 | getline(cin, zombies_name); 20 | if (cin.eof() == true) 21 | { 22 | cin.clear(); 23 | clearerr(stdin); 24 | cout << endl; 25 | } 26 | if (!zombies_name.empty()) 27 | break; 28 | cout << "Please insert a name for the zombies" << endl; 29 | } 30 | 31 | cout << "How much zombies you'd like to have?" << endl; 32 | cin >> n; 33 | 34 | Zombie *zombie_army = zombieHorde(n, zombies_name); 35 | 36 | for (int i = 0; i < n; i += 1) 37 | zombie_army[i].announce(); 38 | 39 | delete[] zombie_army; 40 | return EXIT_SUCCESS; 41 | } -------------------------------------------------------------------------------- /CPP01/ex01/srcs/Zombie.cpp: -------------------------------------------------------------------------------- 1 | #include "Zombie.hpp" 2 | #include // std::cout, std::endl 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | /* Destructor */ 8 | Zombie::~Zombie(void) { 9 | cout << "Zombie " << this->getName() << " died 💀" << endl; 10 | } 11 | 12 | void Zombie::setName(std::string name) { 13 | this->name = name; 14 | } 15 | 16 | const std::string& Zombie::getName(void) const { 17 | return this->name; 18 | } 19 | 20 | void Zombie::announce(void) { 21 | cout << this->getName() << ": BraiiiiiiinnnzzzZ..." << endl; 22 | } 23 | -------------------------------------------------------------------------------- /CPP01/ex01/srcs/zombieHorde.cpp: -------------------------------------------------------------------------------- 1 | #include "Zombie.hpp" 2 | #include 3 | 4 | Zombie *zombieHorde(int N, std::string name) 5 | { 6 | Zombie *zombie_array = new Zombie[N]; 7 | 8 | for (int i = 0; i < N; i += 1) 9 | zombie_array[i].setName(name); 10 | return (zombie_array); 11 | } 12 | -------------------------------------------------------------------------------- /CPP01/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | RM = rm -rf 4 | 5 | NAME = ex02 6 | SRCS = main.cpp 7 | 8 | OBJ_DIR = obj 9 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 10 | 11 | all: $(NAME) 12 | 13 | $(NAME): $(OBJ_DIR) $(OBJS) 14 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 15 | 16 | $(OBJ_DIR): 17 | mkdir -p obj 18 | 19 | $(OBJ_DIR)/%.o: %.cpp 20 | $(CC) $(CFLAGS) -c $< -o $@ 21 | 22 | clean: 23 | $(RM) $(OBJ_DIR) 24 | 25 | fclean: clean 26 | $(RM) $(NAME) 27 | 28 | re: fclean all 29 | 30 | run: all clean 31 | ./$(NAME) 32 | 33 | .PHONY: all clean fclean re run 34 | 35 | .SILENT: -------------------------------------------------------------------------------- /CPP01/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include // std::cout, std::endl 2 | #include // EXIT_* MACROS 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | int main(void) 8 | { 9 | std::string stringOG = "HI THIS IS BRAIN"; 10 | std::string *stringPTR = &stringOG; 11 | std::string &stringREF = stringOG; 12 | 13 | cout << "Address of stringOG = " << &stringOG << endl; 14 | cout << "Thru pointer value = " << stringPTR << endl; 15 | cout << "Thru reference value = " << &stringREF << endl; 16 | 17 | cout << endl; 18 | 19 | cout << "Value of stringOG = " << stringOG << endl; 20 | cout << "Thru pointer dereferencing = " << *stringPTR << endl; 21 | cout << "Thru reference = " << stringREF << endl; 22 | 23 | return EXIT_SUCCESS; 24 | } 25 | -------------------------------------------------------------------------------- /CPP01/ex03/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Weapon HumanA HumanB 8 | 9 | NAME = ex03 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP01/ex03/includes/HumanA.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HUMAN_A_HPP 2 | # define HUMAN_A_HPP 3 | 4 | # include 5 | # include "Weapon.hpp" 6 | 7 | class HumanA { 8 | public: 9 | HumanA(std::string name, Weapon &weapon); 10 | void attack(void); 11 | 12 | private: 13 | std::string name; 14 | Weapon &weapon; 15 | }; 16 | 17 | #endif // HUMAN_A_HPP -------------------------------------------------------------------------------- /CPP01/ex03/includes/HumanB.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HUMAN_B_HPP 2 | # define HUMAN_B_HPP 3 | 4 | # include 5 | # include "Weapon.hpp" 6 | 7 | class HumanB { 8 | public: 9 | HumanB(std::string name); 10 | void setWeapon(Weapon &weapon); 11 | void attack(void); 12 | 13 | private: 14 | std::string name; 15 | Weapon *weapon; 16 | }; 17 | 18 | #endif // HUMAN_B_HPP -------------------------------------------------------------------------------- /CPP01/ex03/includes/Weapon.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WEAPON_HPP 2 | # define WEAPON_HPP 3 | 4 | # include 5 | 6 | class Weapon { 7 | public: 8 | Weapon(std::string type); 9 | const std::string &getType(void) const; 10 | void setType(std::string type); 11 | 12 | private: 13 | std::string type; 14 | }; 15 | 16 | #endif // WEAPON_HPP -------------------------------------------------------------------------------- /CPP01/ex03/main.cpp: -------------------------------------------------------------------------------- 1 | #include // EXIT_* MACROS 2 | #include "Weapon.hpp" 3 | #include "HumanA.hpp" 4 | #include "HumanB.hpp" 5 | 6 | int main(void) 7 | { 8 | { 9 | Weapon club = Weapon("crude spiked club"); 10 | 11 | HumanA bob("Bob", club); 12 | bob.attack(); 13 | club.setType("some other type of club"); 14 | bob.attack(); 15 | } 16 | { 17 | Weapon club = Weapon("crude spiked club"); 18 | 19 | HumanB jim("Jim"); 20 | jim.setWeapon(club); 21 | jim.attack(); 22 | club.setType("some other type of club"); 23 | jim.attack(); 24 | } 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /CPP01/ex03/srcs/HumanA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "HumanA.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | HumanA::HumanA(std::string name, Weapon &weapon) : name(name), weapon(weapon) {} 8 | 9 | void HumanA::attack(void) { 10 | cout << this->name << " attacks with their " << this->weapon.getType() << endl; 11 | } 12 | -------------------------------------------------------------------------------- /CPP01/ex03/srcs/HumanB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "HumanB.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | HumanB::HumanB(std::string name) : name(name) {} 8 | 9 | void HumanB::setWeapon(Weapon &weapon) { 10 | this->weapon = &weapon; 11 | } 12 | 13 | void HumanB::attack(void) { 14 | cout << this->name << " attacks using their " << weapon->getType() << endl; 15 | } 16 | -------------------------------------------------------------------------------- /CPP01/ex03/srcs/Weapon.cpp: -------------------------------------------------------------------------------- 1 | #include "Weapon.hpp" 2 | 3 | Weapon::Weapon(std::string type) : type(type) {}; 4 | 5 | const std::string &Weapon::getType(void) const { 6 | return this->type; 7 | } 8 | 9 | void Weapon::setType(std::string type) { 10 | this->type = type; 11 | } 12 | -------------------------------------------------------------------------------- /CPP01/ex04/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | RM = rm -rf 4 | 5 | NAME = wannabe_sed 6 | SRCS = main.cpp 7 | 8 | OBJ_DIR = obj 9 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 10 | 11 | all: $(NAME) 12 | 13 | $(NAME): $(OBJ_DIR) $(OBJS) 14 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 15 | 16 | $(OBJ_DIR): 17 | mkdir -p obj 18 | 19 | $(OBJ_DIR)/%.o: %.cpp 20 | $(CC) $(CFLAGS) -c $< -o $@ 21 | 22 | clean: 23 | $(RM) $(OBJ_DIR) 24 | 25 | fclean: clean 26 | $(RM) $(NAME) 27 | $(RM) *.replace 28 | 29 | re: fclean all 30 | 31 | run: all clean 32 | ./$(NAME) test.txt "replace" "abc" 33 | 34 | .PHONY: all clean fclean re run 35 | 36 | #.SILENT: -------------------------------------------------------------------------------- /CPP01/ex04/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cout; 7 | using std::cerr; 8 | using std::endl; 9 | 10 | # define ARGC_ERR "Wrong number of arguments\nUsage: ./wannabe_sed " 11 | # define FILE_OPEN_ERR "Failed to open file" 12 | 13 | int panic(const std::string& error_msg) 14 | { 15 | cerr << "wannabe_sed: error: " << error_msg << '\n'; 16 | return EXIT_FAILURE; 17 | } 18 | 19 | // ./wannabe_sed 20 | // replaces s1 with s2 21 | int main(int argc, char **argv) 22 | { 23 | if (argc != 4) 24 | return panic(ARGC_ERR); 25 | 26 | std::ifstream in_file(argv[1], std::ifstream::in); 27 | 28 | std::ofstream new_file; 29 | std::string new_file_name(argv[1]); 30 | new_file_name.append(".replace"); 31 | 32 | std::string s1(argv[2]); 33 | std::string s2(argv[3]); 34 | std::string file_content; 35 | 36 | if (!in_file.is_open()) 37 | return panic(FILE_OPEN_ERR); 38 | 39 | // get infile content until \0 (EOF) 40 | std::getline(in_file, file_content, '\0'); 41 | 42 | new_file.open(new_file_name.c_str()); 43 | 44 | int idx; 45 | // while s1 is still on the file 46 | while ((idx = file_content.find(s1)) != -1) 47 | { 48 | file_content.erase(idx, s1.length()); 49 | file_content.insert(idx, s2); 50 | } 51 | new_file << file_content; 52 | 53 | in_file.close(); 54 | new_file.close(); 55 | return EXIT_SUCCESS; 56 | } 57 | -------------------------------------------------------------------------------- /CPP01/ex04/test.txt: -------------------------------------------------------------------------------- 1 | Hello there! 2 | replace 3 | replace replace replace 4 | replace -------------------------------------------------------------------------------- /CPP01/ex05/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Harl 8 | 9 | NAME = ex05 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP01/ex05/includes/Harl.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HARL_HPP 2 | # define HARL_HPP 3 | 4 | # include 5 | 6 | class Harl { 7 | public: 8 | Harl(void); 9 | ~Harl(void); 10 | void complain(std::string level); 11 | 12 | private: 13 | void debug(void); 14 | void info(void); 15 | void warning(void); 16 | void error(void); 17 | }; 18 | 19 | // Return_Type (Class_Name::* pointer_name) (Argument_List); 20 | typedef void (Harl::* harl_func_t) (void); 21 | 22 | #endif // HARL_HPP -------------------------------------------------------------------------------- /CPP01/ex05/main.cpp: -------------------------------------------------------------------------------- 1 | #include // std::string, std::cin, std::cout, std::endl 2 | #include // EXIT_* MACROS 3 | #include // clearerr() 4 | #include "Harl.hpp" 5 | 6 | using std::cin; 7 | using std::cout; 8 | using std::endl; 9 | 10 | bool isInputValid(const std::string& input) 11 | { 12 | if (input != "DEBUG" && input != "INFO" 13 | && input != "WARNING" && input != "ERROR") 14 | { 15 | cout << "Please insert a valid level for Harl 🤓" << endl; 16 | return false; 17 | } 18 | return true; 19 | } 20 | 21 | int main(void) 22 | { 23 | Harl harl; 24 | std::string input; 25 | 26 | while (1) 27 | { 28 | cout << "Enter a level for Harl 🤓: "; 29 | getline(cin, input); 30 | if (cin.eof() == true) 31 | { 32 | cin.clear(); 33 | clearerr(stdin); 34 | cout << endl; 35 | } 36 | if (input.empty() || !isInputValid(input)) 37 | continue; 38 | harl.complain(input); 39 | } 40 | return EXIT_SUCCESS; 41 | } 42 | -------------------------------------------------------------------------------- /CPP01/ex05/srcs/Harl.cpp: -------------------------------------------------------------------------------- 1 | #include "Harl.hpp" 2 | #include 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | #define HARL_DEBUG "I love having extra bacon for my 7XL-double-cheese-triple-pickle-special-ketchup burger. I really do!" 8 | #define HARL_INFO "I cannot believe adding extra bacon costs more money.\nYou didn't put enough bacon in my burger! If you did, I wouldn't be asking for more!" 9 | #define HARL_WARNING "I think I deserve to have some extra bacon for free.\nI've been coming for years whereas you started working here since last month." 10 | #define HARL_ERROR "This is unacceptable! I want to speak to the manager now." 11 | 12 | Harl::Harl(void) {}; 13 | Harl::~Harl(void) {}; 14 | 15 | void Harl::debug(void) { 16 | cout << HARL_DEBUG << endl; 17 | } 18 | 19 | void Harl::info(void) { 20 | cout << HARL_INFO << endl; 21 | } 22 | 23 | void Harl::warning(void) { 24 | cout << HARL_WARNING << endl; 25 | } 26 | 27 | void Harl::error(void) { 28 | cout << HARL_ERROR << endl; 29 | } 30 | 31 | void Harl::complain(std::string level) 32 | { 33 | harl_func_t harl_funcs[4] = {&Harl::debug, &Harl::info, &Harl::warning, &Harl::error}; 34 | std::string levels[4] = {"DEBUG", "INFO", "WARNING", "ERROR"}; 35 | 36 | for (size_t i = 0; i < 4; i += 1) 37 | if (level == levels[i]) 38 | (this->*harl_funcs[i])(); 39 | } -------------------------------------------------------------------------------- /CPP01/ex06/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Harl 8 | 9 | NAME = harlFilter 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP01/ex06/includes/Harl.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HARL_HPP 2 | # define HARL_HPP 3 | 4 | # include 5 | 6 | class Harl { 7 | public: 8 | Harl(void); 9 | ~Harl(void); 10 | void complain(std::string level); 11 | 12 | private: 13 | void debug(void); 14 | void info(void); 15 | void warning(void); 16 | void error(void); 17 | }; 18 | 19 | // Return_Type (Class_Name::* pointer_name) (Argument_List); 20 | typedef void (Harl::* harl_func_t) (void); 21 | 22 | #endif // HARL_HPP -------------------------------------------------------------------------------- /CPP01/ex06/main.cpp: -------------------------------------------------------------------------------- 1 | #include // std::cerr, std::cout, std::endl 2 | #include // EXIT_* MACROS 3 | #include "Harl.hpp" 4 | 5 | using std::cerr; 6 | using std::cout; 7 | using std::endl; 8 | 9 | #define ARGC_ERR "Invalid arguments" 10 | #define USAGE "Usage: ./harlFilter \nlevels: DEBUG, INFO, WARNING, ERROR" 11 | 12 | int main(int argc, char **argv) 13 | { 14 | Harl harl; 15 | 16 | if (argc != 2 || !argv[1]) 17 | { 18 | cerr << ARGC_ERR << '\n'; 19 | cout << USAGE << endl; 20 | return EXIT_FAILURE; 21 | } 22 | harl.complain(argv[1]); 23 | return EXIT_SUCCESS; 24 | } 25 | -------------------------------------------------------------------------------- /CPP01/ex06/srcs/Harl.cpp: -------------------------------------------------------------------------------- 1 | #include "Harl.hpp" 2 | #include 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | #define HARL_DEBUG "I love having extra bacon for my 7XL-double-cheese-triple-pickle-special-ketchup burger. I really do!\n" 8 | #define HARL_INFO "I cannot believe adding extra bacon costs more money.\nYou didn't put enough bacon in my burger! If you did, I wouldn't be asking for more!\n" 9 | #define HARL_WARNING "I think I deserve to have some extra bacon for free.\nI've been coming for years whereas you started working here since last month.\n" 10 | #define HARL_ERROR "This is unacceptable! I want to speak to the manager now.\n" 11 | 12 | Harl::Harl(void) {}; 13 | Harl::~Harl(void) {}; 14 | 15 | 16 | void Harl::debug(void) { 17 | cout << "[ DEBUG ]" << endl; 18 | cout << HARL_DEBUG << endl; 19 | } 20 | 21 | void Harl::info(void) { 22 | cout << "[ INFO ]" << endl; 23 | cout << HARL_INFO << endl; 24 | } 25 | 26 | void Harl::warning(void) { 27 | cout << "[ WARNING ]" << '\n' 28 | << HARL_WARNING << endl; 29 | } 30 | 31 | void Harl::error(void) { 32 | cout << "[ ERROR ]" << '\n' 33 | << HARL_ERROR << endl; 34 | } 35 | 36 | void Harl::complain(std::string level) 37 | { 38 | harl_func_t harl_funcs[] = {&Harl::debug, &Harl::info, &Harl::warning, &Harl::error}; 39 | std::string levels[] = {"DEBUG", "INFO", "WARNING", "ERROR"}; 40 | 41 | for (size_t i = 0; i < 4; i += 1) 42 | { 43 | if (level == levels[i]) 44 | { 45 | switch (i) 46 | { 47 | case 0: 48 | (this->*harl_funcs[0])(); 49 | (this->*harl_funcs[1])(); 50 | (this->*harl_funcs[2])(); 51 | (this->*harl_funcs[3])(); 52 | break; 53 | case 1: 54 | (this->*harl_funcs[1])(); 55 | (this->*harl_funcs[2])(); 56 | (this->*harl_funcs[3])(); 57 | break; 58 | case 2: 59 | (this->*harl_funcs[2])(); 60 | (this->*harl_funcs[3])(); 61 | break; 62 | case 3: 63 | (this->*harl_funcs[3])(); 64 | break; 65 | default: 66 | break; 67 | } 68 | return; 69 | } 70 | } 71 | cout << "[ Probably complaining about insignificant problems ]" << endl; 72 | } -------------------------------------------------------------------------------- /CPP02/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Fixed 8 | 9 | NAME = ex00 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP02/ex00/includes/Fixed.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIXED_HPP 2 | # define FIXED_HPP 3 | 4 | class Fixed { 5 | public: 6 | // Default constructor 7 | Fixed(void); 8 | // Copy constructor 9 | Fixed(const Fixed &to_copy); 10 | // Copy assignment operator overload 11 | Fixed &operator=(const Fixed &original); 12 | // Destructor 13 | ~Fixed(void); 14 | 15 | int getRawBits(void) const; 16 | void setRawBits(int const raw); 17 | 18 | private: 19 | int value; 20 | static const int bits = 8; 21 | }; 22 | 23 | #endif // FIXED_HPP -------------------------------------------------------------------------------- /CPP02/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Fixed.hpp" 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | // DEFAULT CONSTRUCTOR 11 | Fixed a; 12 | // COPY CONSTRUCTOR 13 | Fixed b(a); 14 | // DEFAULT CONSTRUCTOR 15 | Fixed c; 16 | 17 | // COPY ASSIGNMENT OPERATOR OVERLOAD 18 | c = b; 19 | 20 | cout << a.getRawBits() << endl; 21 | cout << b.getRawBits() << endl; 22 | cout << c.getRawBits() << endl; 23 | return EXIT_SUCCESS; 24 | } -------------------------------------------------------------------------------- /CPP02/ex00/srcs/Fixed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Fixed.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Fixed::Fixed(void) : value(0) { 8 | cout << "Default constructor called" << endl; 9 | } 10 | 11 | Fixed::Fixed(const Fixed &to_copy) { 12 | cout << "Copy constructor called" << endl; 13 | *this = to_copy; 14 | } 15 | 16 | Fixed &Fixed::operator=(const Fixed &original) 17 | { 18 | cout << "Copy assignment operator called" << endl; 19 | this->setRawBits(original.getRawBits()); 20 | return *this; 21 | } 22 | 23 | Fixed::~Fixed(void) { 24 | cout << "Destructor called" << endl; 25 | }; 26 | 27 | int Fixed::getRawBits(void) const 28 | { 29 | cout << "getRawBits() member function called" << endl; 30 | return this->value; 31 | } 32 | 33 | void Fixed::setRawBits(int const raw) { 34 | this->value = raw; 35 | } 36 | -------------------------------------------------------------------------------- /CPP02/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Fixed 8 | 9 | NAME = ex01 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP02/ex01/includes/Fixed.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIXED_HPP 2 | # define FIXED_HPP 3 | 4 | # include 5 | 6 | class Fixed { 7 | public: 8 | // Default constructor 9 | Fixed(void); 10 | // Parametrized Constructors 11 | Fixed(const int inInt); 12 | Fixed(const float inFloat); 13 | // Copy constructor 14 | Fixed(const Fixed &to_copy); 15 | // Copy assignment operator overload 16 | Fixed &operator=(const Fixed &original); 17 | // Destructor 18 | ~Fixed(void); 19 | 20 | int getRawBits(void) const; 21 | void setRawBits(int const raw); 22 | 23 | float toFloat(void) const; 24 | int toInt(void) const; 25 | 26 | private: 27 | int value; 28 | static const int bits = 8; 29 | }; 30 | 31 | std::ostream &operator<<(std::ostream &stream, const Fixed &nbr); 32 | 33 | #endif // FIXED_HPP -------------------------------------------------------------------------------- /CPP02/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Fixed.hpp" 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | // DEFAULT CONSTRUCTOR 11 | Fixed a; 12 | // INT CONSTRUCTOR 13 | Fixed const b(10); 14 | // FLOAT CONSTRUCTOR 15 | Fixed const c(42.42f); 16 | // COPY CONSTRUCTOR 17 | Fixed const d(b); 18 | 19 | cout << endl; 20 | 21 | // COPY ASSIGNMENT OPERATOR 22 | a = Fixed (1234.4321f); 23 | cout << endl; 24 | 25 | cout << "a is " << a << endl; 26 | cout << "b is " << b << endl; 27 | cout << "c is " << c << endl; 28 | cout << "d is " << d << endl; 29 | 30 | cout << endl; 31 | 32 | cout << "a is " << a.toInt() << " as integer" << endl; 33 | cout << "b is " << b.toInt() << " as integer" << endl; 34 | cout << "c is " << c.toInt() << " as integer" << endl; 35 | cout << "d is " << d.toInt() << " as integer" << endl; 36 | 37 | cout << endl; 38 | return EXIT_SUCCESS; 39 | } 40 | -------------------------------------------------------------------------------- /CPP02/ex01/srcs/Fixed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Fixed.hpp" 3 | #include 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | Fixed::Fixed(void) : value(0) { 9 | cout << "Default constructor called" << endl; 10 | } 11 | 12 | Fixed::Fixed(const int inInt) 13 | : value(inInt * (1 << Fixed::bits)) { 14 | cout << "Int constructor called" << endl; 15 | } 16 | 17 | Fixed::Fixed(const float inFloat) 18 | : value( roundf(inFloat * (1 << Fixed::bits)) ) { 19 | cout << "Float constructor called" << endl; 20 | } 21 | 22 | Fixed::Fixed(const Fixed &to_copy) { 23 | cout << "Copy constructor called" << endl; 24 | *this = to_copy; 25 | } 26 | 27 | Fixed &Fixed::operator=(const Fixed &original) 28 | { 29 | cout << "Copy assignment operator called" << endl; 30 | this->setRawBits(original.getRawBits()); 31 | return *this; 32 | } 33 | 34 | Fixed::~Fixed(void) { 35 | cout << "Destructor called" << endl; 36 | }; 37 | 38 | int Fixed::getRawBits(void) const { 39 | return this->value; 40 | } 41 | 42 | void Fixed::setRawBits(int const raw) { 43 | this->value = raw; 44 | } 45 | 46 | float Fixed::toFloat(void) const { 47 | return (float)this->value / (float)(1 << Fixed::bits); 48 | } 49 | 50 | int Fixed::toInt(void) const { 51 | return this->value >> Fixed::bits; 52 | } 53 | 54 | std::ostream &operator<<(std::ostream &stream, const Fixed &nbr) 55 | { 56 | stream << nbr.toFloat(); 57 | return stream; 58 | } 59 | -------------------------------------------------------------------------------- /CPP02/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Fixed 8 | 9 | NAME = ex02 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP02/ex02/includes/Fixed.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIXED_HPP 2 | # define FIXED_HPP 3 | 4 | # include 5 | 6 | class Fixed { 7 | public: 8 | // Default constructor 9 | Fixed(void); 10 | // Parametrized Constructors 11 | Fixed(const int inInt); 12 | Fixed(const float inFloat); 13 | // Copy constructor 14 | Fixed(const Fixed &to_copy); 15 | // Copy assignment operator overload 16 | Fixed &operator=(const Fixed &original); 17 | // Destructor 18 | ~Fixed(void); 19 | bool operator>(const Fixed &nbr); 20 | bool operator<(const Fixed &nbr); 21 | bool operator<=(const Fixed &nbr); 22 | bool operator>=(const Fixed &nbr); 23 | bool operator==(const Fixed &nbr); 24 | bool operator!=(const Fixed &nbr); 25 | Fixed operator+(const Fixed &nbr); 26 | Fixed operator-(const Fixed &nbr); 27 | Fixed operator*(const Fixed &nbr); 28 | Fixed operator/(const Fixed &nbr); 29 | Fixed &operator++(void); 30 | Fixed operator++(int); 31 | Fixed &operator--(void); 32 | Fixed operator--(int); 33 | 34 | int getRawBits(void) const; 35 | void setRawBits(int const raw); 36 | 37 | float toFloat(void) const; 38 | int toInt(void) const; 39 | static Fixed& min(Fixed &a, Fixed &b); 40 | static const Fixed& min(const Fixed &a, const Fixed &b); 41 | static Fixed& max(Fixed &a, Fixed &b); 42 | static const Fixed& max(const Fixed &a, const Fixed &b); 43 | 44 | private: 45 | int value; 46 | static const int bits = 8; 47 | }; 48 | 49 | std::ostream &operator<<(std::ostream &stream, const Fixed &nbr); 50 | 51 | #endif // FIXED_HPP -------------------------------------------------------------------------------- /CPP02/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Fixed.hpp" 2 | #include 3 | #include 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | Fixed a; 11 | Fixed const b(Fixed(5.05f) * Fixed(2)); 12 | 13 | cout << a << endl; 14 | cout << ++a << endl; 15 | cout << a << endl; 16 | cout << a++ << endl; 17 | cout << a << endl; 18 | cout << b << endl; 19 | cout << Fixed::max(a, b) << endl; 20 | 21 | return EXIT_SUCCESS; 22 | } 23 | -------------------------------------------------------------------------------- /CPP02/ex03/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Fixed Point bsp 8 | 9 | NAME = bsp 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | .PHONY: all clean fclean re run 39 | 40 | #.SILENT: -------------------------------------------------------------------------------- /CPP02/ex03/includes/BSP.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BSP_HPP 2 | # define BSP_HPP 3 | 4 | # include "Fixed.hpp" 5 | # include "Point.hpp" 6 | 7 | bool bsp(Point const a, Point const b, Point const c, Point const point); 8 | 9 | #endif // BSP_HPP -------------------------------------------------------------------------------- /CPP02/ex03/includes/Fixed.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIXED_HPP 2 | # define FIXED_HPP 3 | 4 | # include 5 | 6 | class Fixed { 7 | public: 8 | // Default constructor 9 | Fixed(void); 10 | // Parametrized Constructors 11 | Fixed(const int inInt); 12 | Fixed(const float inFloat); 13 | // Copy constructor 14 | Fixed(const Fixed &to_copy); 15 | // Copy assignment operator overload 16 | Fixed& operator=(const Fixed &original); 17 | // Destructor 18 | ~Fixed(void); 19 | 20 | bool operator>(const Fixed &nbr); 21 | bool operator<(const Fixed &nbr); 22 | bool operator<=(const Fixed &nbr); 23 | bool operator>=(const Fixed &nbr); 24 | bool operator==(const Fixed &nbr); 25 | bool operator!=(const Fixed &nbr); 26 | Fixed operator+(const Fixed &nbr); 27 | Fixed operator-(const Fixed &nbr); 28 | Fixed operator*(const Fixed &nbr); 29 | Fixed operator/(const Fixed &nbr); 30 | Fixed &operator++(void); 31 | Fixed operator++(int); 32 | Fixed &operator--(void); 33 | Fixed operator--(int); 34 | 35 | int getRawBits(void) const; 36 | void setRawBits(int const raw); 37 | 38 | float toFloat(void) const; 39 | int toInt(void) const; 40 | static Fixed& min(Fixed &a, Fixed &b); 41 | static const Fixed& min(const Fixed &a, const Fixed &b); 42 | static Fixed& max(Fixed &a, Fixed &b); 43 | static const Fixed& max(const Fixed &a, const Fixed &b); 44 | 45 | private: 46 | int value; 47 | static const int bits = 8; 48 | }; 49 | 50 | std::ostream& operator<<(std::ostream &stream, const Fixed &nbr); 51 | 52 | #endif // FIXED_HPP -------------------------------------------------------------------------------- /CPP02/ex03/includes/Point.hpp: -------------------------------------------------------------------------------- 1 | #ifndef POINT_HPP 2 | # define POINT_HPP 3 | 4 | # include "Fixed.hpp" 5 | 6 | class Point { 7 | public: 8 | Point(void); 9 | Point(const float x, const float y); 10 | Point(const Point &to_copy); 11 | Point& operator=(const Point &original); 12 | ~Point(void); 13 | 14 | Fixed getX(void) const; 15 | Fixed getY(void) const; 16 | private: 17 | Fixed const x; 18 | Fixed const y; 19 | }; 20 | 21 | #endif // POINT_HPP -------------------------------------------------------------------------------- /CPP02/ex03/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BSP.hpp" 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | /* https://www.geeksforgeeks.org/check-whether-a-given-point-lies-inside-a-triangle-or-not/ */ 9 | 10 | int main(void) 11 | { 12 | if (bsp(Point(0, 0), Point(20, 0), Point(10, 30), Point(10, 15)) == true) 13 | cout << "Point is in the triangle" << endl; 14 | else 15 | cout << "Point is not in the triangle" << endl; 16 | return EXIT_SUCCESS; 17 | } 18 | -------------------------------------------------------------------------------- /CPP02/ex03/srcs/Point.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.hpp" 2 | 3 | Point::Point(void) : x(0), y(0) {}; 4 | 5 | Point::Point(const float x, const float y) : x(x), y(y) {}; 6 | 7 | Point::Point(const Point &to_copy) { 8 | *this = to_copy; 9 | }; 10 | 11 | Point& Point::operator=(const Point &original) 12 | { 13 | (Fixed)this->x = original.getX(); 14 | (Fixed)this->y = original.getY(); 15 | return *this; 16 | } 17 | 18 | Point::~Point(void) {}; 19 | 20 | Fixed Point::getX(void) const { 21 | return this->x; 22 | } 23 | 24 | Fixed Point::getY(void) const { 25 | return this->y; 26 | } 27 | -------------------------------------------------------------------------------- /CPP02/ex03/srcs/bsp.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.hpp" 2 | 3 | static Fixed abs(Fixed x) 4 | { 5 | if (x < 0) 6 | return x * -1; 7 | return x; 8 | } 9 | 10 | /* Formula 11 | ( x1*(y2 - y3) 12 | + x2*(y3 - y1) 13 | + x3*(y1 - y2) ) / 2 14 | */ 15 | 16 | static Fixed triangleArea(Point const p1, Point const p2, Point const p3) 17 | { 18 | return (abs( 19 | (p1.getX() * (p2.getY() - p3.getY()) 20 | + p2.getX() * (p3.getY() - p1.getY()) 21 | + p3.getX() * (p1.getY() - p2.getY()) 22 | ) / 2) 23 | ); 24 | } 25 | 26 | bool bsp(Point const a, Point const b, Point const c, Point const point) 27 | { 28 | Fixed abcArea = triangleArea(a, b, c); 29 | Fixed pbcArea = triangleArea(point, b, c); 30 | Fixed pacArea = triangleArea(point, c, a); 31 | Fixed pabArea = triangleArea(point, a, b); 32 | 33 | return (abcArea == (pbcArea + pacArea + pabArea)); 34 | } 35 | -------------------------------------------------------------------------------- /CPP03/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = ClapTrap 8 | 9 | NAME = ex00 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | echo "" 37 | ./ex00 38 | 39 | .PHONY: all clean fclean re run 40 | 41 | #.SILENT: -------------------------------------------------------------------------------- /CPP03/ex00/includes/ClapTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLAPTRAP_HPP 2 | # define CLAPTRAP_HPP 3 | 4 | # include 5 | 6 | class ClapTrap { 7 | public: 8 | ClapTrap(std::string name); 9 | ClapTrap(const ClapTrap &to_copy); 10 | ClapTrap& operator=(const ClapTrap &original); 11 | ~ClapTrap(void); 12 | 13 | void attack(const std::string& target); 14 | void takeDamage(unsigned int amount); 15 | void beRepaired(unsigned int amount); 16 | 17 | private: 18 | std::string name; 19 | unsigned int hit_points; 20 | unsigned int energy_points; 21 | unsigned int attack_damage; 22 | }; 23 | 24 | #endif // CLAPTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "ClapTrap.hpp" 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | ClapTrap Jubileus("Jubileus"); 11 | ClapTrap Robert("Robert"); 12 | 13 | cout << endl; 14 | 15 | Jubileus.attack("Robert"); 16 | Robert.takeDamage(5); 17 | 18 | cout << endl; 19 | 20 | Jubileus.attack("Robert"); 21 | Robert.takeDamage(1); 22 | 23 | cout << endl; 24 | 25 | Jubileus.attack("Robert"); 26 | Robert.takeDamage(1); 27 | 28 | cout << endl; 29 | 30 | Robert.beRepaired(7); 31 | 32 | cout << endl; 33 | 34 | Jubileus.attack("Robert"); 35 | Robert.takeDamage(10); 36 | 37 | cout << endl; 38 | 39 | return EXIT_SUCCESS; 40 | } 41 | -------------------------------------------------------------------------------- /CPP03/ex00/srcs/ClapTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ClapTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | ClapTrap::ClapTrap(std::string name) 8 | : name(name), hit_points(10), energy_points(10), attack_damage(0) { 9 | cout << "Default constructor <" << name << "> has been called!" << endl; 10 | } 11 | 12 | ClapTrap::ClapTrap(const ClapTrap &to_copy) 13 | { 14 | this->name = to_copy.name; 15 | this->hit_points = to_copy.hit_points; 16 | this->energy_points = to_copy.energy_points; 17 | this->attack_damage = to_copy.attack_damage; 18 | cout << "Copy constructor has been called!" << endl; 19 | } 20 | 21 | ClapTrap& ClapTrap::operator=(const ClapTrap &original) 22 | { 23 | if (this != &original) 24 | { 25 | this->name = original.name; 26 | this->hit_points = original.hit_points; 27 | this->energy_points = original.energy_points; 28 | this->attack_damage = original.attack_damage; 29 | } 30 | cout << "Copy assignment constructor has been called!" << endl; 31 | return *this; 32 | } 33 | 34 | ClapTrap::~ClapTrap(void) { 35 | cout << "Destructor has been called" << endl; 36 | }; 37 | 38 | void ClapTrap::attack(const std::string& target) 39 | { 40 | if (this->energy_points == 0) 41 | { 42 | cout << "ClapTrap is out of energy points!" << endl; 43 | return; 44 | } 45 | cout << "ClapTrap " << this->name 46 | << " attacks " << target 47 | << " causing " 48 | << "points of damage!" << endl; 49 | this->energy_points -= 1; 50 | } 51 | 52 | void ClapTrap::takeDamage(unsigned int amount) 53 | { 54 | if (amount >= this->hit_points || this->hit_points == 0) 55 | { 56 | cout << "ClapTrap " << this->name << " died" << endl; 57 | this->hit_points = 0; 58 | return; 59 | } 60 | this->hit_points -= amount; 61 | cout << "ClapTrap " << this->name 62 | << " took " << amount << " points of damage" << endl; 63 | } 64 | 65 | void ClapTrap::beRepaired(unsigned int amount) 66 | { 67 | if (this->energy_points == 0) 68 | { 69 | cout << "ClapTrap is out of energy points!" << endl; 70 | return ; 71 | } 72 | this->hit_points += amount; 73 | this->energy_points -= 1; 74 | cout << "ClapTrap " << this->name 75 | << " has been repaired and gained " 76 | << amount << " hit points" << endl; 77 | } 78 | -------------------------------------------------------------------------------- /CPP03/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = ClapTrap ScavTrap 8 | 9 | NAME = ex01 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | echo "" 37 | ./ex01 38 | 39 | .PHONY: all clean fclean re run 40 | 41 | #.SILENT: -------------------------------------------------------------------------------- /CPP03/ex01/includes/ClapTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLAPTRAP_HPP 2 | # define CLAPTRAP_HPP 3 | 4 | # include 5 | 6 | class ClapTrap { 7 | public: 8 | ClapTrap(std::string name); 9 | ClapTrap(const ClapTrap &to_copy); 10 | ClapTrap& operator=(const ClapTrap &original); 11 | ~ClapTrap(void); 12 | 13 | void attack(const std::string& target); 14 | void takeDamage(unsigned int amount); 15 | void beRepaired(unsigned int amount); 16 | 17 | protected: 18 | std::string name; 19 | unsigned int hit_points; 20 | unsigned int energy_points; 21 | unsigned int attack_damage; 22 | }; 23 | 24 | #endif // CLAPTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex01/includes/ScavTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SCAVTRAP_HPP 2 | # define SCAVTRAP_HPP 3 | 4 | # include "ClapTrap.hpp" 5 | 6 | class ScavTrap : public ClapTrap { 7 | public: 8 | ScavTrap(std::string name); 9 | ~ScavTrap(void); 10 | 11 | void attack(const std::string& target); 12 | void guardGate(void); 13 | 14 | private: 15 | ScavTrap(void); 16 | }; 17 | 18 | #endif // SCAVTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "ScavTrap.hpp" 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | ScavTrap Jubileus("Jubileus"); 11 | ScavTrap Robert("Robert"); 12 | 13 | cout << endl; 14 | 15 | Jubileus.attack("Robert"); 16 | Robert.takeDamage(5); 17 | 18 | cout << endl; 19 | 20 | Jubileus.attack("Robert"); 21 | Robert.takeDamage(1); 22 | 23 | cout << endl; 24 | 25 | Jubileus.attack("Robert"); 26 | Robert.takeDamage(1); 27 | 28 | //Jubileus.attack("Robert"); 29 | //Robert.takeDamage(200); 30 | 31 | cout << endl; 32 | 33 | Robert.beRepaired(7); 34 | 35 | cout << endl; 36 | 37 | Jubileus.attack("Robert"); 38 | Robert.takeDamage(10); 39 | 40 | cout << endl; 41 | 42 | Jubileus.guardGate(); 43 | Robert.guardGate(); 44 | 45 | cout << endl; 46 | 47 | return EXIT_SUCCESS; 48 | } 49 | -------------------------------------------------------------------------------- /CPP03/ex01/srcs/ClapTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ClapTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | ClapTrap::ClapTrap(std::string name) 8 | : name(name), hit_points(10), energy_points(10), attack_damage(0) { 9 | cout << "Super Class (ClapTrap) <" << name << "> constructor has been called!" << endl; 10 | } 11 | 12 | ClapTrap::ClapTrap(const ClapTrap &to_copy) 13 | { 14 | *this = to_copy; 15 | cout << "Super Class (ClapTrap) copy constructor has been called!" << endl; 16 | } 17 | 18 | ClapTrap& ClapTrap::operator=(const ClapTrap &original) 19 | { 20 | this->name = original.name; 21 | this->hit_points = original.hit_points; 22 | this->energy_points = original.energy_points; 23 | this->attack_damage = original.attack_damage; 24 | cout << "Super Class (ClapTrap) copy assignment constructor has been called!" << endl; 25 | return *this; 26 | } 27 | 28 | ClapTrap::~ClapTrap(void) { 29 | cout << "Super Class (ClapTrap) destructor has been called" << endl; 30 | }; 31 | 32 | void ClapTrap::attack(const std::string& target) 33 | { 34 | if (this->energy_points == 0) 35 | { 36 | cout << "ClapTrap is out of energy points!" << endl; 37 | return; 38 | } 39 | cout << "ClapTrap " << this->name 40 | << " attacks " << target 41 | << " causing " 42 | << "points of damage!" << endl; 43 | this->energy_points -= 1; 44 | } 45 | 46 | void ClapTrap::takeDamage(unsigned int amount) 47 | { 48 | if (amount >= this->hit_points || this->hit_points == 0) 49 | { 50 | cout << "ClapTrap " << this->name << " died" << endl; 51 | this->hit_points = 0; 52 | return; 53 | } 54 | this->hit_points -= amount; 55 | cout << "ClapTrap " << this->name 56 | << " took " << amount << " points of damage" << endl; 57 | } 58 | 59 | void ClapTrap::beRepaired(unsigned int amount) 60 | { 61 | if (this->energy_points == 0) 62 | { 63 | cout << "ClapTrap " << this->name << " is out of energy points!" << endl; 64 | return ; 65 | } 66 | this->hit_points += amount; 67 | this->energy_points -= 1; 68 | cout << "ClapTrap " << this->name 69 | << " has been repaired and gained " 70 | << amount << " hit points" << endl; 71 | } 72 | -------------------------------------------------------------------------------- /CPP03/ex01/srcs/ScavTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ScavTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | ScavTrap::ScavTrap(std::string name) : ClapTrap(name) 8 | { 9 | this->hit_points = 100; 10 | this->energy_points = 50; 11 | this->attack_damage = 20; 12 | cout << "Sub Class (ScavTrap) constructor has been called" << endl; 13 | } 14 | 15 | ScavTrap::~ScavTrap(void) { 16 | cout << "Sub Class (ScavTrap) destructor has been called" << endl; 17 | } 18 | 19 | void ScavTrap::attack(const std::string& target) 20 | { 21 | if (this->energy_points == 0) 22 | { 23 | cout << "ScavTrap is out of energy points!" << endl; 24 | return; 25 | } 26 | cout << "ScavTrap " << this->name 27 | << " attacks " << target 28 | << " causing " 29 | << "points of damage!" << endl; 30 | this->energy_points -= 1; 31 | } 32 | 33 | void ScavTrap::guardGate(void) { 34 | cout << "ScarvTrap " << this->name << " is now in Gate keeper mode" << endl; 35 | } 36 | -------------------------------------------------------------------------------- /CPP03/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = ClapTrap ScavTrap FragTrap 8 | 9 | NAME = ex02 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | echo "" 37 | ./ex02 38 | 39 | .PHONY: all clean fclean re run 40 | 41 | #.SILENT: -------------------------------------------------------------------------------- /CPP03/ex02/includes/ClapTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLAPTRAP_HPP 2 | # define CLAPTRAP_HPP 3 | 4 | # include 5 | 6 | class ClapTrap { 7 | public: 8 | ClapTrap(std::string name); 9 | ClapTrap(const ClapTrap &to_copy); 10 | ClapTrap& operator=(const ClapTrap &original); 11 | ~ClapTrap(void); 12 | 13 | void attack(const std::string& target); 14 | void takeDamage(unsigned int amount); 15 | void beRepaired(unsigned int amount); 16 | 17 | protected: 18 | std::string name; 19 | unsigned int hit_points; 20 | unsigned int energy_points; 21 | unsigned int attack_damage; 22 | }; 23 | 24 | #endif // CLAPTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex02/includes/FragTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FRAGTRAP_HPP 2 | # define FRAGTRAP_HPP 3 | 4 | # include "ClapTrap.hpp" 5 | 6 | class FragTrap : public ClapTrap 7 | { 8 | public: 9 | FragTrap(std::string name); 10 | ~FragTrap(void); 11 | 12 | void highFivesGuys(void); 13 | 14 | private: 15 | FragTrap(void); 16 | }; 17 | 18 | #endif // FRAGTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex02/includes/ScavTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SCAVTRAP_HPP 2 | # define SCAVTRAP_HPP 3 | 4 | # include "ClapTrap.hpp" 5 | 6 | class ScavTrap : public ClapTrap { 7 | public: 8 | ScavTrap(std::string name); 9 | ~ScavTrap(void); 10 | 11 | void attack(const std::string& target); 12 | void guardGate(void); 13 | 14 | private: 15 | ScavTrap(void); 16 | }; 17 | 18 | #endif // SCAVTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "FragTrap.hpp" 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | FragTrap John("John"); 11 | FragTrap Albert("Albert"); 12 | 13 | cout << endl; 14 | 15 | John.attack("Albert"); 16 | Albert.takeDamage(30); 17 | 18 | cout << endl; 19 | 20 | John.attack("Albert"); 21 | Albert.takeDamage(30); 22 | 23 | cout << endl; 24 | 25 | John.attack("Albert"); 26 | Albert.takeDamage(30); 27 | 28 | cout << endl; 29 | 30 | John.attack("Albert"); 31 | Albert.takeDamage(30); 32 | 33 | cout << endl; 34 | 35 | Albert.beRepaired(100); 36 | 37 | cout << endl; 38 | 39 | Albert.highFivesGuys(); 40 | 41 | cout << endl; 42 | 43 | Albert.takeDamage(100); 44 | 45 | return EXIT_SUCCESS; 46 | } 47 | -------------------------------------------------------------------------------- /CPP03/ex02/srcs/ClapTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ClapTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | ClapTrap::ClapTrap(std::string name) 8 | : name(name), hit_points(10), energy_points(10), attack_damage(0) { 9 | cout << "Super Class (ClapTrap) <" << name << "> constructor has been called!" << endl; 10 | } 11 | 12 | ClapTrap::ClapTrap(const ClapTrap &to_copy) 13 | { 14 | this->name = to_copy.name; 15 | this->hit_points = to_copy.hit_points; 16 | this->energy_points = to_copy.energy_points; 17 | this->attack_damage = to_copy.attack_damage; 18 | 19 | cout << "Super Class (ClapTrap) copy constructor has been called!" << endl; 20 | } 21 | 22 | ClapTrap& ClapTrap::operator=(const ClapTrap &original) 23 | { 24 | if (this != &original) 25 | { 26 | this->name = original.name; 27 | this->hit_points = original.hit_points; 28 | this->energy_points = original.energy_points; 29 | this->attack_damage = original.attack_damage; 30 | } 31 | cout << "Super Class (ClapTrap) copy assignment constructor has been called!" << endl; 32 | return *this; 33 | } 34 | 35 | ClapTrap::~ClapTrap(void) { 36 | cout << "Super Class (ClapTrap) destructor has been called" << endl; 37 | }; 38 | 39 | void ClapTrap::attack(const std::string& target) 40 | { 41 | if (this->energy_points == 0) { 42 | cout << "ClapTrap is out of energy points!" << endl; 43 | return; 44 | } 45 | cout << "ClapTrap " << this->name 46 | << " attacks " << target 47 | << " causing " 48 | << "points of damage!" << endl; 49 | this->energy_points -= 1; 50 | } 51 | 52 | void ClapTrap::takeDamage(unsigned int amount) 53 | { 54 | if (amount >= this->hit_points || this->hit_points == 0) 55 | { 56 | cout << "ClapTrap " << this->name << " died" << endl; 57 | this->hit_points = 0; 58 | return; 59 | } 60 | this->hit_points -= amount; 61 | cout << "ClapTrap " << this->name 62 | << " took " << amount << " points of damage" << endl; 63 | } 64 | 65 | void ClapTrap::beRepaired(unsigned int amount) 66 | { 67 | if (this->energy_points == 0) 68 | { 69 | cout << "ClapTrap is out of energy points!" << endl; 70 | return ; 71 | } 72 | this->hit_points += amount; 73 | this->energy_points -= 1; 74 | cout << "ClapTrap " << this->name 75 | << " has been repaired and gained " 76 | << amount << " hit points" << endl; 77 | } 78 | -------------------------------------------------------------------------------- /CPP03/ex02/srcs/FragTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "FragTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | FragTrap::FragTrap(std::string name) : ClapTrap(name) 8 | { 9 | this->hit_points = 100; 10 | this->energy_points = 100; 11 | this->attack_damage = 30; 12 | cout << "Sub Class (FragTrap) constructor has been called" << endl; 13 | } 14 | 15 | FragTrap::~FragTrap(void) { 16 | cout << "Sub Class (FragTrap) destructor has been called" << endl; 17 | } 18 | 19 | void FragTrap::highFivesGuys(void) { 20 | if (this->energy_points == 0) { 21 | cout << "FragTrap "<< this->name << " is out of energy points!" << endl; 22 | return; 23 | } 24 | cout << "FragTrap " << this->name << " says: High Fives!" << endl; 25 | this->energy_points -= 1; 26 | } 27 | -------------------------------------------------------------------------------- /CPP03/ex02/srcs/ScavTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ScavTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | ScavTrap::ScavTrap(std::string name) : ClapTrap(name) 8 | { 9 | this->hit_points = 100; 10 | this->energy_points = 50; 11 | this->attack_damage = 20; 12 | cout << "Sub Class (ScavTrap) constructor has been called" << endl; 13 | } 14 | 15 | ScavTrap::~ScavTrap(void) { 16 | cout << "Sub Class (ScavTrap) destructor has been called" << endl; 17 | } 18 | 19 | void ScavTrap::attack(const std::string& target) 20 | { 21 | if (this->energy_points == 0) { 22 | cout << "ScavTrap is out of energy points!" << endl; 23 | return; 24 | } 25 | cout << "ScavTrap " << this->name 26 | << " attacks " << target 27 | << " causing " 28 | << "points of damage!" << endl; 29 | this->energy_points -= 1; 30 | } 31 | 32 | void ScavTrap::guardGate(void) { 33 | cout << "ScavTrap " << this->name << " is now in gate keeper mode" << endl; 34 | } 35 | -------------------------------------------------------------------------------- /CPP03/ex03/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = ClapTrap ScavTrap FragTrap DiamondTrap 8 | 9 | NAME = ex03 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | echo "" 37 | ./$(NAME) 38 | 39 | .PHONY: all clean fclean re run 40 | 41 | #.SILENT: -------------------------------------------------------------------------------- /CPP03/ex03/includes/ClapTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLAPTRAP_HPP 2 | # define CLAPTRAP_HPP 3 | 4 | # include 5 | 6 | class ClapTrap { 7 | public: 8 | ClapTrap(void); 9 | ClapTrap(std::string name); 10 | ClapTrap(const ClapTrap &to_copy); 11 | ClapTrap& operator=(const ClapTrap &original); 12 | ~ClapTrap(void); 13 | 14 | void attack(const std::string& target); 15 | void takeDamage(unsigned int amount); 16 | void beRepaired(unsigned int amount); 17 | 18 | protected: 19 | std::string name; 20 | unsigned int hit_points; 21 | unsigned int energy_points; 22 | unsigned int attack_damage; 23 | }; 24 | 25 | #endif // CLAPTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex03/includes/DiamondTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DIAMONDTRAP_HPP 2 | # define DIAMONDTRAP_HPP 3 | 4 | # include 5 | # include "FragTrap.hpp" 6 | # include "ScavTrap.hpp" 7 | 8 | class DiamondTrap : public FragTrap, public ScavTrap { 9 | public: 10 | DiamondTrap(void); 11 | DiamondTrap(std::string name); 12 | DiamondTrap(const DiamondTrap &original); 13 | DiamondTrap &operator=(const DiamondTrap &original); 14 | ~DiamondTrap(void); 15 | 16 | using ScavTrap::attack; 17 | void whoAmI(void); 18 | 19 | private: 20 | std::string name; 21 | }; 22 | 23 | #endif // DIAMONDTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex03/includes/FragTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FRAGTRAP_HPP 2 | # define FRAGTRAP_HPP 3 | 4 | # include "ClapTrap.hpp" 5 | 6 | class FragTrap : virtual public ClapTrap 7 | { 8 | public: 9 | FragTrap(void); 10 | FragTrap(std::string name); 11 | ~FragTrap(void); 12 | 13 | void highFivesGuys(void); 14 | 15 | static const int defaultHitPoints = 100; 16 | static const int defaultEnergyPoints = 100; 17 | static const int defaultAttackDamage = 30; 18 | }; 19 | 20 | #endif // FRAGTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex03/includes/ScavTrap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SCAVTRAP_HPP 2 | # define SCAVTRAP_HPP 3 | 4 | # include "ClapTrap.hpp" 5 | 6 | class ScavTrap : virtual public ClapTrap 7 | { 8 | public: 9 | ScavTrap(void); 10 | ScavTrap(std::string name); 11 | ~ScavTrap(void); 12 | 13 | void attack(const std::string& target); 14 | void guardGate(void); 15 | 16 | static const int defaultHitPoints = 100; 17 | static const int defaultEnergyPoints = 50; 18 | static const int defaultAttackDamage = 20; 19 | }; 20 | 21 | #endif // SCAVTRAP_HPP -------------------------------------------------------------------------------- /CPP03/ex03/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "DiamondTrap.hpp" 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | DiamondTrap Bozo("Bozo"); 11 | /* DiamondTrap Bozo("Bozo"); 12 | DiamondTrap Rato("Rato"); 13 | DiamondTrap Clone; 14 | 15 | cout << endl; 16 | 17 | Rato.attack("Bozo"); 18 | Bozo.takeDamage(30); 19 | 20 | cout << endl; 21 | 22 | Rato.attack("Bozo"); 23 | Bozo.takeDamage(30); 24 | 25 | cout << endl; 26 | 27 | Rato.attack("Bozo"); 28 | Bozo.takeDamage(30); 29 | 30 | cout << endl; 31 | 32 | Clone = Bozo; 33 | 34 | cout << endl; 35 | 36 | Rato.attack("Bozo Clone"); 37 | Clone.takeDamage(30); 38 | 39 | cout << endl; 40 | 41 | Rato.guardGate(); 42 | 43 | cout << endl; 44 | 45 | Clone.beRepaired(100); 46 | Clone.highFivesGuys(); 47 | 48 | cout << endl; 49 | 50 | Rato.whoAmI(); 51 | 52 | cout << endl; 53 | 54 | return EXIT_SUCCESS; */ 55 | } 56 | -------------------------------------------------------------------------------- /CPP03/ex03/srcs/ClapTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ClapTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | ClapTrap::ClapTrap(void) {}; 8 | 9 | ClapTrap::ClapTrap(std::string name) 10 | : name(name), hit_points(10), energy_points(10), attack_damage(0) { 11 | cout << "Super Class (ClapTrap) constructor has been called!" << endl; 12 | } 13 | 14 | ClapTrap::ClapTrap(const ClapTrap &to_copy) 15 | { 16 | *this = to_copy; 17 | cout << "Super Class (ClapTrap) copy constructor has been called!" << endl; 18 | } 19 | 20 | ClapTrap& ClapTrap::operator=(const ClapTrap &original) 21 | { 22 | if (this != &original) 23 | { 24 | this->name = original.name; 25 | this->hit_points = original.hit_points; 26 | this->energy_points = original.energy_points; 27 | this->attack_damage = original.attack_damage; 28 | } 29 | cout << "Super Class (ClapTrap) copy assignment constructor has been called!" << endl; 30 | return *this; 31 | } 32 | 33 | ClapTrap::~ClapTrap(void) { 34 | cout << "Super Class (ClapTrap) destructor has been called" << endl; 35 | }; 36 | 37 | void ClapTrap::attack(const std::string& target) 38 | { 39 | if (this->energy_points == 0) 40 | { 41 | cout << "ClapTrap is out of energy points!" << endl; 42 | return; 43 | } 44 | cout << "ClapTrap " << this->name 45 | << " attacks " << target 46 | << " causing " 47 | << "points of damage!" << endl; 48 | this->energy_points -= 1; 49 | } 50 | 51 | void ClapTrap::takeDamage(unsigned int amount) 52 | { 53 | if (amount >= this->hit_points || this->hit_points == 0) 54 | { 55 | cout << "ClapTrap " << this->name << " died" << endl; 56 | this->hit_points = 0; 57 | return; 58 | } 59 | this->hit_points -= amount; 60 | cout << "ClapTrap " << this->name 61 | << " took " << amount << " points of damage" << endl; 62 | } 63 | 64 | void ClapTrap::beRepaired(unsigned int amount) 65 | { 66 | if (this->energy_points == 0) 67 | { 68 | cout << "ClapTrap is out of energy points!" << endl; 69 | return ; 70 | } 71 | this->hit_points += amount; 72 | this->energy_points -= 1; 73 | cout << "ClapTrap " << this->name 74 | << " has been repaired and gained " 75 | << amount << " hit points" << endl; 76 | } 77 | -------------------------------------------------------------------------------- /CPP03/ex03/srcs/DiamondTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DiamondTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | DiamondTrap::DiamondTrap(void) : FragTrap(), ScavTrap() {}; 8 | 9 | DiamondTrap::DiamondTrap(std::string name) 10 | : ClapTrap(name + "_clap_name"), FragTrap(name), ScavTrap(name) 11 | { 12 | this->name = name; 13 | this->hit_points = FragTrap::defaultHitPoints; 14 | this->energy_points = ScavTrap::defaultEnergyPoints; 15 | this->attack_damage = FragTrap::defaultAttackDamage; 16 | 17 | cout << "Sub Class (DiamondTrap) constructor has been called" << endl; 18 | } 19 | 20 | DiamondTrap::DiamondTrap(const DiamondTrap &original) 21 | : ClapTrap(original), FragTrap(original), ScavTrap(original) 22 | { 23 | *this = original; 24 | cout << "Sub Class (DiamondTrap) copy constructor has been called" << endl; 25 | } 26 | 27 | DiamondTrap &DiamondTrap::operator=(const DiamondTrap &original) 28 | { 29 | this->name = original.name; 30 | ClapTrap::name = (this->name + "_clap_name"); 31 | this->hit_points = original.hit_points; 32 | this->energy_points = original.energy_points; 33 | this->attack_damage = original.attack_damage; 34 | 35 | cout << "Sub Class (DiamondTrap) Assignment operator called" << endl; 36 | return *this; 37 | } 38 | 39 | DiamondTrap::~DiamondTrap(void) { 40 | cout << "Sub Class (DiamondTrap) destructor has been called" << endl; 41 | } 42 | 43 | void DiamondTrap::whoAmI(void) { 44 | cout << "I AM:\nDiamondTrap: " << this->name << "\nClapTrap: " << ClapTrap::name << endl; 45 | } 46 | -------------------------------------------------------------------------------- /CPP03/ex03/srcs/FragTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "FragTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | FragTrap::FragTrap(void) : ClapTrap(){}; 8 | 9 | FragTrap::FragTrap(std::string name) : ClapTrap(name) 10 | { 11 | this->hit_points = this->defaultHitPoints; 12 | this->energy_points = this->defaultEnergyPoints; 13 | this->attack_damage = this->defaultAttackDamage; 14 | cout << "Sub Class (FragTrap) constructor has been called" << endl; 15 | } 16 | 17 | FragTrap::~FragTrap(void) 18 | { 19 | cout << "Sub Class (FragTrap) destructor has been called" << endl; 20 | } 21 | 22 | void FragTrap::highFivesGuys(void) 23 | { 24 | if (this->energy_points == 0) 25 | { 26 | cout << "FragTrap " << this->name << " is out of energy points!" << endl; 27 | return; 28 | } 29 | cout << "FragTrap " << this->name << " says: High Fives!" << endl; 30 | this->energy_points -= 1; 31 | } 32 | -------------------------------------------------------------------------------- /CPP03/ex03/srcs/ScavTrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ScavTrap.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | ScavTrap::ScavTrap(void){}; 8 | 9 | ScavTrap::ScavTrap(std::string name) : ClapTrap(name) 10 | { 11 | this->hit_points = this->defaultHitPoints; 12 | this->energy_points = this->defaultEnergyPoints; 13 | this->attack_damage = this->defaultAttackDamage; 14 | cout << "Sub Class (ScavTrap) constructor has been called" << endl; 15 | } 16 | 17 | ScavTrap::~ScavTrap(void) 18 | { 19 | cout << "Sub Class (ScavTrap) destructor has been called" << endl; 20 | } 21 | 22 | void ScavTrap::attack(const std::string &target) 23 | { 24 | if (this->energy_points == 0) 25 | { 26 | cout << "ScavTrap is out of energy points!" << endl; 27 | return; 28 | } 29 | cout << "ScavTrap " << this->name 30 | << " attacks " << target 31 | << " causing " 32 | << "points of damage!" << endl; 33 | this->energy_points -= 1; 34 | } 35 | 36 | void ScavTrap::guardGate(void) 37 | { 38 | cout << "ScavTrap " << this->name << " is now in gate keeper mode" << endl; 39 | } 40 | -------------------------------------------------------------------------------- /CPP04/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Animal Dog Cat WrongAnimal WrongCat 8 | 9 | NAME = ex00 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | echo "" 37 | ./$(NAME) 38 | 39 | leaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP04/ex00/includes/Animal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ANIMAL_HPP 2 | # define ANIMAL_HPP 3 | 4 | # include 5 | 6 | class Animal { 7 | public: 8 | Animal(void); 9 | Animal(const Animal &to_copy); 10 | Animal &operator=(const Animal &to_copy); 11 | virtual ~Animal(void); 12 | 13 | std::string getType(void) const; 14 | void setType(std::string type); 15 | 16 | virtual void makeSound(void) const; 17 | 18 | protected: 19 | std::string type; 20 | }; 21 | 22 | #endif // ANIMAL_HPP -------------------------------------------------------------------------------- /CPP04/ex00/includes/Cat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAT_HPP 2 | # define CAT_HPP 3 | 4 | # include "Animal.hpp" 5 | 6 | class Cat : public Animal { 7 | public: 8 | Cat(void); 9 | Cat(const Cat &original); 10 | Cat &operator=(const Cat &to_copy); 11 | ~Cat(void); 12 | 13 | void makeSound(void) const; 14 | }; 15 | 16 | #endif // CAT_HPP -------------------------------------------------------------------------------- /CPP04/ex00/includes/Dog.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DOG_HPP 2 | # define DOG_HPP 3 | 4 | # include "Animal.hpp" 5 | 6 | class Dog : public Animal { 7 | public: 8 | Dog(void); 9 | Dog(const Dog &original); 10 | Dog &operator=(const Dog &to_copy); 11 | ~Dog(void); 12 | 13 | void makeSound(void) const; 14 | }; 15 | 16 | #endif // DOG_HPP -------------------------------------------------------------------------------- /CPP04/ex00/includes/WrongAnimal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WRONGANIMAL_HPP 2 | # define WRONGANIMAL_HPP 3 | 4 | # include 5 | 6 | class WrongAnimal { 7 | public: 8 | WrongAnimal(void); 9 | WrongAnimal(const WrongAnimal &original); 10 | WrongAnimal &operator=(const WrongAnimal &to_copy); 11 | virtual ~WrongAnimal(void); 12 | 13 | std::string getType(void) const; 14 | void setType(std::string type); 15 | 16 | void makeSound(void) const; 17 | 18 | protected: 19 | std::string type; 20 | }; 21 | 22 | #endif // WRONGANIMAL_HPP -------------------------------------------------------------------------------- /CPP04/ex00/includes/WrongCat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WRONGCAT_HPP 2 | # define WRONGCAT_HPP 3 | 4 | # include "WrongAnimal.hpp" 5 | 6 | class WrongCat : public WrongAnimal { 7 | public: 8 | WrongCat(void); 9 | WrongCat(const WrongCat &original); 10 | WrongCat &operator=(const WrongCat &to_copy); 11 | ~WrongCat(void); 12 | 13 | void makeSound(void) const; 14 | }; 15 | 16 | #endif // WRONGCAT_HPP -------------------------------------------------------------------------------- /CPP04/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Animal.hpp" 4 | #include "Dog.hpp" 5 | #include "Cat.hpp" 6 | #include "WrongAnimal.hpp" 7 | #include "WrongCat.hpp" 8 | 9 | using std::cout; 10 | using std::endl; 11 | 12 | int main(void) 13 | { 14 | const Animal* dog = new Dog(); 15 | const Animal* cat = new Cat(); 16 | const Animal* meta_animal = new Animal(); 17 | 18 | cout << dog->getType() << "-> "; 19 | dog->makeSound(); 20 | 21 | cout << cat->getType() << "-> "; 22 | cat->makeSound(); 23 | 24 | cout << endl; 25 | 26 | meta_animal->makeSound(); 27 | 28 | cout << endl; 29 | 30 | cout << "---------- WrongAnimal & WrongCat ----------" << endl; 31 | 32 | const WrongAnimal* wrong_cat = new WrongCat(); 33 | const WrongAnimal* meta_wrong_animal = new WrongAnimal(); 34 | 35 | cout << endl; 36 | 37 | cout << wrong_cat->getType() << "-> "; 38 | wrong_cat->makeSound(); 39 | 40 | meta_wrong_animal->makeSound(); 41 | 42 | cout << endl; 43 | 44 | delete dog; 45 | delete cat; 46 | delete meta_animal; 47 | delete wrong_cat; 48 | delete meta_wrong_animal; 49 | 50 | return EXIT_SUCCESS; 51 | } 52 | -------------------------------------------------------------------------------- /CPP04/ex00/srcs/Animal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Animal.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Animal::Animal(void) { 8 | cout << "[Animal] default constructor has been called" << endl; 9 | }; 10 | 11 | Animal::Animal(const Animal &to_copy) 12 | { 13 | *this = to_copy; 14 | cout << "[Animal] copy constructor has been called" << endl; 15 | } 16 | 17 | Animal& Animal::operator=(const Animal &to_copy) 18 | { 19 | this->type = to_copy.type; 20 | cout << "[Animal] copy assignment constructor has been called" << endl; 21 | return *this; 22 | } 23 | 24 | Animal::~Animal(void) { 25 | cout << "[Animal] default destructor has been called" << endl; 26 | } 27 | 28 | std::string Animal::getType(void) const { 29 | return this->type; 30 | } 31 | 32 | void Animal::setType(std::string type) { 33 | this->type = type; 34 | } 35 | 36 | void Animal::makeSound(void) const { 37 | cout << "[Animal] makeSound() has been called" << endl; 38 | } 39 | -------------------------------------------------------------------------------- /CPP04/ex00/srcs/Cat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Cat.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Cat::Cat(void) : Animal() { 8 | cout << "[Cat] default constructor has been called" << endl; 9 | this->setType("Cat"); 10 | } 11 | 12 | Cat::Cat(const Cat &to_copy) : Animal(to_copy) 13 | { 14 | *this = to_copy; 15 | cout << "[Cat] copy constructor has been called " << endl; 16 | } 17 | 18 | Cat& Cat::operator=(const Cat &to_copy) 19 | { 20 | this->type = to_copy.type; 21 | cout << "[Cat] copy assignment constructor has been called " << endl; 22 | return *this; 23 | } 24 | 25 | Cat::~Cat(void) { 26 | cout << "[Cat] default destructor has been called" << endl; 27 | } 28 | 29 | void Cat::makeSound(void) const { 30 | cout << "Miau Miau 🐈" << endl; 31 | } 32 | -------------------------------------------------------------------------------- /CPP04/ex00/srcs/Dog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Dog.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Dog::Dog(void) : Animal() { 8 | cout << "[Dog] default constructor has been called" << endl; 9 | this->setType("Dog"); 10 | } 11 | 12 | Dog::Dog(const Dog &to_copy) : Animal(to_copy) 13 | { 14 | *this = to_copy; 15 | cout << "[Dog] copy constructor has been called " << endl; 16 | } 17 | 18 | Dog& Dog::operator=(const Dog &to_copy) 19 | { 20 | *this = to_copy; 21 | cout << "[Dog] copy assignment constructor has been called " << endl; 22 | return *this; 23 | } 24 | 25 | Dog::~Dog(void) { 26 | cout << "[Dog] default destructor has been called" << endl; 27 | } 28 | 29 | void Dog::makeSound(void) const { 30 | cout << "AuAu 🐕" << endl; 31 | } 32 | -------------------------------------------------------------------------------- /CPP04/ex00/srcs/WrongAnimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WrongAnimal.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | WrongAnimal::WrongAnimal(void) { 8 | cout << "[WrongAnimal] default constructor has been called" << endl; 9 | }; 10 | 11 | WrongAnimal::WrongAnimal(const WrongAnimal &to_copy) 12 | { 13 | *this = to_copy; 14 | cout << "[WrongAnimal] copy constructor has been called" << endl; 15 | } 16 | 17 | WrongAnimal& WrongAnimal::operator=(const WrongAnimal &to_copy) 18 | { 19 | this->type = to_copy.type; 20 | cout << "[WrongAnimal] copy assignment constructor has been called" << endl; 21 | return *this; 22 | } 23 | 24 | WrongAnimal::~WrongAnimal(void) { 25 | cout << "[WrongAnimal] default destructor has been called" << endl; 26 | } 27 | 28 | std::string WrongAnimal::getType(void) const { 29 | return this->type; 30 | } 31 | 32 | void WrongAnimal::setType(std::string type) { 33 | this->type = type; 34 | } 35 | 36 | void WrongAnimal::makeSound(void) const { 37 | cout << "[WrongAnimal] makeSound() has been called" << endl; 38 | } 39 | -------------------------------------------------------------------------------- /CPP04/ex00/srcs/WrongCat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WrongCat.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | WrongCat::WrongCat(void) : WrongAnimal() { 8 | cout << "[WrongCat] default constructor has been called" << endl; 9 | this->setType("WrongCat"); 10 | } 11 | 12 | WrongCat::WrongCat(const WrongCat &to_copy) : WrongAnimal(to_copy) 13 | { 14 | this->type = to_copy.type; 15 | cout << "[WrongCat] copy constructor has been called " << endl; 16 | } 17 | 18 | WrongCat& WrongCat::operator=(const WrongCat &to_copy) 19 | { 20 | if (this != &to_copy) 21 | this->type = to_copy.type; 22 | cout << "[WrongCat] copy assignment constructor has been called " << endl; 23 | return *this; 24 | } 25 | 26 | WrongCat::~WrongCat(void) { 27 | cout << "[WrongCat] default destructor has been called" << endl; 28 | } 29 | 30 | void WrongCat::makeSound(void) const { 31 | cout << "Miau Miau 🐈" << endl; 32 | } -------------------------------------------------------------------------------- /CPP04/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Brain Animal Dog Cat 8 | 9 | NAME = ex01 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: re clean 36 | echo "" 37 | ./$(NAME) 38 | 39 | leaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP04/ex01/includes/Animal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ANIMAL_HPP 2 | # define ANIMAL_HPP 3 | 4 | # include 5 | # include "Brain.hpp" 6 | 7 | 8 | class Animal { 9 | public: 10 | Animal(void); 11 | Animal(const Animal &to_copy); 12 | Animal &operator=(const Animal &to_copy); 13 | virtual ~Animal(void); 14 | 15 | std::string getType(void) const; 16 | void setType(std::string type); 17 | 18 | virtual void makeSound(void) const; 19 | 20 | protected: 21 | std::string type; 22 | }; 23 | 24 | #endif // ANIMAL_HPP -------------------------------------------------------------------------------- /CPP04/ex01/includes/Brain.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BRAIN_HPP 2 | # define BRAIN_HPP 3 | 4 | # include 5 | 6 | class Brain { 7 | public: 8 | Brain(void); 9 | Brain(const Brain &to_copy); 10 | Brain &operator=(const Brain &to_copy); 11 | ~Brain(void); 12 | 13 | private: 14 | std::string ideas[100]; 15 | }; 16 | 17 | #endif // BRAIN_HPP -------------------------------------------------------------------------------- /CPP04/ex01/includes/Cat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAT_HPP 2 | # define CAT_HPP 3 | 4 | # include "Animal.hpp" 5 | 6 | class Cat : public Animal { 7 | public: 8 | Cat(void); 9 | Cat(const Cat &to_copy); 10 | Cat &operator=(const Cat &to_copy); 11 | ~Cat(void); 12 | 13 | void makeSound(void) const; 14 | 15 | private: 16 | Brain* brain; 17 | }; 18 | 19 | #endif // CAT_HPP -------------------------------------------------------------------------------- /CPP04/ex01/includes/Dog.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DOG_HPP 2 | # define DOG_HPP 3 | 4 | # include "Animal.hpp" 5 | 6 | class Dog : public Animal { 7 | public: 8 | Dog(void); 9 | Dog(const Dog &to_copy); 10 | Dog &operator=(const Dog &to_copy); 11 | ~Dog(void); 12 | 13 | void makeSound(void) const; 14 | 15 | private: 16 | Brain* brain; 17 | }; 18 | 19 | #endif // DOG_HPP -------------------------------------------------------------------------------- /CPP04/ex01/includes/WrongAnimal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WRONGANIMAL_HPP 2 | # define WRONGANIMAL_HPP 3 | 4 | # include 5 | 6 | class WrongAnimal { 7 | public: 8 | WrongAnimal(void); 9 | WrongAnimal(const WrongAnimal &to_copy); 10 | WrongAnimal &operator=(const WrongAnimal &to_copy); 11 | ~WrongAnimal(void); 12 | 13 | std::string getType(void) const; 14 | void setType(std::string type); 15 | 16 | void makeSound(void) const; 17 | 18 | protected: 19 | std::string type; 20 | }; 21 | 22 | #endif // WRONGANIMAL_HPP -------------------------------------------------------------------------------- /CPP04/ex01/includes/WrongCat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WRONGCAT_HPP 2 | # define WRONGCAT_HPP 3 | 4 | # include "WrongAnimal.hpp" 5 | 6 | class WrongCat : public WrongAnimal { 7 | public: 8 | WrongCat(void); 9 | WrongCat(const WrongCat &to_copy); 10 | WrongCat &operator=(const WrongCat &to_copy); 11 | ~WrongCat(void); 12 | 13 | void makeSound(void) const; 14 | }; 15 | 16 | #endif // WRONGCAT_HPP -------------------------------------------------------------------------------- /CPP04/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Animal.hpp" 2 | #include "Dog.hpp" 3 | #include "Cat.hpp" 4 | #include 5 | #include 6 | 7 | #define NBR_ANIMALS 4 8 | 9 | using std::cout; 10 | using std::endl; 11 | 12 | int main(void) 13 | { 14 | const Animal* Bisteca = new Dog(); 15 | cout << endl; 16 | 17 | const Animal* Junim = new Cat(); 18 | cout << endl; 19 | 20 | cout << "------------------- ZOO -------------------" << endl; 21 | const Animal* zoo[NBR_ANIMALS]; 22 | 23 | cout << "------------------- DOGS -------------------" << endl; 24 | for (size_t i = 0; i < 2; i += 1) { 25 | zoo[i] = new Dog(); 26 | cout << endl; 27 | } 28 | 29 | cout << "------------------- CATS -------------------" << endl; 30 | for (size_t i = 2; i < 4; i += 1) { 31 | zoo[i] = new Cat(); 32 | cout << endl; 33 | } 34 | 35 | cout << "---------------- DELETE ZOO ---------------" << endl; 36 | for (size_t i = 0; i < NBR_ANIMALS; i += 1) { 37 | delete zoo[i]; 38 | } 39 | cout << "--------------- ZOO DELETED ---------------" << endl; 40 | cout << endl; 41 | 42 | delete Bisteca; 43 | delete Junim; 44 | 45 | return EXIT_SUCCESS; 46 | } 47 | -------------------------------------------------------------------------------- /CPP04/ex01/srcs/Animal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Animal.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Animal::Animal(void) { 8 | cout << "[Animal] default constructor has been called" << endl; 9 | } 10 | 11 | Animal::Animal(const Animal &to_copy) 12 | { 13 | *this = to_copy; 14 | cout << "[Animal] copy constructor has been called" << endl; 15 | } 16 | 17 | Animal& Animal::operator=(const Animal &to_copy) 18 | { 19 | this->type = to_copy.type; 20 | cout << "[Animal] copy assignment constructor has been called" << endl; 21 | return *this; 22 | } 23 | 24 | Animal::~Animal(void) { 25 | cout << "[Animal] default destructor has been called" << endl; 26 | } 27 | 28 | std::string Animal::getType(void) const { 29 | return this->type; 30 | } 31 | 32 | void Animal::setType(std::string type) { 33 | this->type = type; 34 | } 35 | 36 | void Animal::makeSound(void) const { 37 | cout << "[Animal] makeSound() has been called" << endl; 38 | } 39 | -------------------------------------------------------------------------------- /CPP04/ex01/srcs/Brain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Brain.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Brain::Brain(void) { 8 | cout << "[Brain] default constructor has been called" << endl; 9 | }; 10 | 11 | Brain::Brain(const Brain &to_copy) 12 | { 13 | *this = to_copy; 14 | cout << "[Brain] copy constructor has been called" << endl; 15 | } 16 | 17 | Brain& Brain::operator=(const Brain &to_copy) 18 | { 19 | for (size_t i = 0; i < 100; i += 1) { 20 | this->ideas[i] = to_copy.ideas[i]; 21 | } 22 | cout << "[Brain] copy assignment constructor has been called" << endl; 23 | return *this; 24 | } 25 | 26 | Brain::~Brain(void) { 27 | cout << "[Brain] default destructor has been called" << endl; 28 | } 29 | -------------------------------------------------------------------------------- /CPP04/ex01/srcs/Cat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Cat.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Cat::Cat(void) : Animal() { 8 | cout << "[Cat] default constructor has been called" << endl; 9 | this->setType("Cat"); 10 | try { 11 | this->brain = new Brain(); 12 | } 13 | catch (const std::bad_alloc& e) { 14 | cout << "[Cat] Brain memory allocation failed: " << e.what() << endl; 15 | } 16 | } 17 | 18 | Cat::Cat(const Cat &to_copy) : Animal(to_copy) 19 | { 20 | this->type = to_copy.type; 21 | this->brain = new Brain(); 22 | cout << "[Cat] copy constructor has been called " << endl; 23 | } 24 | 25 | Cat& Cat::operator=(const Cat &to_copy) 26 | { 27 | if (this != &to_copy) 28 | this->type = to_copy.type; 29 | cout << "[Cat] copy assignment constructor has been called " << endl; 30 | return *this; 31 | } 32 | 33 | Cat::~Cat(void) 34 | { 35 | cout << "[Cat] default destructor has been called" << endl; 36 | delete this->brain; 37 | } 38 | 39 | void Cat::makeSound(void) const { 40 | cout << "Miau Miau 🐈" << endl; 41 | } 42 | -------------------------------------------------------------------------------- /CPP04/ex01/srcs/Dog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Dog.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Dog::Dog(void) : Animal() { 8 | cout << "[Dog] default constructor has been called" << endl; 9 | this->setType("Dog"); 10 | try { 11 | this->brain = new Brain(); 12 | } 13 | catch (const std::bad_alloc& e) { 14 | cout << "[Dog] Brain memory allocation failed: " << e.what() << endl; 15 | } 16 | } 17 | 18 | Dog::Dog(const Dog &to_copy) : Animal(to_copy) 19 | { 20 | this->type = to_copy.type; 21 | this->brain = new Brain(); 22 | cout << "[Dog] copy constructor has been called " << endl; 23 | } 24 | 25 | Dog& Dog::operator=(const Dog &to_copy) 26 | { 27 | if (this != &to_copy) 28 | *this = to_copy; 29 | cout << "[Dog] copy assignment constructor has been called " << endl; 30 | return *this; 31 | } 32 | 33 | Dog::~Dog(void) { 34 | cout << "[Dog] default destructor has been called" << endl; 35 | delete this->brain; 36 | } 37 | 38 | void Dog::makeSound(void) const { 39 | cout << "AuAu 🐕" << endl; 40 | } 41 | -------------------------------------------------------------------------------- /CPP04/ex01/srcs/WrongAnimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WrongAnimal.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | WrongAnimal::WrongAnimal(void) { 8 | cout << "[WrongAnimal] default constructor has been called" << endl; 9 | }; 10 | 11 | WrongAnimal::WrongAnimal(const WrongAnimal &to_copy) 12 | { 13 | this->type = to_copy.type; 14 | cout << "[WrongAnimal] copy constructor has been called" << endl; 15 | } 16 | 17 | WrongAnimal& WrongAnimal::operator=(const WrongAnimal &to_copy) 18 | { 19 | if (this != &to_copy) 20 | this->type = to_copy.type; 21 | cout << "[WrongAnimal] copy assignment constructor has been called" << endl; 22 | return *this; 23 | } 24 | 25 | WrongAnimal::~WrongAnimal(void) { 26 | cout << "[WrongAnimal] default destructor has been called" << endl; 27 | } 28 | 29 | string WrongAnimal::getType(void) const { 30 | return this->type; 31 | } 32 | 33 | void WrongAnimal::setType(string type) { 34 | this->type = type; 35 | } 36 | 37 | void WrongAnimal::makeSound(void) const { 38 | cout << "[WrongAnimal] makeSound() has been called" << endl; 39 | } 40 | -------------------------------------------------------------------------------- /CPP04/ex01/srcs/WrongCat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WrongCat.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | WrongCat::WrongCat(void) : WrongAnimal() { 8 | cout << "[WrongCat] default constructor has been called" << endl; 9 | this->setType("WrongCat"); 10 | } 11 | 12 | WrongCat::WrongCat(const WrongCat &to_copy) : WrongAnimal(to_copy) 13 | { 14 | this->type = to_copy.type; 15 | cout << "[WrongCat] copy constructor has been called " << endl; 16 | } 17 | 18 | WrongCat& WrongCat::operator=(const WrongCat &to_copy) 19 | { 20 | if (this != &to_copy) 21 | this->type = to_copy.type; 22 | cout << "[WrongCat] copy assignment constructor has been called " << endl; 23 | return *this; 24 | } 25 | 26 | WrongCat::~WrongCat(void) { 27 | cout << "[WrongCat] default destructor has been called" << endl; 28 | } 29 | 30 | void WrongCat::makeSound(void) const { 31 | cout << "Miau Miau 🐈" << endl; 32 | } -------------------------------------------------------------------------------- /CPP04/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = AAnimal Brain Cat Dog 8 | 9 | NAME = ex02 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: re clean 36 | echo "" 37 | ./$(NAME) 38 | 39 | leaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP04/ex02/includes/AAnimal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef AANIMAL_HPP 2 | # define AANIMAL_HPP 3 | 4 | # include 5 | # include "Brain.hpp" 6 | 7 | class AAnimal { 8 | public: 9 | AAnimal(void); 10 | AAnimal(const AAnimal &to_copy); 11 | AAnimal &operator=(const AAnimal &to_copy); 12 | virtual ~AAnimal(void); 13 | 14 | std::string getType(void) const; 15 | void setType(std::string type); 16 | 17 | virtual void makeSound(void) const = 0; 18 | 19 | protected: 20 | std::string type; 21 | }; 22 | 23 | #endif // AANIMAL_HPP -------------------------------------------------------------------------------- /CPP04/ex02/includes/Brain.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BRAIN_HPP 2 | # define BRAIN_HPP 3 | 4 | # include 5 | 6 | class Brain { 7 | public: 8 | Brain(void); 9 | Brain(const Brain &to_copy); 10 | Brain &operator=(const Brain &to_copy); 11 | ~Brain(void); 12 | 13 | private: 14 | std::string ideas[100]; 15 | }; 16 | 17 | #endif // BRAIN_HPP -------------------------------------------------------------------------------- /CPP04/ex02/includes/Cat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAT_HPP 2 | # define CAT_HPP 3 | 4 | # include "AAnimal.hpp" 5 | 6 | class Cat : public AAnimal { 7 | public: 8 | Cat(void); 9 | Cat(const Cat &to_copy); 10 | Cat &operator=(const Cat &to_copy); 11 | ~Cat(void); 12 | 13 | void makeSound(void) const; 14 | 15 | private: 16 | Brain* brain; 17 | }; 18 | 19 | #endif // CAT_HPP -------------------------------------------------------------------------------- /CPP04/ex02/includes/Dog.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DOG_HPP 2 | # define DOG_HPP 3 | 4 | # include "AAnimal.hpp" 5 | 6 | class Dog : public AAnimal { 7 | public: 8 | Dog(void); 9 | Dog(const Dog &to_copy); 10 | Dog &operator=(const Dog &to_copy); 11 | ~Dog(void); 12 | 13 | void makeSound(void) const; 14 | 15 | private: 16 | Brain* brain; 17 | }; 18 | 19 | #endif // DOG_HPP -------------------------------------------------------------------------------- /CPP04/ex02/includes/WrongAnimal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WRONGANIMAL_HPP 2 | # define WRONGANIMAL_HPP 3 | 4 | # include 5 | using std::string; 6 | 7 | class WrongAnimal { 8 | public: 9 | WrongAnimal(void); 10 | WrongAnimal(const WrongAnimal &to_copy); 11 | WrongAnimal &operator=(const WrongAnimal &to_copy); 12 | ~WrongAnimal(void); 13 | 14 | string getType(void) const; 15 | void setType(string type); 16 | 17 | void makeSound(void) const; 18 | 19 | protected: 20 | string type; 21 | }; 22 | 23 | #endif // WRONGANIMAL_HPP -------------------------------------------------------------------------------- /CPP04/ex02/includes/WrongCat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WRONGCAT_HPP 2 | # define WRONGCAT_HPP 3 | 4 | # include "WrongAnimal.hpp" 5 | 6 | class WrongCat : public WrongAnimal { 7 | public: 8 | WrongCat(void); 9 | WrongCat(const WrongCat &to_copy); 10 | WrongCat &operator=(const WrongCat &to_copy); 11 | ~WrongCat(void); 12 | 13 | void makeSound(void) const; 14 | }; 15 | 16 | #endif // WRONGCAT_HPP -------------------------------------------------------------------------------- /CPP04/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include "AAnimal.hpp" 2 | #include "Dog.hpp" 3 | #include "Cat.hpp" 4 | #include 5 | #include 6 | 7 | #define NBR_ANIMALS 4 8 | 9 | using std::cout; 10 | using std::endl; 11 | 12 | int main(void) 13 | { 14 | // AAnimal test = new AAnimal(); 15 | 16 | const AAnimal* Bisteca = new Dog(); 17 | cout << endl; 18 | 19 | const AAnimal* Junim = new Cat(); 20 | cout << endl; 21 | 22 | cout << "------------------- ZOO -------------------" << endl; 23 | const AAnimal* zoo[NBR_ANIMALS]; 24 | 25 | cout << "------------------- DOGS -------------------" << endl; 26 | for (size_t i = 0; i < 2; i += 1) { 27 | zoo[i] = new Dog(); 28 | cout << endl; 29 | } 30 | 31 | cout << "------------------- CATS -------------------" << endl; 32 | for (size_t i = 2; i < 4; i += 1) { 33 | zoo[i] = new Cat(); 34 | cout << endl; 35 | } 36 | 37 | cout << "---------------- DELETE ZOO ---------------" << endl; 38 | for (size_t i = 0; i < NBR_ANIMALS; i += 1) { 39 | delete zoo[i]; 40 | } 41 | cout << "--------------- ZOO DELETED ---------------" << endl; 42 | cout << endl; 43 | 44 | delete Bisteca; 45 | delete Junim; 46 | 47 | return EXIT_SUCCESS; 48 | } 49 | -------------------------------------------------------------------------------- /CPP04/ex02/srcs/AAnimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "AAnimal.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | AAnimal::AAnimal(void) { 8 | cout << "[AAnimal] default constructor has been called" << endl; 9 | } 10 | 11 | AAnimal::AAnimal(const AAnimal &to_copy) 12 | { 13 | this->type = to_copy.type; 14 | cout << "[AAnimal] copy constructor has been called" << endl; 15 | } 16 | 17 | AAnimal& AAnimal::operator=(const AAnimal &to_copy) 18 | { 19 | if (this != &to_copy) 20 | this->type = to_copy.type; 21 | cout << "[AAnimal] copy assignment constructor has been called" << endl; 22 | return *this; 23 | } 24 | 25 | AAnimal::~AAnimal(void) { 26 | cout << "[AAnimal] default destructor has been called" << endl; 27 | } 28 | 29 | std::string AAnimal::getType(void) const { 30 | return this->type; 31 | } 32 | 33 | void AAnimal::setType(std::string type) { 34 | this->type = type; 35 | } 36 | 37 | void AAnimal::makeSound(void) const { 38 | cout << "[AAnimal] makeSound() has been called" << endl; 39 | } 40 | -------------------------------------------------------------------------------- /CPP04/ex02/srcs/Brain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Brain.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Brain::Brain(void) { 8 | cout << "[Brain] default constructor has been called" << endl; 9 | }; 10 | 11 | Brain::Brain(const Brain &to_copy) 12 | { 13 | *this = to_copy; 14 | cout << "[Brain] copy constructor has been called" << endl; 15 | } 16 | 17 | Brain& Brain::operator=(const Brain &to_copy) 18 | { 19 | for (size_t i = 0; i < 100; i += 1) { 20 | this->ideas[i] = to_copy.ideas[i]; 21 | } 22 | cout << "[Brain] copy assignment constructor has been called" << endl; 23 | return *this; 24 | } 25 | 26 | Brain::~Brain(void) { 27 | cout << "[Brain] default destructor has been called" << endl; 28 | } 29 | -------------------------------------------------------------------------------- /CPP04/ex02/srcs/Cat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Cat.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Cat::Cat(void) : AAnimal() { 8 | cout << "[Cat] default constructor has been called" << endl; 9 | this->setType("Cat"); 10 | try { 11 | this->brain = new Brain(); 12 | } 13 | catch (const std::bad_alloc& e) { 14 | cout << "[Cat] Brain memory allocation failed: " << e.what() << endl; 15 | } 16 | } 17 | 18 | Cat::Cat(const Cat &to_copy) : AAnimal(to_copy) 19 | { 20 | this->type = to_copy.type; 21 | this->brain = new Brain(); 22 | cout << "[Cat] copy constructor has been called " << endl; 23 | } 24 | 25 | Cat& Cat::operator=(const Cat &to_copy) 26 | { 27 | if (this != &to_copy) 28 | this->type = to_copy.type; 29 | cout << "[Cat] copy assignment constructor has been called " << endl; 30 | return *this; 31 | } 32 | 33 | Cat::~Cat(void) 34 | { 35 | cout << "[Cat] default destructor has been called" << endl; 36 | delete this->brain; 37 | } 38 | 39 | void Cat::makeSound(void) const { 40 | cout << "Miau Miau 🐈" << endl; 41 | } 42 | -------------------------------------------------------------------------------- /CPP04/ex02/srcs/Dog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Dog.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Dog::Dog(void) : AAnimal() { 8 | cout << "[Dog] default constructor has been called" << endl; 9 | this->setType("Dog"); 10 | this->brain = new Brain(); 11 | } 12 | 13 | Dog::Dog(const Dog &to_copy) : AAnimal(to_copy) 14 | { 15 | this->type = to_copy.type; 16 | this->brain = new Brain(); 17 | cout << "[Dog] copy constructor has been called " << endl; 18 | } 19 | 20 | Dog& Dog::operator=(const Dog &to_copy) 21 | { 22 | if (this != &to_copy) 23 | *this = to_copy; 24 | cout << "[Dog] copy assignment constructor has been called " << endl; 25 | return *this; 26 | } 27 | 28 | Dog::~Dog(void) { 29 | cout << "[Dog] default destructor has been called" << endl; 30 | delete this->brain; 31 | } 32 | 33 | void Dog::makeSound(void) const { 34 | cout << "AuAu 🐕" << endl; 35 | } 36 | -------------------------------------------------------------------------------- /CPP04/ex02/srcs/WrongAnimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WrongAnimal.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | WrongAnimal::WrongAnimal(void) { 8 | cout << "[WrongAnimal] default constructor has been called" << endl; 9 | }; 10 | 11 | WrongAnimal::WrongAnimal(const WrongAnimal &to_copy) 12 | { 13 | *this = to_copy; 14 | cout << "[WrongAnimal] copy constructor has been called" << endl; 15 | } 16 | 17 | WrongAnimal& WrongAnimal::operator=(const WrongAnimal &to_copy) 18 | { 19 | this->type = to_copy.type; 20 | cout << "[WrongAnimal] copy assignment constructor has been called" << endl; 21 | return *this; 22 | } 23 | 24 | WrongAnimal::~WrongAnimal(void) { 25 | cout << "[WrongAnimal] default destructor has been called" << endl; 26 | } 27 | 28 | std::string WrongAnimal::getType(void) const { 29 | return this->type; 30 | } 31 | 32 | void WrongAnimal::setType(std::string type) { 33 | this->type = type; 34 | } 35 | 36 | void WrongAnimal::makeSound(void) const { 37 | cout << "[WrongAnimal] makeSound() has been called" << endl; 38 | } 39 | -------------------------------------------------------------------------------- /CPP04/ex02/srcs/WrongCat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WrongCat.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | WrongCat::WrongCat(void) : WrongAnimal() { 8 | cout << "[WrongCat] default constructor has been called" << endl; 9 | this->setType("WrongCat"); 10 | } 11 | 12 | WrongCat::WrongCat(const WrongCat &to_copy) : WrongAnimal(to_copy) 13 | { 14 | this->type = to_copy.type; 15 | cout << "[WrongCat] copy constructor has been called " << endl; 16 | } 17 | 18 | WrongCat& WrongCat::operator=(const WrongCat &to_copy) 19 | { 20 | if (this != &to_copy) 21 | this->type = to_copy.type; 22 | cout << "[WrongCat] copy assignment constructor has been called " << endl; 23 | return *this; 24 | } 25 | 26 | WrongCat::~WrongCat(void) { 27 | cout << "[WrongCat] default destructor has been called" << endl; 28 | } 29 | 30 | void WrongCat::makeSound(void) const { 31 | cout << "Miau Miau 🐈" << endl; 32 | } -------------------------------------------------------------------------------- /CPP05/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Bureaucrat 8 | 9 | NAME = ex00 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: re clean 36 | echo "" 37 | ./$(NAME) 38 | 39 | leaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP05/ex00/includes/Bureaucrat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BUREAUCRAT_HPP 2 | # define BUREAUCRAT_HPP 3 | 4 | # include 5 | # include 6 | # include 7 | 8 | # define HIGHEST_GRADE 1 9 | # define LOWEST_GRADE 150 10 | 11 | class Bureaucrat { 12 | public: 13 | Bureaucrat(void); 14 | Bureaucrat(const Bureaucrat& to_copy); 15 | Bureaucrat(std::string name, int grade); 16 | Bureaucrat &operator=(const Bureaucrat &original); 17 | ~Bureaucrat(void); 18 | 19 | const std::string& getName(void) const; 20 | int getGrade(void) const; 21 | 22 | void setGrade(int grade); 23 | 24 | void incrementGrade(void); 25 | void decrementGrade(void); 26 | 27 | class GradeTooHighException : public std::exception 28 | { 29 | public: 30 | virtual const char* what() const throw(); 31 | }; 32 | class GradeTooLowException : public std::exception 33 | { 34 | public: 35 | virtual const char* what() const throw(); 36 | }; 37 | 38 | private: 39 | const std::string name; 40 | int grade; 41 | }; 42 | 43 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat); 44 | 45 | #endif // BUREAUCRAT_HPP -------------------------------------------------------------------------------- /CPP05/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Bureaucrat.hpp" 3 | 4 | using std::cout; 5 | using std::cerr; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | cout << "GENERAL" << endl; 11 | { 12 | // Constructor 13 | Bureaucrat a("Ana", 87); 14 | // Copy Constructor 15 | Bureaucrat b(a); 16 | // Copy assignment 17 | Bureaucrat c = b; 18 | 19 | // ostream overload 20 | cout << "a: " << a << endl; 21 | cout << "b: " << b << endl; 22 | cout << "c: " << c << endl; 23 | } 24 | 25 | cout << endl; 26 | 27 | { 28 | cout << "TEST 1" << endl; 29 | try 30 | { 31 | // grade too low 32 | Bureaucrat a("A", LOWEST_GRADE + 1); 33 | cout << a << endl; 34 | } 35 | catch (std::exception& e) { 36 | cerr << e.what() << endl; 37 | } 38 | 39 | cout << endl; 40 | 41 | cout << "TEST 2" << endl; 42 | try 43 | { 44 | // grade too high 45 | Bureaucrat b("B", HIGHEST_GRADE - 1); 46 | cout << b << endl; 47 | } 48 | catch (std::exception& e) { 49 | cerr << e.what() << endl; 50 | } 51 | 52 | cout << endl; 53 | 54 | cout << "TEST 3" << endl; 55 | try 56 | { 57 | // Increment to a grade higher than HIGHEST_GRADE 58 | Bureaucrat c("C", HIGHEST_GRADE); 59 | 60 | c.incrementGrade(); 61 | cout << c << endl; 62 | } 63 | catch (std::exception& e) { 64 | cerr << e.what() << endl; 65 | } 66 | 67 | cout << endl; 68 | 69 | cout << "TEST 4" << endl; 70 | try 71 | { 72 | // Decrement to a grade lower than LOWEST_GRADE 73 | Bureaucrat d("D", LOWEST_GRADE); 74 | 75 | d.decrementGrade(); 76 | cout << d << endl; 77 | } 78 | catch (std::exception& e) { 79 | cerr << e.what() << endl; 80 | } 81 | 82 | cout << endl; 83 | 84 | cout << "TEST 5" << endl; 85 | try 86 | { 87 | // Acceptable grade changes 88 | Bureaucrat ebil("EBIL", 10); 89 | 90 | ebil.decrementGrade(); 91 | cout << ebil << endl; 92 | ebil.incrementGrade(); 93 | cout << ebil << endl; 94 | } 95 | catch (std::exception& e) { 96 | cerr << e.what() << endl; 97 | } 98 | } 99 | return EXIT_SUCCESS; 100 | } -------------------------------------------------------------------------------- /CPP05/ex00/srcs/Bureaucrat.cpp: -------------------------------------------------------------------------------- 1 | #include "Bureaucrat.hpp" 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | Bureaucrat::Bureaucrat(void) { 7 | cout << "[Bureaucrat] default constructor has been called" << endl; 8 | }; 9 | 10 | Bureaucrat::Bureaucrat(const Bureaucrat& to_copy) 11 | { 12 | if (this != &to_copy) 13 | *this = to_copy; 14 | cout << "[Bureaucrat] <" << this->getName() << "> copy constructor called" << endl; 15 | } 16 | 17 | Bureaucrat::Bureaucrat(std::string name, int grade) : name(name) 18 | { 19 | if (grade < HIGHEST_GRADE) 20 | throw Bureaucrat::GradeTooHighException(); 21 | else if (grade > LOWEST_GRADE) 22 | throw Bureaucrat::GradeTooLowException(); 23 | else 24 | this->grade = grade; 25 | cout << "[Bureaucrat] <" << this->getName() << "> constructor called" << endl; 26 | }; 27 | 28 | Bureaucrat &Bureaucrat::operator=(const Bureaucrat &original) 29 | { 30 | const_cast(this->name) = original.name; 31 | this->grade = original.grade; 32 | cout << "[Bureaucrat] <" << this->getName() << "> copy assignment operator called" << endl; 33 | return *this; 34 | } 35 | 36 | Bureaucrat::~Bureaucrat(void) { 37 | cout << "[Bureaucrat] <" << this->getName() << "> default destructor has been called" << endl; 38 | }; 39 | 40 | const std::string& Bureaucrat::getName(void) const { 41 | return this->name; 42 | } 43 | 44 | int Bureaucrat::getGrade(void) const { 45 | return this->grade; 46 | } 47 | 48 | void Bureaucrat::setGrade(int grade) { 49 | if (grade > LOWEST_GRADE) 50 | throw Bureaucrat::GradeTooLowException(); 51 | else if (grade < HIGHEST_GRADE) 52 | throw Bureaucrat::GradeTooHighException(); 53 | else 54 | this->grade = grade; 55 | } 56 | 57 | void Bureaucrat::incrementGrade(void) 58 | { 59 | if (grade - 1 < HIGHEST_GRADE) 60 | throw Bureaucrat::GradeTooHighException(); 61 | else 62 | this->grade -= 1; 63 | } 64 | 65 | void Bureaucrat::decrementGrade(void) 66 | { 67 | if (grade + 1 > LOWEST_GRADE) 68 | throw Bureaucrat::GradeTooLowException(); 69 | else 70 | this->grade += 1; 71 | } 72 | 73 | const char* Bureaucrat::GradeTooHighException::what() const throw() { 74 | return "Bureaucrat exception: grade too high!"; 75 | } 76 | 77 | const char* Bureaucrat::GradeTooLowException::what() const throw() { 78 | return "Bureaucrat exception: grade too low!"; 79 | } 80 | 81 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat) 82 | { 83 | stream << Bureaucrat.getName() << ", bureaucrat grade " << Bureaucrat.getGrade(); 84 | return stream; 85 | } 86 | -------------------------------------------------------------------------------- /CPP05/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Bureaucrat Form 8 | 9 | NAME = ex01 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: re clean 36 | echo "" 37 | ./$(NAME) 38 | 39 | leaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP05/ex01/includes/Bureaucrat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BUREAUCRAT_HPP 2 | # define BUREAUCRAT_HPP 3 | 4 | # include 5 | # include 6 | # include 7 | 8 | # define HIGHEST_GRADE 1 9 | # define LOWEST_GRADE 150 10 | 11 | class Bureaucrat { 12 | public: 13 | Bureaucrat(void); 14 | Bureaucrat(const Bureaucrat& to_copy); 15 | Bureaucrat(std::string name, int grade); 16 | Bureaucrat &operator=(const Bureaucrat &original); 17 | ~Bureaucrat(void); 18 | 19 | const std::string& getName(void) const; 20 | int getGrade(void) const; 21 | 22 | void setGrade(int grade); 23 | 24 | void incrementGrade(void); 25 | void decrementGrade(void); 26 | void signForm(std::string form_name, bool was_signed); 27 | 28 | class GradeTooHighException : public std::exception 29 | { 30 | public: 31 | virtual const char* what() const throw(); 32 | }; 33 | class GradeTooLowException : public std::exception 34 | { 35 | public: 36 | virtual const char* what() const throw(); 37 | }; 38 | 39 | private: 40 | const std::string name; 41 | int grade; 42 | }; 43 | 44 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat); 45 | 46 | #endif // BUREAUCRAT_HPP -------------------------------------------------------------------------------- /CPP05/ex01/includes/Form.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FORM_HPP 2 | # define FORM_HPP 3 | 4 | # include 5 | # include 6 | # include "Bureaucrat.hpp" 7 | 8 | # define HIGHEST_GRADE 1 9 | # define LOWEST_GRADE 150 10 | 11 | class Form { 12 | public: 13 | Form(const Form& to_copy); 14 | Form(std::string name, int grade_to_sign, int grade_to_execute); 15 | Form &operator=(const Form &original); 16 | ~Form(void); 17 | 18 | const std::string& getName(void) const; 19 | bool getSignState(void) const; 20 | int getGradeToSign(void) const; 21 | int getGradeToExecute(void) const; 22 | 23 | void beSigned(Bureaucrat &bureaucrat); 24 | 25 | class GradeTooHighException : public std::exception 26 | { 27 | public: 28 | virtual const char* what() const throw(); 29 | }; 30 | class GradeTooLowException : public std::exception 31 | { 32 | public: 33 | virtual const char* what() const throw(); 34 | }; 35 | 36 | private: 37 | const std::string name; 38 | bool is_signed; 39 | const int grade_to_sign; 40 | const int grade_to_execute; 41 | Form(void); 42 | }; 43 | 44 | std::ostream &operator<<(std::ostream &stream, Form &Form); 45 | 46 | #endif // FORM_HPP -------------------------------------------------------------------------------- /CPP05/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Form.hpp" 3 | 4 | using std::cout; 5 | using std::cerr; 6 | using std::endl; 7 | 8 | int main(void) 9 | { 10 | cout << "GENERAL" << endl; 11 | { 12 | // Constructor 13 | Form a("Contract", 42, 42); 14 | // Copy Constructor 15 | Form b(a); 16 | // Copy assignment 17 | Form c = b; 18 | 19 | // ostream overload 20 | cout << "a:\n" << a << endl; 21 | cout << "b:\n" << b << endl; 22 | cout << "c:\n" << c << endl; 23 | } 24 | 25 | cout << endl; 26 | 27 | { 28 | cout << "TEST 1" << endl; 29 | try 30 | { 31 | // Grade too low 32 | Form a("A", LOWEST_GRADE + 1, LOWEST_GRADE - 1); 33 | cout << a << endl; 34 | } 35 | catch (std::exception& e) { 36 | cerr << e.what() << endl; 37 | } 38 | 39 | cout << endl; 40 | 41 | cout << "TEST 2" << endl; 42 | try 43 | { 44 | // Grade too high 45 | Form b("B", HIGHEST_GRADE - 1, 42); 46 | cout << b << endl; 47 | } 48 | catch (std::exception& e) { 49 | cerr << e.what() << endl; 50 | } 51 | 52 | cout << endl; 53 | 54 | cout << "TEST 3" << endl; 55 | try 56 | { 57 | // Able to sign all forms 58 | Bureaucrat s1("Student1", HIGHEST_GRADE); 59 | cout << s1 << endl; 60 | 61 | Form c1("C1", HIGHEST_GRADE, 2); 62 | Form c2("C2", 90, LOWEST_GRADE); 63 | cout << c1 << endl; 64 | cout << c2 << endl; 65 | 66 | c1.beSigned(s1); 67 | c2.beSigned(s1); 68 | c1.beSigned(s1); 69 | 70 | cout << c1 << endl; 71 | cout << c2 << endl; 72 | } 73 | catch (std::exception& e) { 74 | cerr << e.what() << endl; 75 | } 76 | 77 | cout << endl; 78 | 79 | cout << "TEST 4" << endl; 80 | try 81 | { 82 | // Unable to sign any form 83 | Bureaucrat s2("Student2", LOWEST_GRADE); 84 | cout << s2 << endl; 85 | 86 | Form c3("C3", HIGHEST_GRADE, 2); 87 | cout << c3 << endl; 88 | 89 | // Exception will be thrown 90 | c3.beSigned(s2); 91 | cout << c3 << endl; 92 | } 93 | catch (std::exception& e) { 94 | cerr << e.what() << endl; 95 | } 96 | } 97 | return EXIT_SUCCESS; 98 | } 99 | -------------------------------------------------------------------------------- /CPP05/ex01/srcs/Bureaucrat.cpp: -------------------------------------------------------------------------------- 1 | #include "Bureaucrat.hpp" 2 | 3 | using std::cout; 4 | using std::cerr; 5 | using std::endl; 6 | 7 | Bureaucrat::Bureaucrat(void) { 8 | cout << "[Bureaucrat] default constructor has been called" << endl; 9 | }; 10 | 11 | Bureaucrat::Bureaucrat(const Bureaucrat& to_copy) 12 | { 13 | if (this != &to_copy) 14 | *this = to_copy; 15 | cout << "[Bureaucrat] <" << this->getName() << "> copy constructor called" << endl; 16 | } 17 | 18 | Bureaucrat::Bureaucrat(std::string name, int grade) : name(name) 19 | { 20 | if (grade < HIGHEST_GRADE) 21 | throw(Bureaucrat::GradeTooHighException()); 22 | else if (grade > LOWEST_GRADE) 23 | throw(Bureaucrat::GradeTooLowException()); 24 | else 25 | this->grade = grade; 26 | cout << "[Bureaucrat] <" << this->getName() << "> constructor called" << endl; 27 | }; 28 | 29 | Bureaucrat &Bureaucrat::operator=(const Bureaucrat &original) 30 | { 31 | const_cast(this->name) = original.name; 32 | this->grade = original.grade; 33 | cout << "[Bureaucrat] <" << this->getName() << "> copy assignment operator called" << endl; 34 | return *this; 35 | } 36 | 37 | Bureaucrat::~Bureaucrat(void) { 38 | cout << "[Bureaucrat] <" << this->getName() << "> default destructor has been called" << endl; 39 | }; 40 | 41 | const std::string& Bureaucrat::getName(void) const { 42 | return this->name; 43 | } 44 | 45 | int Bureaucrat::getGrade(void) const { 46 | return this->grade; 47 | } 48 | 49 | void Bureaucrat::setGrade(int grade) { 50 | if (grade > LOWEST_GRADE) 51 | throw(Bureaucrat::GradeTooLowException()); 52 | else if (grade < HIGHEST_GRADE) 53 | throw(Bureaucrat::GradeTooHighException()); 54 | else 55 | this->grade = grade; 56 | } 57 | 58 | void Bureaucrat::incrementGrade(void) 59 | { 60 | if (grade - 1 < HIGHEST_GRADE) 61 | throw(Bureaucrat::GradeTooHighException()); 62 | else 63 | this->grade -= 1; 64 | } 65 | 66 | void Bureaucrat::decrementGrade(void) 67 | { 68 | if (grade + 1 > LOWEST_GRADE) 69 | throw(Bureaucrat::GradeTooLowException()); 70 | else 71 | this->grade += 1; 72 | } 73 | 74 | void Bureaucrat::signForm(std::string form_name, bool is_signed) 75 | { 76 | if (is_signed) 77 | cout << this->getName() << " signed " << form_name << endl; 78 | else 79 | cout << this->getName() << " couldn't sign " << form_name 80 | << " because the grade was too low and was nervoxhinhu 🥺" << endl; 81 | } 82 | 83 | const char* Bureaucrat::GradeTooHighException::what() const throw() { 84 | return "Bureaucrat exception: grade too high!"; 85 | } 86 | 87 | const char* Bureaucrat::GradeTooLowException::what() const throw() { 88 | return "Bureaucrat exception: grade too low!"; 89 | } 90 | 91 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat) 92 | { 93 | stream << Bureaucrat.getName() << ", bureaucrat grade " << Bureaucrat.getGrade(); 94 | return stream; 95 | } 96 | -------------------------------------------------------------------------------- /CPP05/ex01/srcs/Form.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Form.hpp" 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | Form::Form(const Form& to_copy) 8 | : name(to_copy.name), is_signed(to_copy.is_signed), 9 | grade_to_sign(to_copy.grade_to_sign), grade_to_execute(to_copy.grade_to_execute) { 10 | cout << "[Form] <" << this->getName() << "> copy constructor called" << endl; 11 | } 12 | 13 | Form::Form(std::string name, int grade_to_sign, int grade_to_execute) 14 | : name(name), grade_to_sign(grade_to_sign), grade_to_execute(grade_to_execute) 15 | { 16 | if (grade_to_sign > LOWEST_GRADE 17 | || grade_to_execute > LOWEST_GRADE) 18 | throw (Form::GradeTooLowException()); 19 | else if (grade_to_sign < HIGHEST_GRADE 20 | || grade_to_execute < HIGHEST_GRADE) 21 | throw (Form::GradeTooHighException()); 22 | this->is_signed = false; 23 | cout << "[Form] <" << this->getName() << "> constructor has been called" << endl; 24 | } 25 | 26 | Form::~Form(void) { 27 | cout << "[Form] <" << this->getName() << "> default destructor has been called" << endl; 28 | } 29 | 30 | const std::string& Form::getName(void) const { 31 | return this->name; 32 | } 33 | 34 | bool Form::getSignState(void) const { 35 | return this->is_signed; 36 | } 37 | 38 | int Form::getGradeToSign(void) const { 39 | return this->grade_to_sign; 40 | } 41 | 42 | int Form::getGradeToExecute(void) const { 43 | return this->grade_to_execute; 44 | } 45 | 46 | void Form::beSigned(Bureaucrat &bureaucrat) 47 | { 48 | if (bureaucrat.getGrade() <= this->getGradeToSign()) 49 | { 50 | this->is_signed = true; 51 | bureaucrat.signForm(this->getName(), this->is_signed); 52 | } 53 | else 54 | { 55 | bureaucrat.signForm(this->getName(), this->is_signed); 56 | throw (Form::GradeTooLowException()); 57 | } 58 | 59 | } 60 | 61 | const char* Form::GradeTooHighException::what(void) const throw() { 62 | return "Form exception: exception: grade too high!"; 63 | } 64 | 65 | const char* Form::GradeTooLowException::what(void) const throw() { 66 | return "Form exception: exception: grade too low!"; 67 | } 68 | 69 | std::ostream &operator<<(std::ostream &stream, Form &form) 70 | { 71 | stream << "Name: " << form.getName() << "\n" 72 | << "Signed: " << (form.getSignState() == true ? "yes" : "no") << "\n" 73 | << "Grade to sign: " << form.getGradeToSign() << "\n" 74 | << "Grade to execute: " << form.getGradeToExecute() << "\n" 75 | << endl; 76 | return stream; 77 | } 78 | -------------------------------------------------------------------------------- /CPP05/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Bureaucrat AForm ShrubberyCreationForm RobotomyRequestForm PresidentialPardonForm 8 | 9 | NAME = ex02 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | $(RM) *_shrubbery 33 | 34 | re: fclean all 35 | 36 | run: re clean 37 | echo "" 38 | ./$(NAME) 39 | 40 | leaks: re clean 41 | echo "" 42 | valgrind --leak-check=full -s ./$(NAME) 43 | 44 | .PHONY: all clean fclean re run 45 | 46 | #.SILENT: -------------------------------------------------------------------------------- /CPP05/ex02/includes/AForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef AFORM_HPP 2 | # define AFORM_HPP 3 | 4 | # include 5 | # include 6 | # include "Bureaucrat.hpp" 7 | 8 | # define HIGHEST_GRADE 1 9 | # define LOWEST_GRADE 150 10 | 11 | class Bureaucrat; 12 | 13 | class AForm { 14 | public: 15 | AForm(std::string name, std::string target, int grade_to_sign, int grade_to_execute); 16 | AForm(AForm& to_copy); 17 | AForm &operator=(AForm& original); 18 | virtual ~AForm(void); 19 | 20 | const std::string& getName(void) const; 21 | const std::string& getTarget(void) const; 22 | bool getSignState(void) const; 23 | int getGradeToSign(void) const; 24 | int getGradeToExecute(void) const; 25 | 26 | void beSigned(Bureaucrat &bureaucrat); 27 | void execute(Bureaucrat const& executor) const; 28 | 29 | class GradeTooHighException : public std::exception 30 | { 31 | public: 32 | virtual const char* what() const throw(); 33 | }; 34 | class GradeTooLowException : public std::exception 35 | { 36 | public: 37 | virtual const char* what() const throw(); 38 | }; 39 | 40 | private: 41 | const std::string name; 42 | const std::string target; 43 | bool is_signed; 44 | const int grade_to_sign; 45 | const int grade_to_execute; 46 | AForm(void); 47 | virtual void executeSuperClassForm(Bureaucrat const& executor) const = 0; 48 | }; 49 | 50 | std::ostream &operator<<(std::ostream &stream, AForm &forM); 51 | 52 | #endif // AFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex02/includes/Bureaucrat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BUREAUCRAT_HPP 2 | # define BUREAUCRAT_HPP 3 | 4 | # include 5 | # include 6 | # include 7 | # include "AForm.hpp" 8 | 9 | class AForm; 10 | 11 | class Bureaucrat { 12 | public: 13 | Bureaucrat(void); 14 | Bureaucrat(const Bureaucrat& to_copy); 15 | Bureaucrat(std::string name, int grade); 16 | Bureaucrat &operator=(const Bureaucrat &original); 17 | ~Bureaucrat(void); 18 | 19 | const std::string& getName(void) const; 20 | int getGrade(void) const; 21 | 22 | void setGrade(int grade); 23 | 24 | void incrementGrade(void); 25 | void decrementGrade(void); 26 | void signForm(std::string form_name, bool was_signed); 27 | 28 | void executeForm(AForm const& form); 29 | 30 | class GradeTooHighException : public std::exception 31 | { 32 | public: 33 | virtual const char* what() const throw(); 34 | }; 35 | class GradeTooLowException : public std::exception 36 | { 37 | public: 38 | virtual const char* what() const throw(); 39 | }; 40 | 41 | private: 42 | const std::string name; 43 | int grade; 44 | }; 45 | 46 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat); 47 | 48 | #endif // BUREAUCRAT_HPP -------------------------------------------------------------------------------- /CPP05/ex02/includes/PresidentialPardonForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PRESIDENTIALPARDONFORM_HPP 2 | # define PRESIDENTIALPARDONFORM_HPP 3 | 4 | # include "AForm.hpp" 5 | 6 | class PresidentialPardonForm : public AForm 7 | { 8 | public: 9 | PresidentialPardonForm(std::string target); 10 | PresidentialPardonForm(const PresidentialPardonForm ©); 11 | ~PresidentialPardonForm(void); 12 | 13 | PresidentialPardonForm& operator=(const PresidentialPardonForm &assign); 14 | 15 | void executeSuperClassForm(Bureaucrat const& executor) const; 16 | 17 | private: 18 | PresidentialPardonForm(void); 19 | }; 20 | 21 | #endif // PRESIDENTIALPARDONFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex02/includes/RobotomyRequestForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ROBOTOMYREQUESTFORM_HPP 2 | # define ROBOTOMYREQUESTFORM_HPP 3 | 4 | # include "AForm.hpp" 5 | 6 | class RobotomyRequestForm : public AForm { 7 | public: 8 | RobotomyRequestForm(std::string target); 9 | RobotomyRequestForm(const RobotomyRequestForm &to_copy); 10 | ~RobotomyRequestForm(void); 11 | 12 | RobotomyRequestForm& operator=(const RobotomyRequestForm &assign); 13 | 14 | void executeSuperClassForm(Bureaucrat const& executor) const; 15 | 16 | private: 17 | RobotomyRequestForm(void); 18 | }; 19 | 20 | #endif // ROBOTOMYREQUESTFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex02/includes/ShrubberyCreationForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SHRUBBERYCREATIONFORM_HPP 2 | # define SHRUBBERYCREATIONFORM_HPP 3 | 4 | # include "AForm.hpp" 5 | 6 | class ShrubberyCreationForm : public AForm { 7 | public: 8 | ShrubberyCreationForm(std::string target); 9 | ShrubberyCreationForm(const ShrubberyCreationForm& to_copy); 10 | ShrubberyCreationForm &operator=(const ShrubberyCreationForm &original); 11 | ~ShrubberyCreationForm(void); 12 | 13 | void executeSuperClassForm(Bureaucrat const& executor) const; 14 | 15 | private: 16 | ShrubberyCreationForm(void); 17 | }; 18 | 19 | #endif // SHRUBBERYCREATIONFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Bureaucrat.hpp" 5 | #include "AForm.hpp" 6 | #include "PresidentialPardonForm.hpp" 7 | #include "RobotomyRequestForm.hpp" 8 | #include "ShrubberyCreationForm.hpp" 9 | 10 | using std::cout; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | int main (int argc, char **argv) 15 | { 16 | (void)argc; 17 | (void)argv; 18 | 19 | Bureaucrat hermano("Hermano", LOWEST_GRADE); 20 | 21 | Bureaucrat ebil("Ebil", HIGHEST_GRADE); 22 | cout << endl; 23 | 24 | cout << endl; 25 | ShrubberyCreationForm scf("Ebil"); 26 | PresidentialPardonForm ppf("Ebil"); 27 | RobotomyRequestForm rrf("Ebil"); 28 | cout << endl; 29 | 30 | ebil.executeForm(scf); 31 | scf.beSigned(ebil); 32 | ebil.executeForm(scf); 33 | 34 | cout << endl; 35 | 36 | ebil.executeForm(ppf); 37 | ppf.beSigned(ebil); 38 | ebil.executeForm(ppf); 39 | 40 | cout << endl; 41 | 42 | ebil.executeForm(rrf); 43 | rrf.beSigned(ebil); 44 | ebil.executeForm(rrf); 45 | 46 | cout << endl; 47 | 48 | try { 49 | hermano.executeForm(scf); 50 | } 51 | catch (std::exception& e) { 52 | cerr << e.what() << endl; 53 | } 54 | 55 | cout << endl; 56 | return EXIT_SUCCESS; 57 | } -------------------------------------------------------------------------------- /CPP05/ex02/srcs/AForm.cpp: -------------------------------------------------------------------------------- 1 | #include "AForm.hpp" 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | AForm::AForm(std::string name, std::string target, int grade_to_sign, int grade_to_execute) 7 | : name(name), target(target), is_signed(false), grade_to_sign(grade_to_sign), grade_to_execute(grade_to_execute) 8 | { 9 | #ifdef LOGS 10 | cout << "[AForm] Parameterized constructor called" << endl; 11 | #endif 12 | if (grade_to_sign > 150 || grade_to_execute > 150) 13 | throw AForm::GradeTooLowException(); 14 | else if (grade_to_sign < 1 || grade_to_execute < 1) 15 | throw AForm::GradeTooHighException(); 16 | } 17 | 18 | AForm::~AForm() { 19 | #ifdef LOGS 20 | cout << "[AForm] Destructor called" << endl; 21 | #endif 22 | } 23 | 24 | AForm::AForm(AForm& to_copy) 25 | : name(to_copy.getName()), target(to_copy.getTarget()), is_signed(to_copy.getSignState()), grade_to_sign(to_copy.getGradeToSign()), grade_to_execute(to_copy.getGradeToExecute()) { 26 | #ifdef LOGS 27 | cout << "[AForm] Copy Constructor called" << endl; 28 | #endif 29 | } 30 | 31 | AForm& AForm::operator=(AForm& original) { 32 | #ifdef LOGS 33 | cout << "[AForm] Copy Assignment Operator called" << endl; 34 | #endif 35 | this->is_signed = original.getSignState(); 36 | return *this; 37 | } 38 | 39 | const std::string& AForm::getName(void) const { 40 | return this->name; 41 | } 42 | 43 | const std::string& AForm::getTarget(void) const { 44 | return this->target; 45 | } 46 | 47 | int AForm::getGradeToSign(void) const { 48 | return this->grade_to_sign; 49 | } 50 | 51 | int AForm::getGradeToExecute(void) const { 52 | return this->grade_to_execute; 53 | } 54 | 55 | bool AForm::getSignState(void) const { 56 | return this->is_signed; 57 | } 58 | 59 | void AForm::beSigned(Bureaucrat &bureaucrat) 60 | { 61 | if (bureaucrat.getGrade() <= this->getGradeToSign()) 62 | { 63 | this->is_signed = true; 64 | bureaucrat.signForm(this->getName(), this->is_signed); 65 | } 66 | else 67 | { 68 | bureaucrat.signForm(this->getName(), this->is_signed); 69 | throw AForm::GradeTooLowException(); 70 | } 71 | } 72 | 73 | void AForm::execute(Bureaucrat const& executor) const { 74 | this->executeSuperClassForm(executor); 75 | } 76 | 77 | const char* AForm::GradeTooHighException::what() const throw() { 78 | return "AForm exception: grade too high!"; 79 | } 80 | 81 | const char* AForm::GradeTooLowException::what() const throw() { 82 | return "AForm exception: grade too low!"; 83 | } 84 | 85 | std::ostream &operator<<(std::ostream& stream, AForm& form) 86 | { 87 | stream << "AForm: " << form.getName() 88 | << "Grade to sign: " << form.getGradeToSign() 89 | << "Grade to execute: " << form.getGradeToExecute() 90 | << "Signed state:" << form.getSignState(); 91 | return stream; 92 | } 93 | -------------------------------------------------------------------------------- /CPP05/ex02/srcs/Bureaucrat.cpp: -------------------------------------------------------------------------------- 1 | #include "Bureaucrat.hpp" 2 | 3 | using std::cout; 4 | using std::cerr; 5 | using std::endl; 6 | 7 | Bureaucrat::Bureaucrat(void) { 8 | #ifdef LOGS 9 | cout << "[Bureaucrat] default constructor has been called" << endl; 10 | #endif 11 | }; 12 | 13 | Bureaucrat::Bureaucrat(const Bureaucrat& to_copy) 14 | { 15 | if (this != &to_copy) 16 | *this = to_copy; 17 | #ifdef LOGS 18 | cout << "[Bureaucrat] <" << this->getName() << "> copy constructor called" << endl; 19 | #endif 20 | } 21 | 22 | Bureaucrat::Bureaucrat(std::string name, int grade) : name(name) 23 | { 24 | #ifdef LOGS 25 | cout << "[Bureaucrat] <" << this->getName() << "> constructor called" << endl; 26 | #endif 27 | if (grade < HIGHEST_GRADE) 28 | throw Bureaucrat::GradeTooHighException(); 29 | else if (grade > LOWEST_GRADE) 30 | throw Bureaucrat::GradeTooLowException(); 31 | else 32 | this->grade = grade; 33 | }; 34 | 35 | Bureaucrat& Bureaucrat::operator=(const Bureaucrat &original) 36 | { 37 | const_cast(this->name) = original.name; 38 | this->grade = original.grade; 39 | #ifdef LOGS 40 | cout << "[Bureaucrat] <" << this->getName() << "> copy assignment operator called" << endl; 41 | #endif 42 | return *this; 43 | } 44 | 45 | Bureaucrat::~Bureaucrat(void) { 46 | #ifdef LOGS 47 | cout << "[Bureaucrat] <" << this->getName() << "> default destructor has been called" << endl; 48 | #endif 49 | }; 50 | 51 | const std::string& Bureaucrat::getName(void) const { 52 | return this->name; 53 | } 54 | 55 | int Bureaucrat::getGrade(void) const { 56 | return this->grade; 57 | } 58 | 59 | void Bureaucrat::setGrade(int grade) { 60 | if (grade > LOWEST_GRADE) 61 | throw(Bureaucrat::GradeTooLowException()); 62 | else if (grade < HIGHEST_GRADE) 63 | throw(Bureaucrat::GradeTooHighException()); 64 | else 65 | this->grade = grade; 66 | } 67 | 68 | void Bureaucrat::incrementGrade(void) 69 | { 70 | if (grade - 1 < HIGHEST_GRADE) 71 | throw Bureaucrat::GradeTooHighException(); 72 | else 73 | this->grade -= 1; 74 | } 75 | 76 | void Bureaucrat::decrementGrade(void) 77 | { 78 | if (grade + 1 > LOWEST_GRADE) 79 | throw Bureaucrat::GradeTooLowException(); 80 | else 81 | this->grade += 1; 82 | } 83 | 84 | void Bureaucrat::signForm(std::string form_name, bool is_signed) 85 | { 86 | if (is_signed) 87 | cout << this->getName() << " signed " << form_name << endl; 88 | else 89 | cout << this->getName() << " couldn't sign " << form_name 90 | << " because the grade was too low 🥺" << endl; 91 | } 92 | 93 | void Bureaucrat::executeForm(AForm const& form) { 94 | form.execute(*this); 95 | } 96 | 97 | const char* Bureaucrat::GradeTooHighException::what() const throw() { 98 | return "Bureaucrat exception: grade too high!"; 99 | } 100 | 101 | const char* Bureaucrat::GradeTooLowException::what() const throw() { 102 | return "Bureaucrat exception: grade too low!"; 103 | } 104 | 105 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat) 106 | { 107 | stream << Bureaucrat.getName() 108 | << "Grade: " << Bureaucrat.getGrade(); 109 | return stream; 110 | } 111 | -------------------------------------------------------------------------------- /CPP05/ex02/srcs/PresidentialPardonForm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "PresidentialPardonForm.hpp" 3 | 4 | using std::cout; 5 | using std::cerr; 6 | using std::endl; 7 | 8 | PresidentialPardonForm::PresidentialPardonForm(std::string target) 9 | : AForm("PresidentialPardonForm", target, 25, 5) { 10 | #ifdef LOGS 11 | cout << "[PresidentialPardonForm] Parameterized Constructor called" << endl; 12 | #endif 13 | }; 14 | 15 | PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm ©) 16 | : AForm(copy.getName(), copy.getTarget(), copy.getGradeToSign(), copy.getGradeToExecute()) { 17 | #ifdef LOGS 18 | cout << "[PresidentialPardonForm] Copy Constructor called" << endl; 19 | #endif 20 | *this = copy; 21 | } 22 | 23 | PresidentialPardonForm::~PresidentialPardonForm(void) { 24 | #ifdef LOGS 25 | cout << "[PresidentialPardonForm] Destructor called" << endl; 26 | #endif 27 | } 28 | 29 | PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm &assign) { 30 | #ifdef LOGS 31 | cout << "[PresidentialPardonForm] Copy Assignment Operator called" << endl; 32 | #endif 33 | if (this == &assign) 34 | return *this; 35 | return *this; 36 | } 37 | 38 | void PresidentialPardonForm::executeSuperClassForm(Bureaucrat const& executor) const 39 | { 40 | if (executor.getGrade() > this->getGradeToExecute()) 41 | throw Bureaucrat::GradeTooLowException(); 42 | else if (this->getSignState() == false) 43 | cerr << "PresidentialPardonForm couldn't be executed by " << executor.getName() << " because it wasn't signed!" << endl; 44 | else 45 | cout << this->getTarget() << " has been pardoned by Zaphod Beeblebrox" << endl; 46 | } 47 | -------------------------------------------------------------------------------- /CPP05/ex02/srcs/RobotomyRequestForm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "RobotomyRequestForm.hpp" 5 | 6 | using std::cout; 7 | using std::cerr; 8 | using std::endl; 9 | 10 | RobotomyRequestForm::RobotomyRequestForm(std::string target) 11 | : AForm("RobotomyRequestForm", target, 72, 45) { 12 | #ifdef LOGS 13 | cout << "[RobotomyRequestForm] Parameterized Constructor" << endl; 14 | #endif 15 | }; 16 | 17 | RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &to_copy) 18 | : AForm(to_copy.getName(), to_copy.getTarget(), to_copy.getGradeToSign(), to_copy.getGradeToExecute()) { 19 | #ifdef LOGS 20 | cout << "[RobotomyRequestForm] Copy Constructor" << endl; 21 | #endif 22 | } 23 | 24 | RobotomyRequestForm::~RobotomyRequestForm() { 25 | #ifdef LOGS 26 | cout << "[RobotomyRequestForm] Destructor called" << endl; 27 | #endif 28 | } 29 | 30 | RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm &assign) { 31 | #ifdef LOGS 32 | cout << "[RobotomyRequestForm] copy assignment operator called"<< endl; 33 | #endif 34 | if (this == &assign) 35 | return *this; 36 | return *this; 37 | } 38 | 39 | void RobotomyRequestForm::executeSuperClassForm(Bureaucrat const& executor) const 40 | { 41 | if (executor.getGrade() > this->getGradeToExecute()) 42 | throw Bureaucrat::GradeTooLowException(); 43 | else if (this->getSignState() == false) 44 | cerr << "RobotomyRequestForm couldn't be executed by " << executor.getName() << " because it wasn't signed!" << endl; 45 | else 46 | { 47 | std::srand(std::time(NULL)); 48 | 49 | cout << "(Loud drill noises) " << endl; 50 | 51 | if (random() % 2 == 0) 52 | cout << this->getTarget() << " successfully robotomized." << endl; 53 | else 54 | cout << this->getTarget() << " failed when trying to robotimize." << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /CPP05/ex03/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -O3 -Wshadow -Wno-shadow -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Bureaucrat AForm ShrubberyCreationForm RobotomyRequestForm PresidentialPardonForm Intern 8 | 9 | NAME = ex03 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: re clean 36 | echo "" 37 | ./$(NAME) 38 | 39 | leaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP05/ex03/includes/AForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef AFORM_HPP 2 | # define AFORM_HPP 3 | 4 | # include 5 | # include 6 | # include "Bureaucrat.hpp" 7 | 8 | # define HIGHEST_GRADE 1 9 | # define LOWEST_GRADE 150 10 | 11 | class Bureaucrat; 12 | 13 | class AForm { 14 | public: 15 | AForm(std::string name, std::string target, int grade_to_sign, int grade_to_execute); 16 | AForm(AForm& to_copy); 17 | AForm &operator=(AForm& original); 18 | virtual ~AForm(void); 19 | 20 | const std::string& getName(void) const; 21 | const std::string& getTarget(void) const; 22 | bool getSignState(void) const; 23 | int getGradeToSign(void) const; 24 | int getGradeToExecute(void) const; 25 | 26 | void beSigned(Bureaucrat &bureaucrat); 27 | void execute(Bureaucrat const& executor) const; 28 | 29 | class GradeTooHighException : public std::exception 30 | { 31 | public: 32 | virtual const char* what() const throw(); 33 | }; 34 | class GradeTooLowException : public std::exception 35 | { 36 | public: 37 | virtual const char* what() const throw(); 38 | }; 39 | 40 | private: 41 | const std::string name; 42 | const std::string target; 43 | bool is_signed; 44 | const int grade_to_sign; 45 | const int grade_to_execute; 46 | AForm(void); 47 | virtual void executeSuperClassForm(Bureaucrat const& executor) const = 0; 48 | }; 49 | 50 | std::ostream &operator<<(std::ostream &stream, AForm &forM); 51 | 52 | #endif // AFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex03/includes/Bureaucrat.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BUREAUCRAT_HPP 2 | # define BUREAUCRAT_HPP 3 | 4 | # include 5 | # include 6 | # include 7 | # include "AForm.hpp" 8 | 9 | class AForm; 10 | 11 | class Bureaucrat { 12 | public: 13 | Bureaucrat(void); 14 | Bureaucrat(const Bureaucrat& to_copy); 15 | Bureaucrat(std::string name, int grade); 16 | Bureaucrat &operator=(const Bureaucrat &original); 17 | ~Bureaucrat(void); 18 | 19 | const std::string& getName(void) const; 20 | int getGrade(void) const; 21 | 22 | void setGrade(int grade); 23 | 24 | void incrementGrade(void); 25 | void decrementGrade(void); 26 | void signForm(std::string form_name, bool was_signed); 27 | 28 | void executeForm(AForm const& form); 29 | 30 | class GradeTooHighException : public std::exception 31 | { 32 | public: 33 | virtual const char* what() const throw(); 34 | }; 35 | class GradeTooLowException : public std::exception 36 | { 37 | public: 38 | virtual const char* what() const throw(); 39 | }; 40 | 41 | private: 42 | const std::string name; 43 | int grade; 44 | }; 45 | 46 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat); 47 | 48 | #endif // BUREAUCRAT_HPP -------------------------------------------------------------------------------- /CPP05/ex03/includes/Intern.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INTERN_HPP 2 | # define INTERN_HPP 3 | 4 | # include 5 | # include "AForm.hpp" 6 | 7 | class Intern { 8 | public: 9 | Intern(void); 10 | Intern(const Intern &to_copy); 11 | ~Intern(void); 12 | 13 | Intern& operator=(const Intern &assign); 14 | 15 | AForm *makeForm(std::string name, std::string target); 16 | }; 17 | 18 | #endif // INTERN_HPP -------------------------------------------------------------------------------- /CPP05/ex03/includes/PresidentialPardonForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PRESIDENTIALPARDONFORM_HPP 2 | # define PRESIDENTIALPARDONFORM_HPP 3 | 4 | # include "AForm.hpp" 5 | 6 | class PresidentialPardonForm : public AForm 7 | { 8 | public: 9 | PresidentialPardonForm(std::string target); 10 | PresidentialPardonForm(const PresidentialPardonForm ©); 11 | ~PresidentialPardonForm(void); 12 | 13 | PresidentialPardonForm& operator=(const PresidentialPardonForm &assign); 14 | 15 | void executeSuperClassForm(Bureaucrat const& executor) const; 16 | 17 | private: 18 | PresidentialPardonForm(void); 19 | }; 20 | 21 | #endif // PRESIDENTIALPARDONFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex03/includes/RobotomyRequestForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ROBOTOMYREQUESTFORM_HPP 2 | # define ROBOTOMYREQUESTFORM_HPP 3 | 4 | # include "AForm.hpp" 5 | 6 | class RobotomyRequestForm : public AForm { 7 | public: 8 | RobotomyRequestForm(std::string target); 9 | RobotomyRequestForm(const RobotomyRequestForm &to_copy); 10 | ~RobotomyRequestForm(void); 11 | 12 | RobotomyRequestForm& operator=(const RobotomyRequestForm &assign); 13 | 14 | void executeSuperClassForm(Bureaucrat const& executor) const; 15 | 16 | private: 17 | RobotomyRequestForm(void); 18 | }; 19 | 20 | #endif // ROBOTOMYREQUESTFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex03/includes/ShrubberyCreationForm.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SHRUBBERYCREATIONFORM_HPP 2 | # define SHRUBBERYCREATIONFORM_HPP 3 | 4 | # include "AForm.hpp" 5 | 6 | class ShrubberyCreationForm : public AForm { 7 | public: 8 | ShrubberyCreationForm(std::string target); 9 | ShrubberyCreationForm(const ShrubberyCreationForm& to_copy); 10 | ShrubberyCreationForm &operator=(const ShrubberyCreationForm &original); 11 | ~ShrubberyCreationForm(void); 12 | 13 | void executeSuperClassForm(Bureaucrat const& executor) const; 14 | 15 | private: 16 | ShrubberyCreationForm(void); 17 | }; 18 | 19 | #endif // SHRUBBERYCREATIONFORM_HPP -------------------------------------------------------------------------------- /CPP05/ex03/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "AForm.hpp" 5 | #include "PresidentialPardonForm.hpp" 6 | #include "RobotomyRequestForm.hpp" 7 | #include "ShrubberyCreationForm.hpp" 8 | #include "Intern.hpp" 9 | 10 | using std::cout; 11 | using std::endl; 12 | 13 | int main(int argc, char **argv) 14 | { 15 | (void)argc; 16 | (void)argv; 17 | 18 | AForm *a; 19 | AForm *b; 20 | AForm *c; 21 | 22 | cout << endl; 23 | 24 | Intern Arnaldo; 25 | 26 | a = Arnaldo.makeForm("robotomy request", "Ebil"); 27 | cout << endl; 28 | b = Arnaldo.makeForm("presidential pardon", "Ebil"); 29 | cout << endl; 30 | c = Arnaldo.makeForm("shrubbery creation", "Ebil"); 31 | cout << endl; 32 | 33 | Arnaldo.makeForm("foo", "Ebil"); 34 | cout << endl; 35 | 36 | delete a; 37 | delete b; 38 | delete c; 39 | return EXIT_SUCCESS; 40 | } -------------------------------------------------------------------------------- /CPP05/ex03/srcs/AForm.cpp: -------------------------------------------------------------------------------- 1 | #include "AForm.hpp" 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | AForm::AForm(std::string name, std::string target, int grade_to_sign, int grade_to_execute) 7 | : name(name), target(target), is_signed(false), grade_to_sign(grade_to_sign), grade_to_execute(grade_to_execute) 8 | { 9 | #ifdef LOGS 10 | cout << "[AForm] Parameterized constructor called" << endl; 11 | #endif 12 | if (grade_to_sign > 150 || grade_to_execute > 150) 13 | throw AForm::GradeTooLowException(); 14 | else if (grade_to_sign < 1 || grade_to_execute < 1) 15 | throw AForm::GradeTooHighException(); 16 | } 17 | 18 | AForm::~AForm() { 19 | #ifdef LOGS 20 | cout << "[AForm] Destructor called" << endl; 21 | #endif 22 | } 23 | 24 | AForm::AForm(AForm& to_copy) 25 | : name(to_copy.getName()), target(to_copy.getTarget()), is_signed(to_copy.getSignState()), grade_to_sign(to_copy.getGradeToSign()), grade_to_execute(to_copy.getGradeToExecute()) { 26 | #ifdef LOGS 27 | cout << "[AForm] Copy Constructor called" << endl; 28 | #endif 29 | } 30 | 31 | AForm& AForm::operator=(AForm& original) { 32 | #ifdef LOGS 33 | cout << "[AForm] Copy Assignment Operator called" << endl; 34 | #endif 35 | this->is_signed = original.getSignState(); 36 | return *this; 37 | } 38 | 39 | const std::string& AForm::getName(void) const { 40 | return this->name; 41 | } 42 | 43 | const std::string& AForm::getTarget(void) const { 44 | return this->target; 45 | } 46 | 47 | int AForm::getGradeToSign(void) const { 48 | return this->grade_to_sign; 49 | } 50 | 51 | int AForm::getGradeToExecute(void) const { 52 | return this->grade_to_execute; 53 | } 54 | 55 | bool AForm::getSignState(void) const { 56 | return this->is_signed; 57 | } 58 | 59 | void AForm::beSigned(Bureaucrat &bureaucrat) 60 | { 61 | if (bureaucrat.getGrade() <= this->getGradeToSign()) 62 | { 63 | this->is_signed = true; 64 | bureaucrat.signForm(this->getName(), this->is_signed); 65 | } 66 | else 67 | { 68 | bureaucrat.signForm(this->getName(), this->is_signed); 69 | throw AForm::GradeTooLowException(); 70 | } 71 | } 72 | 73 | void AForm::execute(Bureaucrat const& executor) const { 74 | this->executeSuperClassForm(executor); 75 | } 76 | 77 | const char* AForm::GradeTooHighException::what() const throw() { 78 | return "AForm exception: grade too high!"; 79 | } 80 | 81 | const char* AForm::GradeTooLowException::what() const throw() { 82 | return "AForm exception: grade too low!"; 83 | } 84 | 85 | std::ostream &operator<<(std::ostream& stream, AForm& form) 86 | { 87 | stream << "AForm: " << form.getName() 88 | << "Grade to sign: " << form.getGradeToSign() 89 | << "Grade to execute: " << form.getGradeToExecute() 90 | << "Signed state:" << form.getSignState(); 91 | return stream; 92 | } 93 | -------------------------------------------------------------------------------- /CPP05/ex03/srcs/Bureaucrat.cpp: -------------------------------------------------------------------------------- 1 | #include "Bureaucrat.hpp" 2 | 3 | using std::cout; 4 | using std::cerr; 5 | using std::endl; 6 | 7 | Bureaucrat::Bureaucrat(void) { 8 | #ifdef LOGS 9 | cout << "[Bureaucrat] default constructor has been called" << endl; 10 | #endif 11 | }; 12 | 13 | Bureaucrat::Bureaucrat(const Bureaucrat& to_copy) 14 | { 15 | if (this != &to_copy) 16 | *this = to_copy; 17 | #ifdef LOGS 18 | cout << "[Bureaucrat] <" << this->getName() << "> copy constructor called" << endl; 19 | #endif 20 | } 21 | 22 | Bureaucrat::Bureaucrat(std::string name, int grade) : name(name) 23 | { 24 | #ifdef LOGS 25 | cout << "[Bureaucrat] <" << this->getName() << "> constructor called" << endl; 26 | #endif 27 | if (grade < HIGHEST_GRADE) 28 | throw Bureaucrat::GradeTooHighException(); 29 | else if (grade > LOWEST_GRADE) 30 | throw Bureaucrat::GradeTooLowException(); 31 | else 32 | this->grade = grade; 33 | }; 34 | 35 | Bureaucrat& Bureaucrat::operator=(const Bureaucrat &original) 36 | { 37 | const_cast(this->name) = original.name; 38 | this->grade = original.grade; 39 | #ifdef LOGS 40 | cout << "[Bureaucrat] <" << this->getName() << "> copy assignment operator called" << endl; 41 | #endif 42 | return *this; 43 | } 44 | 45 | Bureaucrat::~Bureaucrat(void) { 46 | #ifdef LOGS 47 | cout << "[Bureaucrat] <" << this->getName() << "> default destructor has been called" << endl; 48 | #endif 49 | }; 50 | 51 | const std::string& Bureaucrat::getName(void) const { 52 | return this->name; 53 | } 54 | 55 | int Bureaucrat::getGrade(void) const { 56 | return this->grade; 57 | } 58 | 59 | void Bureaucrat::setGrade(int grade) { 60 | if (grade > LOWEST_GRADE) 61 | throw(Bureaucrat::GradeTooLowException()); 62 | else if (grade < HIGHEST_GRADE) 63 | throw(Bureaucrat::GradeTooHighException()); 64 | else 65 | this->grade = grade; 66 | } 67 | 68 | void Bureaucrat::incrementGrade(void) 69 | { 70 | if (grade - 1 < HIGHEST_GRADE) 71 | throw Bureaucrat::GradeTooHighException(); 72 | else 73 | this->grade -= 1; 74 | } 75 | 76 | void Bureaucrat::decrementGrade(void) 77 | { 78 | if (grade + 1 > LOWEST_GRADE) 79 | throw Bureaucrat::GradeTooLowException(); 80 | else 81 | this->grade += 1; 82 | } 83 | 84 | void Bureaucrat::signForm(std::string form_name, bool is_signed) 85 | { 86 | if (is_signed) 87 | cout << this->getName() << " signed " << form_name << endl; 88 | else 89 | cout << this->getName() << " couldn't sign " << form_name 90 | << " because the grade was too low 🥺" << endl; 91 | } 92 | 93 | void Bureaucrat::executeForm(AForm const& form) { 94 | form.execute(*this); 95 | } 96 | 97 | const char* Bureaucrat::GradeTooHighException::what() const throw() { 98 | return "Bureaucrat exception: grade too high!"; 99 | } 100 | 101 | const char* Bureaucrat::GradeTooLowException::what() const throw() { 102 | return "Bureaucrat exception: grade too low!"; 103 | } 104 | 105 | std::ostream &operator<<(std::ostream &stream, Bureaucrat &Bureaucrat) 106 | { 107 | stream << Bureaucrat.getName() 108 | << "Grade: " << Bureaucrat.getGrade(); 109 | return stream; 110 | } 111 | -------------------------------------------------------------------------------- /CPP05/ex03/srcs/Intern.cpp: -------------------------------------------------------------------------------- 1 | #include "Intern.hpp" 2 | #include "AForm.hpp" 3 | #include "ShrubberyCreationForm.hpp" 4 | #include "PresidentialPardonForm.hpp" 5 | #include "RobotomyRequestForm.hpp" 6 | 7 | using std::cout; 8 | using std::cerr; 9 | using std::endl; 10 | 11 | Intern::Intern(void) { 12 | #ifdef LOGS 13 | cout << "[Intern] Default Constructor called" << endl; 14 | #endif 15 | } 16 | 17 | Intern::Intern(const Intern ©) 18 | { 19 | *this = copy; 20 | #ifdef LOGS 21 | cout << "[Intern] Copy Constructor called" << endl; 22 | #endif 23 | } 24 | 25 | Intern::~Intern() { 26 | #ifdef LOGS 27 | cout << "[Intern] Destructor called" << endl; 28 | #endif 29 | } 30 | 31 | Intern& Intern::operator=(const Intern &assign) 32 | { 33 | #ifdef LOGS 34 | cout << "[Intern] Copy Assignment Operator called" << endl; 35 | #endif 36 | if (this == &assign) 37 | return *this; 38 | return *this; 39 | } 40 | 41 | static AForm *newShrubbery(const std::string target) { 42 | return new ShrubberyCreationForm(target); 43 | } 44 | 45 | static AForm *newRobotomy(const std::string target) { 46 | return new RobotomyRequestForm(target); 47 | } 48 | 49 | static AForm *newPresidential(const std::string target) { 50 | return new PresidentialPardonForm(target); 51 | } 52 | 53 | typedef AForm *(*FormConstructorPtr)(const std::string); 54 | 55 | AForm *Intern::makeForm(std::string name, std::string target) 56 | { 57 | AForm *choosen_form = NULL; 58 | 59 | const std::string form_names[] = { 60 | "robotomy request", 61 | "presidential pardon", 62 | "shrubbery creation" 63 | }; 64 | 65 | FormConstructorPtr form_constructors[3] = {&newShrubbery, &newRobotomy, &newPresidential}; 66 | 67 | for (size_t i = 0; i < 3; i += 1) 68 | { 69 | if (name == form_names[i]) 70 | { 71 | cout << "Intern creates " << name << endl; 72 | choosen_form = form_constructors[i](target);; 73 | break ; 74 | } 75 | } 76 | if (!choosen_form) 77 | cerr << "Intern couldn't create " << name << " form" << endl; 78 | return choosen_form; 79 | } -------------------------------------------------------------------------------- /CPP05/ex03/srcs/PresidentialPardonForm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "PresidentialPardonForm.hpp" 3 | 4 | using std::cout; 5 | using std::cerr; 6 | using std::endl; 7 | 8 | PresidentialPardonForm::PresidentialPardonForm(std::string target) 9 | : AForm("PresidentialPardonForm", target, 25, 5) { 10 | #ifdef LOGS 11 | cout << "[PresidentialPardonForm] Parameterized Constructor called" << endl; 12 | #endif 13 | }; 14 | 15 | PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm ©) 16 | : AForm(copy.getName(), copy.getTarget(), copy.getGradeToSign(), copy.getGradeToExecute()) { 17 | #ifdef LOGS 18 | cout << "[PresidentialPardonForm] Copy Constructor called" << endl; 19 | #endif 20 | *this = copy; 21 | } 22 | 23 | PresidentialPardonForm::~PresidentialPardonForm(void) { 24 | #ifdef LOGS 25 | cout << "[PresidentialPardonForm] Destructor called" << endl; 26 | #endif 27 | } 28 | 29 | PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm &assign) { 30 | #ifdef LOGS 31 | cout << "[PresidentialPardonForm] Copy Assignment Operator called" << endl; 32 | #endif 33 | if (this == &assign) 34 | return *this; 35 | return *this; 36 | } 37 | 38 | void PresidentialPardonForm::executeSuperClassForm(Bureaucrat const& executor) const 39 | { 40 | if (executor.getGrade() > this->getGradeToExecute()) 41 | throw Bureaucrat::GradeTooLowException(); 42 | else if (this->getSignState() == false) 43 | cerr << "PresidentialPardonForm couldn't be executed by " << executor.getName() << " because it wasn't signed!" << endl; 44 | else 45 | cout << this->getTarget() << " has been pardoned by Zaphod Beeblebrox" << endl; 46 | } 47 | -------------------------------------------------------------------------------- /CPP05/ex03/srcs/RobotomyRequestForm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "RobotomyRequestForm.hpp" 5 | 6 | using std::cout; 7 | using std::cerr; 8 | using std::endl; 9 | 10 | RobotomyRequestForm::RobotomyRequestForm(std::string target) 11 | : AForm("RobotomyRequestForm", target, 72, 45) { 12 | #ifdef LOGS 13 | cout << "[RobotomyRequestForm] Parameterized Constructor" << endl; 14 | #endif 15 | }; 16 | 17 | RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &to_copy) 18 | : AForm(to_copy.getName(), to_copy.getTarget(), to_copy.getGradeToSign(), to_copy.getGradeToExecute()) { 19 | #ifdef LOGS 20 | cout << "[RobotomyRequestForm] Copy Constructor" << endl; 21 | #endif 22 | } 23 | 24 | RobotomyRequestForm::~RobotomyRequestForm() { 25 | #ifdef LOGS 26 | cout << "[RobotomyRequestForm] Destructor called" << endl; 27 | #endif 28 | } 29 | 30 | RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm &assign) { 31 | #ifdef LOGS 32 | cout << "[RobotomyRequestForm] copy assignment operator called"<< endl; 33 | #endif 34 | if (this == &assign) 35 | return *this; 36 | return *this; 37 | } 38 | 39 | void RobotomyRequestForm::executeSuperClassForm(Bureaucrat const& executor) const 40 | { 41 | if (executor.getGrade() > this->getGradeToExecute()) 42 | throw Bureaucrat::GradeTooLowException(); 43 | else if (this->getSignState() == false) 44 | cerr << "RobotomyRequestForm couldn't be executed by " << executor.getName() << " because it wasn't signed!" << endl; 45 | else 46 | { 47 | std::srand(std::time(NULL)); 48 | 49 | cout << "(Loud drill noises) " << endl; 50 | 51 | if (random() % 2 == 0) 52 | cout << this->getTarget() << " successfully robotomized." << endl; 53 | else 54 | cout << this->getTarget() << " failed when trying to robotimize." << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /CPP06/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = ScalarConverter 8 | 9 | NAME = convert 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | @echo "" 37 | @echo "TEST 1" 38 | ./$(NAME) 0 39 | @echo "" 40 | @echo "TEST 2" 41 | ./$(NAME) nan 42 | @echo "" 43 | @echo "TEST 3" 44 | ./$(NAME) 42.0f 45 | 46 | noleaks: re clean 47 | echo "" 48 | valgrind --leak-check=full -s ./$(NAME) 49 | 50 | .PHONY: all clean fclean re run 51 | 52 | #.SILENT: -------------------------------------------------------------------------------- /CPP06/ex00/includes/ScalarConverter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SCALARCONVERTER_HPP 2 | # define SCALARCONVERTER_HPP 3 | 4 | # include 5 | 6 | class ScalarConverter { 7 | public: 8 | static void convert(const std::string& literal); 9 | }; 10 | 11 | #endif // SCALARCONVERTER_HPP -------------------------------------------------------------------------------- /CPP06/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "ScalarConverter.hpp" 4 | 5 | using std::cerr; 6 | 7 | int main(int argc, char **argv) 8 | { 9 | if (argc != 2 || !argv[1][0]) 10 | { 11 | cerr << "Usage: ./convert " << "\n"; 12 | return EXIT_FAILURE; 13 | } 14 | ScalarConverter::convert(argv[1]); 15 | return EXIT_SUCCESS; 16 | } 17 | -------------------------------------------------------------------------------- /CPP06/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Serialization 8 | 9 | NAME = serializer 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | @echo "" 37 | ./$(NAME) 38 | 39 | noleaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP06/ex01/includes/Data.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DATA_HPP 2 | # define DATA_HPP 3 | 4 | # include 5 | 6 | typedef struct s_data { 7 | std::string dummy_str; 8 | } data_t; 9 | 10 | #endif // DATA_HPP -------------------------------------------------------------------------------- /CPP06/ex01/includes/Serialization.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SERIALIZATION_HPP 2 | # define SERIALIZATION_HPP 3 | 4 | # include 5 | # include 6 | # include "Data.hpp" 7 | 8 | class Serialization { 9 | public: 10 | static uintptr_t serialize(data_t *ptr); 11 | static data_t *deserialize(uintptr_t raw); 12 | }; 13 | 14 | #endif // SERIALIZATION_HPP -------------------------------------------------------------------------------- /CPP06/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Serialization.hpp" 4 | 5 | using std::cout; 6 | using std::cerr; 7 | using std::endl; 8 | 9 | int main(int argc, char **argv) 10 | { 11 | if (argc > 1 && argv) 12 | { 13 | cerr << "serializer: error: command-line arguments aren't supported" << '\n'; 14 | return EXIT_FAILURE; 15 | } 16 | 17 | data_t foo; 18 | uintptr_t reinterpreted; 19 | 20 | foo.dummy_str = std::string("test"); 21 | reinterpreted = Serialization::serialize(&foo); 22 | 23 | cout << "OG foo (Data) Address = " << &foo << endl 24 | << "dummy_str value = " << foo.dummy_str << endl; 25 | 26 | cout << endl; 27 | 28 | cout << "Reinterpreted foo value = " << reinterpreted << endl; 29 | 30 | cout << endl; 31 | 32 | data_t *deserialized; 33 | 34 | deserialized = Serialization::deserialize(reinterpreted); 35 | 36 | cout << "Deserialized Address = " << deserialized << endl 37 | << "dummy_str value = " << deserialized->dummy_str << endl; 38 | 39 | return EXIT_SUCCESS; 40 | } 41 | -------------------------------------------------------------------------------- /CPP06/ex01/srcs/Serialization.cpp: -------------------------------------------------------------------------------- 1 | #include "Serialization.hpp" 2 | 3 | uintptr_t Serialization::serialize(data_t *ptr) { 4 | return reinterpret_cast(ptr); 5 | } 6 | 7 | data_t *Serialization::deserialize(uintptr_t raw) { 8 | return reinterpret_cast(raw); 9 | } 10 | -------------------------------------------------------------------------------- /CPP06/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 #-fsanitize=address 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Base functions 8 | 9 | NAME = realtype 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | @echo "" 37 | ./$(NAME) 38 | 39 | noleaks: re clean 40 | echo "" 41 | valgrind --leak-check=full -s ./$(NAME) 42 | 43 | .PHONY: all clean fclean re run 44 | 45 | #.SILENT: -------------------------------------------------------------------------------- /CPP06/ex02/includes/A.hpp: -------------------------------------------------------------------------------- 1 | #ifndef A_HPP 2 | # define A_HPP 3 | 4 | # include "Base.hpp" 5 | 6 | class A : public Base {}; 7 | 8 | #endif // A_HPP -------------------------------------------------------------------------------- /CPP06/ex02/includes/B.hpp: -------------------------------------------------------------------------------- 1 | #ifndef B_HPP 2 | # define B_HPP 3 | 4 | # include "Base.hpp" 5 | 6 | class B : public Base {}; 7 | 8 | #endif // B_HPP -------------------------------------------------------------------------------- /CPP06/ex02/includes/Base.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BASE_HPP 2 | # define BASE_HPP 3 | 4 | class Base { 5 | public: 6 | virtual ~Base(void); 7 | }; 8 | 9 | #endif // BASE_HPP -------------------------------------------------------------------------------- /CPP06/ex02/includes/C.hpp: -------------------------------------------------------------------------------- 1 | #ifndef C_HPP 2 | # define C_HPP 3 | 4 | # include "Base.hpp" 5 | 6 | class C : public Base {}; 7 | 8 | #endif // C_HPP -------------------------------------------------------------------------------- /CPP06/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Base.hpp" 4 | 5 | using std::cout; 6 | using std::cerr; 7 | using std::endl; 8 | 9 | Base *generate(void); 10 | void identify(Base *p); 11 | void identify(Base &p); 12 | 13 | int main(int argc, char **argv) 14 | { 15 | if (argc > 1 && argv) 16 | { 17 | cerr << "realtype: error: command-line arguments aren't supported" << endl; 18 | return EXIT_FAILURE; 19 | } 20 | Base *c; 21 | 22 | cout << "TEST 1" << endl; 23 | c = generate(); 24 | 25 | cout << endl; 26 | identify(c); 27 | identify(*c); 28 | 29 | delete c; 30 | return EXIT_SUCCESS; 31 | } 32 | -------------------------------------------------------------------------------- /CPP06/ex02/srcs/Base.cpp: -------------------------------------------------------------------------------- 1 | #include "Base.hpp" 2 | 3 | Base::~Base(void) {}; 4 | -------------------------------------------------------------------------------- /CPP06/ex02/srcs/functions.cpp: -------------------------------------------------------------------------------- 1 | #include "A.hpp" 2 | #include "B.hpp" 3 | #include "C.hpp" 4 | #include 5 | #include 6 | 7 | using std::cout; 8 | using std::cerr; 9 | using std::endl; 10 | 11 | Base *generate(void) 12 | { 13 | srand(time(NULL)); 14 | char c = "ABC"[rand() % 3]; 15 | 16 | cout << "Type " << c << " created!" << endl; 17 | switch (c) 18 | { 19 | case 'A': 20 | return new A; 21 | case 'B': 22 | return new B; 23 | case 'C': 24 | return new C; 25 | } 26 | return NULL; 27 | } 28 | 29 | void identify(Base *p) 30 | { 31 | // just to shut "unused-value" compiler warning 32 | bool sucessfully_casted; 33 | 34 | cout << "Identify with Base *: "; 35 | 36 | sucessfully_casted = dynamic_cast(p); 37 | if (sucessfully_casted) 38 | { 39 | cout << "A" << endl; 40 | return ; 41 | } 42 | sucessfully_casted = dynamic_cast(p); 43 | if (sucessfully_casted) 44 | { 45 | cout << "B" << endl; 46 | return ; 47 | } 48 | sucessfully_casted = dynamic_cast(p); 49 | if (sucessfully_casted) 50 | { 51 | cout << "C" << endl; 52 | return ; 53 | } 54 | if (!sucessfully_casted) 55 | cout << "Invalid type" << endl; 56 | } 57 | 58 | void identify(Base &p) 59 | { 60 | cout << "Identify with Base &: "; 61 | try { 62 | A &a = dynamic_cast(p); 63 | (void)a; 64 | cout << "A" << endl; 65 | } 66 | catch (const std::exception& e) { 67 | try { 68 | B &b = dynamic_cast(p); 69 | (void)b; 70 | cout << "B" << endl; 71 | } 72 | catch (const std::exception& e) { 73 | try { 74 | C &c = dynamic_cast(p); 75 | (void)c; 76 | cout << "C" << endl; 77 | } 78 | catch (const std::exception& e) { 79 | cout << "Invalid type" << endl; 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /CPP07/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = 8 | 9 | NAME = whatever 10 | SRCS = main.cpp 11 | 12 | OBJ_DIR = obj 13 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 14 | 15 | all: $(NAME) 16 | 17 | $(NAME): $(OBJ_DIR) $(OBJS) 18 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 19 | 20 | $(OBJ_DIR): 21 | mkdir -p obj 22 | 23 | $(OBJ_DIR)/%.o: %.cpp 24 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 25 | 26 | clean: 27 | $(RM) $(OBJ_DIR) 28 | 29 | fclean: clean 30 | $(RM) $(NAME) 31 | 32 | re: fclean all 33 | 34 | run: all clean 35 | ./$(NAME) 36 | 37 | noleaks: re clean 38 | echo "" 39 | valgrind --leak-check=full -s ./$(NAME) 40 | 41 | .PHONY: all clean fclean re run 42 | 43 | #.SILENT: -------------------------------------------------------------------------------- /CPP07/ex00/includes/whatever.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WHATEVER_HPP 2 | # define WHATEVER_HPP 3 | 4 | template 5 | void swap(T& parameter1, T& parameter2) 6 | { 7 | T temp; 8 | 9 | temp = parameter1; 10 | parameter1 = parameter2; 11 | parameter2 = temp; 12 | } 13 | 14 | template 15 | T min(T parameter1, T parameter2) 16 | { 17 | if (parameter1 == parameter2) 18 | return parameter2; 19 | return parameter1 > parameter2 ? parameter2 : parameter1; 20 | } 21 | 22 | template 23 | T max(T parameter1, T parameter2) 24 | { 25 | if (parameter1 == parameter2) 26 | return parameter2; 27 | return parameter1 > parameter2 ? parameter1 : parameter2; 28 | } 29 | 30 | #endif // WHATEVER_HPP -------------------------------------------------------------------------------- /CPP07/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cout; 7 | using std::endl; 8 | 9 | int main(void) 10 | { 11 | int a = 2; 12 | int b = 3; 13 | 14 | ::swap(a, b); 15 | 16 | cout << "a = " << a << ", b = " << b << endl; 17 | cout << "min(a, b) = " << ::min(a, b) << endl; 18 | cout << "max(a, b) = " << ::max(a, b) << endl; 19 | 20 | std::string c = "chaine1"; 21 | std::string d = "chaine2"; 22 | 23 | ::swap(c, d); 24 | 25 | cout << "c = " << c << ", d = " << d << endl; 26 | cout << "min(c, d) = " << ::min(c, d) << endl; 27 | cout << "max(c, d) = " << ::max(c, d) << endl; 28 | 29 | return EXIT_SUCCESS; 30 | } 31 | -------------------------------------------------------------------------------- /CPP07/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = 8 | 9 | NAME = iter 10 | SRCS = main.cpp 11 | 12 | OBJ_DIR = obj 13 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 14 | 15 | all: $(NAME) 16 | 17 | $(NAME): $(OBJ_DIR) $(OBJS) 18 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 19 | 20 | $(OBJ_DIR): 21 | mkdir -p obj 22 | 23 | $(OBJ_DIR)/%.o: %.cpp 24 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 25 | 26 | clean: 27 | $(RM) $(OBJ_DIR) 28 | 29 | fclean: clean 30 | $(RM) $(NAME) 31 | 32 | re: fclean all 33 | 34 | run: all clean 35 | @echo "" 36 | @echo "TEST 1" 37 | ./$(NAME) 0 38 | @echo "" 39 | @echo "TEST 2" 40 | ./$(NAME) nan 41 | @echo "" 42 | @echo "TEST 3" 43 | ./$(NAME) 42.0f 44 | 45 | noleaks: re clean 46 | echo "" 47 | valgrind --leak-check=full -s ./$(NAME) 48 | 49 | .PHONY: all clean fclean re run 50 | 51 | #.SILENT: -------------------------------------------------------------------------------- /CPP07/ex01/includes/iter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ITER_HPP 2 | # define ITER_HPP 3 | 4 | # include 5 | # include 6 | 7 | using std::cout; 8 | using std::endl; 9 | 10 | template 11 | void print_element(T &array_element) { 12 | cout << array_element << endl; 13 | } 14 | 15 | template 16 | void plus_one(T &n) { 17 | n += 1; 18 | } 19 | 20 | template 21 | void iter(T *address, size_t length, void(*fn)(T&)) 22 | { 23 | for (size_t i = 0; i < length; i += 1) { 24 | fn(address[i]); 25 | } 26 | } 27 | 28 | #endif // ITER_HPP -------------------------------------------------------------------------------- /CPP07/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cout; 7 | using std::endl; 8 | 9 | # define ARRAY_LEN 3 10 | 11 | int main(void) 12 | { 13 | int int_array[] = {0, 0, 0}; 14 | 15 | cout << "BEFORE plus_one()" << endl; 16 | ::iter(int_array, ARRAY_LEN, &::print_element); 17 | 18 | 19 | ::iter(int_array, ARRAY_LEN, &::plus_one); 20 | 21 | 22 | cout << "AFTER plus_one()" << endl; 23 | ::iter(int_array, ARRAY_LEN, &::print_element); 24 | 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /CPP07/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = 8 | 9 | NAME = array 10 | SRCS = main.cpp 11 | 12 | OBJ_DIR = obj 13 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 14 | 15 | all: $(NAME) 16 | 17 | $(NAME): $(OBJ_DIR) $(OBJS) 18 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 19 | 20 | $(OBJ_DIR): 21 | mkdir -p obj 22 | 23 | $(OBJ_DIR)/%.o: %.cpp 24 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 25 | 26 | clean: 27 | $(RM) $(OBJ_DIR) 28 | 29 | fclean: clean 30 | $(RM) $(NAME) 31 | 32 | re: fclean all 33 | 34 | run: all clean 35 | ./$(NAME) 36 | 37 | noleaks: re clean 38 | echo "" 39 | valgrind --leak-check=full -s ./$(NAME) 40 | 41 | .PHONY: all clean fclean re run 42 | 43 | #.SILENT: -------------------------------------------------------------------------------- /CPP07/ex02/includes/Array.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ARRAY_HPP 2 | # define ARRAY_HPP 3 | 4 | # include 5 | # include 6 | # include 7 | 8 | using std::cout; 9 | using std::endl; 10 | 11 | template 12 | class Array { 13 | public: 14 | Array(void); 15 | Array(uint32_t n); 16 | Array(Array& to_copy); 17 | Array& operator=(Array const& to_copy); 18 | T& operator[](uint32_t n) throw(std::exception); 19 | ~Array(void); 20 | 21 | uint32_t size(void); 22 | class OutOfBoundsException : public std::exception { 23 | public: 24 | virtual const char* what(void) const throw(); 25 | }; 26 | 27 | private: 28 | T *storage; 29 | uint32_t storage_size; 30 | }; 31 | 32 | template 33 | Array::Array(void) : storage(new T[0]()), storage_size(0) { 34 | #ifdef LOGS 35 | cout << "[Array] Default Constructor has been called" << endl; 36 | #endif 37 | } 38 | 39 | template 40 | Array::Array(uint32_t n) : storage(new T[n]()), storage_size(n) { 41 | #ifdef LOGS 42 | cout << "[Array] Parametrized Constructor has been called" << endl; 43 | #endif 44 | } 45 | 46 | template 47 | Array::Array(Array& to_copy) 48 | : storage(new T[to_copy.size()]), storage_size(to_copy.size()) 49 | { 50 | *this = to_copy; 51 | #ifdef LOGS 52 | cout << "[Array] Copy Constructor has been called" << endl; 53 | #endif 54 | } 55 | 56 | template 57 | Array& Array::operator=(Array const& to_copy) 58 | { 59 | delete[] this->storage; 60 | this->storage = new T[to_copy.storage_size]; 61 | this->storage_size = to_copy.storage_size; 62 | for (uint32_t i = 0; i < to_copy.storage_size; i += 1) 63 | this->storage[i] = to_copy.storage[i]; 64 | #ifdef LOGS 65 | cout << "[Array] Copy Assignment Operator has been called" << endl; 66 | #endif 67 | return *this; 68 | } 69 | 70 | template 71 | T& Array::operator[](uint32_t n) throw(std::exception) 72 | { 73 | if (n >= this->storage_size) 74 | throw Array::OutOfBoundsException(); 75 | return this->storage[n]; 76 | } 77 | 78 | template 79 | Array::~Array(void) { 80 | delete[] this->storage; 81 | #ifdef LOGS 82 | cout << "[Array] Default Destructor has been called" << endl; 83 | #endif 84 | } 85 | 86 | template 87 | uint32_t Array::size(void) { 88 | return this->storage_size; 89 | } 90 | 91 | template 92 | char const* Array::OutOfBoundsException::what(void) const throw() { 93 | return "Array: Exception: index out of bounds"; 94 | } 95 | 96 | #endif // ARRAY_HPP -------------------------------------------------------------------------------- /CPP07/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Array.hpp" 5 | 6 | using std::cout; 7 | using std::cerr; 8 | using std::endl; 9 | 10 | /* #define MAX_VAL 750 11 | int main(int, char**) 12 | { 13 | Array numbers(MAX_VAL); 14 | int* mirror = new int[MAX_VAL]; 15 | srand(time(NULL)); 16 | for (int i = 0; i < MAX_VAL; i += 1) 17 | { 18 | const int value = rand(); 19 | numbers[i] = value; 20 | mirror[i] = value; 21 | } 22 | //SCOPE 23 | { 24 | Array tmp = numbers; 25 | Array test(tmp); 26 | } 27 | 28 | for (int i = 0; i < MAX_VAL; i += 1) 29 | { 30 | if (mirror[i] != numbers[i]) 31 | { 32 | cerr << "didn't save the same value!!" << endl; 33 | return EXIT_FAILURE; 34 | } 35 | } 36 | try { 37 | numbers[-2] = 0; 38 | } 39 | catch(const std::exception& e) { 40 | cerr << e.what() << '\n'; 41 | } 42 | try { 43 | numbers[MAX_VAL] = 0; 44 | } 45 | catch(const std::exception& e) { 46 | cerr << e.what() << '\n'; 47 | } 48 | for (int i = 0; i < MAX_VAL; i++) { 49 | numbers[i] = rand(); 50 | } 51 | delete[] mirror;// 52 | return EXIT_SUCCESS; 53 | } */ 54 | 55 | int main(int argc, char **argv) 56 | { 57 | (void)argc; 58 | (void)argv; 59 | Array a(3); 60 | Array b(3); 61 | 62 | for (size_t i = 0; i < a.size(); i += 1) { 63 | cout << "a[" << i << "] = " << a[i] << "\n"; 64 | } 65 | cout << endl; 66 | for (size_t i = 0; i < b.size(); i += 1) { 67 | cout << "b[" << i << "] = " << b[i] << "\n"; 68 | } 69 | a[0] = 1; 70 | a[1] = 1; 71 | a[2] = 1; 72 | cout << endl; 73 | cout << "AFTER FILLING A WITH 1's" << endl; 74 | for (size_t i = 0; i < a.size(); i += 1) { 75 | cout << "a[" << i << "] = " << a[i] << "\n"; 76 | } 77 | cout << endl; 78 | for (size_t i = 0; i < b.size(); i += 1) { 79 | cout << "b[" << i << "] = " << b[i] << "\n"; 80 | } 81 | b = a; 82 | 83 | cout << "AFTER B = A" << endl; 84 | for (size_t i = 0; i < a.size(); i += 1) { 85 | cout << "a[" << i << "] = " << a[i] << "\n"; 86 | } 87 | cout << endl; 88 | for (size_t i = 0; i < b.size(); i += 1) { 89 | cout << "b[" << i << "] = " << b[i] << "\n"; 90 | } 91 | 92 | 93 | // Empty Array 94 | Array empty; 95 | 96 | // OUT OF BOUNDS EXCEPTION 97 | try { 98 | cout << a[a.size() + 1] << endl; 99 | } 100 | catch (std::exception& e) { 101 | cerr << e.what() << endl; 102 | } 103 | 104 | return EXIT_SUCCESS; 105 | } 106 | -------------------------------------------------------------------------------- /CPP08/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = 8 | 9 | NAME = easyfind 10 | SRCS = main.cpp 11 | 12 | OBJ_DIR = obj 13 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 14 | 15 | all: $(NAME) 16 | 17 | $(NAME): $(OBJ_DIR) $(OBJS) 18 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 19 | 20 | $(OBJ_DIR): 21 | mkdir -p obj 22 | 23 | $(OBJ_DIR)/%.o: %.cpp 24 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 25 | 26 | clean: 27 | $(RM) $(OBJ_DIR) 28 | 29 | fclean: clean 30 | $(RM) $(NAME) 31 | 32 | re: fclean all 33 | 34 | run: all clean 35 | ./$(NAME) 36 | 37 | noleaks: re clean 38 | echo "" 39 | valgrind --leak-check=full -s ./$(NAME) 40 | 41 | .PHONY: all clean fclean re run 42 | 43 | #.SILENT: -------------------------------------------------------------------------------- /CPP08/ex00/includes/easyfind.hpp: -------------------------------------------------------------------------------- 1 | #ifndef EASYFIND_HPP 2 | # define EASYFIND_HPP 3 | 4 | # include 5 | # include 6 | # include 7 | 8 | class EasyFindException : public std::exception { 9 | public: 10 | const char *what() const throw() { 11 | return "easyfind: exception: element not found within container"; 12 | } 13 | }; 14 | 15 | template 16 | bool easyfind(T& container, int to_find) 17 | { 18 | if (std::find(container.begin(), container.end(), to_find) == container.end()) 19 | { 20 | throw EasyFindException(); 21 | return false; 22 | } 23 | else 24 | return true; 25 | } 26 | 27 | #endif // EASYFIND_HPP -------------------------------------------------------------------------------- /CPP08/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "easyfind.hpp" 4 | 5 | using std::cout; 6 | using std::cerr; 7 | using std::endl; 8 | 9 | int main(void) 10 | { 11 | // VECTOR // 12 | std::vector v1; 13 | for (size_t i = 0; i <= 10; i += 1) { 14 | v1.push_back(i); 15 | } 16 | 17 | cout << "v1 = "; 18 | for (std::vector::iterator i = v1.begin(), e = v1.end(); i != e; i += 1) { 19 | cout << *i << " "; 20 | } 21 | cout << endl; 22 | 23 | // will find 24 | try { 25 | easyfind(v1, 0); 26 | } 27 | catch (const std::exception& e) { 28 | cerr << e.what() << '\n'; 29 | } 30 | 31 | // will raise EasyFindException 32 | try { 33 | easyfind(v1, 20); 34 | } 35 | catch (const std::exception& e) { 36 | cerr << e.what() << '\n'; 37 | } 38 | return EXIT_SUCCESS; 39 | } 40 | -------------------------------------------------------------------------------- /CPP08/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = Span 8 | 9 | NAME = ex01 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | noleaks: re clean 39 | echo "" 40 | valgrind --leak-check=full -s ./$(NAME) 41 | 42 | .PHONY: all clean fclean re run 43 | 44 | #.SILENT: -------------------------------------------------------------------------------- /CPP08/ex01/includes/Span.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SPAN_HPP 2 | # define SPAN_HPP 3 | 4 | # include 5 | # include 6 | # include 7 | 8 | class Span { 9 | public: 10 | Span(uint32_t N); 11 | Span(const Span &to_copy); 12 | Span &operator=(const Span& to_copy); 13 | ~Span(void); 14 | 15 | void addNumber(int nbr); 16 | void addRandomNumbers(uint32_t quantity); 17 | int shortestSpan(void); 18 | int longestSpan(void); 19 | 20 | void printStorage(void); 21 | 22 | class NotEnoughElementsException : public std::exception { 23 | public: 24 | char const* what() const throw(); 25 | }; 26 | 27 | class BeyondMaxSize : public std::exception { 28 | public: 29 | virtual const char* what() const throw(); 30 | }; 31 | 32 | private: 33 | uint32_t max_size; 34 | std::vector storage; 35 | Span(); 36 | }; 37 | 38 | #endif // SPAN_HPP -------------------------------------------------------------------------------- /CPP08/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Span.hpp" 4 | 5 | using std::cout; 6 | using std::cerr; 7 | using std::endl; 8 | 9 | int main(void) 10 | { 11 | Span sp = Span(5); 12 | 13 | sp.addNumber(6); 14 | sp.addNumber(3); 15 | sp.addNumber(17); 16 | sp.addNumber(9); 17 | sp.addNumber(11); 18 | sp.printStorage(); 19 | cout << sp.shortestSpan() << endl; 20 | cout << sp.longestSpan() << endl; 21 | 22 | 23 | // Pushing beyond N elements 24 | try { 25 | sp.addNumber(42); 26 | } 27 | catch(const std::exception& e) { 28 | cerr << e.what() << '\n'; 29 | } 30 | 31 | // Trying to get a span with less than 2 elements on the vector 32 | Span sp1 = Span(1); 33 | sp1.addNumber(1); 34 | try { 35 | cout << sp1.shortestSpan() << endl; 36 | } 37 | catch(const std::exception& e) { 38 | cerr << e.what() << '\n'; 39 | } 40 | 41 | Span sp2 = Span(5); 42 | 43 | sp2.addRandomNumbers(5); 44 | cout << "sp2 "; 45 | sp2.printStorage(); 46 | cout << "sp2.shortestSpan() = " << sp2.shortestSpan() << endl; 47 | cout << "sp2.longestSpan() = " << sp2.longestSpan() << endl; 48 | return EXIT_SUCCESS; 49 | } 50 | -------------------------------------------------------------------------------- /CPP08/ex01/srcs/Span.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Span.hpp" 6 | 7 | using std::cout; 8 | using std::endl; 9 | 10 | Span::Span(uint32_t N) : max_size(N), storage(0) {}; 11 | 12 | Span::Span(const Span &to_copy) { 13 | *this = to_copy; 14 | } 15 | 16 | Span &Span::operator=(const Span &to_copy) 17 | { 18 | this->max_size = to_copy.max_size; 19 | this->storage = to_copy.storage; 20 | return *this; 21 | } 22 | 23 | Span::~Span(void) {}; 24 | 25 | void Span::addNumber(int nbr) 26 | { 27 | if (this->storage.size() == this->max_size) 28 | throw Span::BeyondMaxSize(); 29 | this->storage.push_back(nbr); 30 | } 31 | 32 | void Span::addRandomNumbers(uint32_t quantity) 33 | { 34 | srand(time(NULL)); 35 | try { 36 | for (uint32_t i = 0; i < quantity; i += 1) 37 | this->addNumber(rand()); 38 | } 39 | catch (std::exception& e) { 40 | cout << e.what() << endl; 41 | } 42 | } 43 | 44 | int Span::shortestSpan(void) 45 | { 46 | std::vector v = this->storage; 47 | 48 | if (v.size() < 2) { 49 | throw Span::NotEnoughElementsException(); 50 | } 51 | 52 | std::sort(v.begin(), v.end()); 53 | int shortest_gap = v.at(1) - v.at(0); 54 | 55 | std::vector::iterator i; 56 | for (i = v.begin(); i != v.end() - 1; i += 1) 57 | { 58 | if (abs(*(i + 1) - *i) < shortest_gap) 59 | shortest_gap = abs(*(i + 1) - *i); 60 | } 61 | return shortest_gap; 62 | } 63 | 64 | int Span::longestSpan(void) 65 | { 66 | std::vector v = this->storage; 67 | 68 | if (v.size() < 2) 69 | throw Span::NotEnoughElementsException(); 70 | 71 | int min = *std::min_element(v.begin(), v.end()); 72 | int max = *std::max_element(v.begin(), v.end()); 73 | 74 | return abs(max - min); 75 | } 76 | 77 | void Span::printStorage(void) 78 | { 79 | std::vector v = this->storage; 80 | std::vector::iterator i; 81 | 82 | cout << "storage = "; 83 | for (i = v.begin(); i != v.end(); i += 1) 84 | cout << *i << " "; 85 | cout << endl; 86 | } 87 | 88 | char const* Span::BeyondMaxSize::what(void) const throw() { 89 | return "Beyond vector's max size"; 90 | } 91 | 92 | char const* Span::NotEnoughElementsException::what(void) const throw() { 93 | return "Vector must have at least 2 elements"; 94 | } 95 | -------------------------------------------------------------------------------- /CPP08/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = 8 | 9 | NAME = ex02 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) 37 | 38 | noleaks: re clean 39 | echo "" 40 | valgrind --leak-check=full -s ./$(NAME) 41 | 42 | .PHONY: all clean fclean re run 43 | 44 | #.SILENT: -------------------------------------------------------------------------------- /CPP08/ex02/includes/MutantStack.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SPAN_HPP 2 | # define SPAN_HPP 3 | 4 | # include 5 | 6 | template 7 | class MutantStack : public std::stack { 8 | public: 9 | typedef typename std::stack::container_type::iterator iterator; 10 | MutantStack(void); 11 | MutantStack(const MutantStack &to_copy); 12 | MutantStack &operator=(const MutantStack& to_copy); 13 | ~MutantStack(void); 14 | 15 | iterator begin(void); 16 | iterator end(void); 17 | }; 18 | 19 | template 20 | MutantStack::MutantStack(void) { 21 | #ifdef DEBUG 22 | cout << "[MutantStack] default constructor called" << endl; 23 | #endif 24 | }; 25 | 26 | template 27 | MutantStack::MutantStack(MutantStack const& to_copy) { 28 | *this = to_copy; 29 | #ifdef DEBUG 30 | cout << "[MutantStack] copy constructor called" << endl; 31 | #endif 32 | } 33 | 34 | template 35 | MutantStack& MutantStack::operator=(MutantStack const& to_copy) 36 | { 37 | std::stack::operator=(to_copy); 38 | return *this; 39 | } 40 | 41 | template 42 | MutantStack::~MutantStack(void) { 43 | #ifdef DEBUG 44 | cout << "[MutantStack] default destructor called" << endl; 45 | #endif 46 | }; 47 | 48 | template 49 | typename MutantStack::iterator MutantStack::begin() { 50 | return this->c.begin(); 51 | } 52 | 53 | template 54 | typename MutantStack::iterator MutantStack::end() { 55 | return this->c.end(); 56 | }; 57 | 58 | #endif // SPAN_HPP -------------------------------------------------------------------------------- /CPP08/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "MutantStack.hpp" 4 | 5 | using std::cout; 6 | using std::cerr; 7 | using std::endl; 8 | 9 | int main(int argc, char **argv) 10 | { 11 | (void)argc; 12 | (void)argv; 13 | 14 | MutantStack ms; 15 | ms.push(1); 16 | ms.push(2); 17 | ms.push(3); 18 | ms.push(4); 19 | ms.push(5); 20 | 21 | MutantStack::iterator begin = ms.begin(); 22 | MutantStack::iterator end = ms.end(); 23 | 24 | cout << "stack = "; 25 | while (begin != end) 26 | { 27 | cout << *begin << " "; 28 | begin += 1; 29 | } 30 | 31 | cout << endl; 32 | return EXIT_SUCCESS; 33 | } 34 | -------------------------------------------------------------------------------- /CPP09/ex00/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = BitcoinExchange 8 | 9 | NAME = btc 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | ./$(NAME) input.txt 37 | 38 | noleaks: re clean 39 | echo "" 40 | valgrind --leak-check=full -s ./$(NAME) 41 | 42 | .PHONY: all clean fclean re run 43 | 44 | #.SILENT: -------------------------------------------------------------------------------- /CPP09/ex00/includes/BitcoinExchange.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BITCOINEXCHANGE_HPP 2 | # define BITCOINEXCHANGE_HPP 3 | 4 | # include 5 | # include 6 | 7 | class BitcoinExchange { 8 | public: 9 | BitcoinExchange(void); 10 | BitcoinExchange(const BitcoinExchange& to_copy); 11 | BitcoinExchange& operator=(const BitcoinExchange& to_copy); 12 | ~BitcoinExchange(void); 13 | 14 | void readInternalDataBase(std::ifstream& internal_db); 15 | 16 | float getRateFromDataBase(const std::string& date); 17 | 18 | bool isDateInCorrectFormat(const std::string &date); 19 | bool isValidDate(const std::string& date); 20 | bool isRateInCorrectFormat(const std::string& rate); 21 | 22 | private: 23 | std::map dataBase; 24 | }; 25 | 26 | #endif // BITCOINEXCHANGE_HPP -------------------------------------------------------------------------------- /CPP09/ex00/input.txt: -------------------------------------------------------------------------------- 1 | date | value 2 | 2011-01-03 | 3 3 | 2011-01-03 | 2 4 | 2011-01-03 | 1 5 | 2011-01-03 | 1.2 6 | 2011-01-09 | 1 7 | 2012-01-11 | -1 8 | 2001-42-42 9 | 2012-01-11 | 1 10 | 2012-01-11 | 2147483648 -------------------------------------------------------------------------------- /CPP09/ex00/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "BitcoinExchange.hpp" 9 | 10 | using std::cout; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | #define BAD_INPUT_ERR "Error: bad input => " 15 | #define FILE_OPEN_ERR "Error: could not open file" 16 | #define INTERNAL_DB_OPEN_ERR "Error: fatal: could not open internal database file" 17 | #define INTERNAL_DB_FILE "./data.csv" 18 | 19 | float ft_stof(const std::string& str); 20 | 21 | static int panic(std::string error_msg) 22 | { 23 | cerr << error_msg << "\n"; 24 | return EXIT_FAILURE; 25 | } 26 | 27 | int main(int argc, char **argv) 28 | { 29 | if (argc != 2) 30 | return panic(FILE_OPEN_ERR); 31 | 32 | std::ifstream input_db(argv[1], std::ifstream::in); 33 | if (!input_db.is_open()) 34 | return panic(FILE_OPEN_ERR); 35 | 36 | std::ifstream internal_db(INTERNAL_DB_FILE, std::ifstream::in); 37 | if (!internal_db.is_open()) 38 | return panic(INTERNAL_DB_OPEN_ERR); 39 | 40 | BitcoinExchange btc; 41 | btc.readInternalDataBase(internal_db); 42 | 43 | std::string line; 44 | 45 | // skip first line 46 | std::getline(input_db, line); 47 | while (std::getline(input_db, line)) 48 | { 49 | size_t delim = line.find('|'); 50 | if (delim == std::string::npos 51 | || line.length() < delim + 2) 52 | { 53 | cerr << BAD_INPUT_ERR << "\"" << line << "\"" << '\n'; 54 | continue ; 55 | } 56 | 57 | std::string date = line.substr(0, delim - 1); 58 | if (!btc.isDateInCorrectFormat(date) || !btc.isValidDate(date)) 59 | continue; 60 | 61 | std::string rate_as_str = line.substr(delim + 2); 62 | if (!btc.isRateInCorrectFormat(rate_as_str)) 63 | continue; 64 | float rate = ft_stof(rate_as_str); 65 | 66 | cout << date << " => " << rate << " = " << std::setprecision(2) << rate * btc.getRateFromDataBase(date) << endl; 67 | } 68 | input_db.close(); 69 | internal_db.close(); 70 | return EXIT_SUCCESS; 71 | } 72 | -------------------------------------------------------------------------------- /CPP09/ex01/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = RPN 8 | 9 | NAME = RPN 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | @echo "" 37 | @echo "TEST 1" 38 | ./$(NAME) "8 9 * 9 - 9 - 9 - 4 - 1 +" 39 | @echo "" 40 | @echo "TEST 2" 41 | ./$(NAME) "7 7 * 7 -" 42 | @echo "" 43 | @echo "TEST 3" 44 | ./$(NAME) "1 2 * 2 / 2 * 2 4 - +" 45 | @echo "" 46 | @echo "TEST 4" 47 | ./$(NAME) "1 0 /" 48 | @echo "" 49 | @echo "TEST 5" 50 | ./$(NAME) "3 3 * 9 / 1" 51 | @echo "" 52 | @echo "TEST 6" 53 | ./$(NAME) "(1 + 1)" 54 | 55 | noleaks: re clean 56 | echo "" 57 | valgrind --leak-check=full -s ./$(NAME) 58 | 59 | .PHONY: all clean fclean re run 60 | 61 | #.SILENT: -------------------------------------------------------------------------------- /CPP09/ex01/includes/RPN.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RPN_HPP 2 | # define RPN_HPP 3 | 4 | # include 5 | # include 6 | 7 | class RPN { 8 | public: 9 | RPN(void); 10 | RPN(const RPN& to_copy); 11 | RPN &operator=(const RPN& to_copy); 12 | ~RPN(void); 13 | 14 | static bool valid_expression(const std::string& expr); 15 | static long long calculate(const std::string& expr); 16 | 17 | class NoResultException : public std::exception { 18 | public: 19 | virtual const char* what() const throw(); 20 | }; 21 | 22 | class DivisionByZeroException : public std::exception { 23 | public: 24 | virtual const char* what() const throw(); 25 | }; 26 | }; 27 | 28 | #endif // RPN_HPP -------------------------------------------------------------------------------- /CPP09/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "RPN.hpp" 4 | 5 | using std::cout; 6 | using std::cerr; 7 | using std::endl; 8 | 9 | # define ERROR_MSG_PREFFIX "RPN: error: " 10 | # define ARGC_ERR "invalid number of arguments" 11 | # define INVALID_EXPR_ERR "invalid expression" 12 | 13 | static int panic(const std::string& error_msg) 14 | { 15 | cerr << ERROR_MSG_PREFFIX << error_msg << '\n'; 16 | return EXIT_FAILURE; 17 | } 18 | 19 | int main(int argc, char **argv) 20 | { 21 | if (argc != 2) 22 | return panic(ARGC_ERR); 23 | 24 | if (!RPN::valid_expression(argv[1])) 25 | return panic(INVALID_EXPR_ERR); 26 | 27 | try { 28 | cout << RPN::calculate(argv[1]) << endl; 29 | } 30 | catch (std::exception& e) { 31 | cerr << e.what() << "\n"; 32 | } 33 | return EXIT_SUCCESS; 34 | } 35 | -------------------------------------------------------------------------------- /CPP09/ex01/srcs/RPN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "RPN.hpp" 6 | 7 | using std::cout; 8 | using std::cerr; 9 | using std::endl; 10 | 11 | static int ft_stoi(const std::string& str) 12 | { 13 | int num; 14 | std::stringstream ss(str); 15 | 16 | ss >> num; 17 | return num; 18 | } 19 | 20 | RPN::RPN(void) {}; 21 | 22 | RPN::RPN(const RPN &to_copy) { 23 | *this = to_copy; 24 | } 25 | 26 | RPN& RPN::operator=(const RPN &to_copy) { 27 | (void)to_copy; 28 | return *this; 29 | } 30 | 31 | RPN::~RPN(void) {}; 32 | 33 | /** 34 | * @brief Checks if the is only composed by 35 | * operands, operators and spaces 36 | * 37 | * @param expr mathematical expression in RPN notation 38 | */ 39 | bool RPN::valid_expression(const std::string& expr) 40 | { 41 | if (expr.find_first_not_of("0123456789+-/* ") == std::string::npos) 42 | return true; 43 | return false; 44 | } 45 | 46 | /** 47 | * @brief Calculates the result of the expression passed as 48 | * a parameter. Raises an exception when an expression is 49 | * wrongly formatted according to the RPN notation or when 50 | * trying to divide something by 0. 51 | * 52 | * @param expr mathematical expression in RPN notation 53 | * @return result 54 | */ 55 | long long RPN::calculate(const std::string& expr) 56 | { 57 | int left; 58 | int right; 59 | int result; 60 | std::stringstream postfix(expr); 61 | std::stack temp; 62 | std::string s; 63 | 64 | while (postfix >> s) 65 | { 66 | if (s == "+" || s == "-" || s == "/" || s == "*") 67 | { 68 | if (temp.size() < 2) 69 | throw NoResultException(); 70 | // Pull out top two elements 71 | right = temp.top(); 72 | temp.pop(); 73 | left = temp.top(); 74 | temp.pop(); 75 | switch (s.at(0)) 76 | { 77 | case '+': result = left + right ; break; 78 | case '-': result = left - right ; break; 79 | case '/': 80 | if (right != 0) 81 | result = left / right; 82 | else 83 | throw DivisionByZeroException(); 84 | break; 85 | case '*': result = left * right ; break; 86 | } 87 | temp.push(result); // push the result of the above operation 88 | } 89 | else 90 | temp.push(ft_stoi(s)); 91 | } 92 | // last element on the stack is the answer 93 | return temp.top(); 94 | } 95 | 96 | const char* RPN::NoResultException::what() const throw() { 97 | return "RPN exception: No result. Wrongly formatted expression"; 98 | } 99 | 100 | const char* RPN::DivisionByZeroException::what() const throw() { 101 | return "RPN exception: division by zero"; 102 | } 103 | -------------------------------------------------------------------------------- /CPP09/ex02/Makefile: -------------------------------------------------------------------------------- 1 | CC = c++ 2 | CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address -O3 #-DLOGS 3 | INCLUDE = -I./includes 4 | VPATH = srcs 5 | RM = rm -rf 6 | 7 | GENERAL = PmergeMe 8 | 9 | NAME = PmergeMe 10 | SRCS = $(addsuffix .cpp, $(GENERAL))\ 11 | main.cpp 12 | 13 | OBJ_DIR = obj 14 | OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o) 15 | 16 | all: $(NAME) 17 | 18 | $(NAME): $(OBJ_DIR) $(OBJS) 19 | $(CC) $(CFLAGS) $(OBJS) -o $(NAME) 20 | 21 | $(OBJ_DIR): 22 | mkdir -p obj 23 | 24 | $(OBJ_DIR)/%.o: %.cpp 25 | $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) 26 | 27 | clean: 28 | $(RM) $(OBJ_DIR) 29 | 30 | fclean: clean 31 | $(RM) $(NAME) 32 | 33 | re: fclean all 34 | 35 | run: all clean 36 | @echo "" 37 | @echo "TEST 1" 38 | ./$(NAME) 3 5 9 7 4 39 | @echo "" 40 | # @echo "TEST 2" 41 | # ./$(NAME) `shuf -i 1-100000 -n 3000 | tr "\n" " "` 42 | @echo "TEST 4" 43 | ./$(NAME) -1 2 44 | @echo "" 45 | 46 | noleaks: re clean 47 | echo "" 48 | valgrind --leak-check=full -s ./$(NAME) 49 | 50 | .PHONY: all clean fclean re run 51 | 52 | #.SILENT: -------------------------------------------------------------------------------- /CPP09/ex02/includes/PmergeMe.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PMERGEME_HPP 2 | # define PMERGEME_HPP 3 | 4 | # include 5 | 6 | class PmergeMe { 7 | public: 8 | PmergeMe(void); 9 | PmergeMe(const PmergeMe& to_copy); 10 | PmergeMe &operator=(const PmergeMe& to_copy); 11 | ~PmergeMe(void); 12 | 13 | void sortVec(int argc, char **argv); 14 | void sortList(int argc, char **argv); 15 | 16 | class InvalidElementException : public std::exception { 17 | public: 18 | virtual const char* what() const throw(); 19 | }; 20 | }; 21 | 22 | #endif // PMERGEME_HPP -------------------------------------------------------------------------------- /CPP09/ex02/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "PmergeMe.hpp" 7 | 8 | using std::cout; 9 | using std::cerr; 10 | using std::endl; 11 | 12 | unsigned int ft_stou(const std::string& str) 13 | { 14 | unsigned int num; 15 | std::stringstream ss(str); 16 | 17 | ss >> num; 18 | return num; 19 | } 20 | 21 | int main(int argc, char **argv) 22 | { 23 | if (argc == 1) 24 | return EXIT_SUCCESS; 25 | 26 | PmergeMe pmm; 27 | 28 | try { 29 | for (int i = 1; i < argc; i += 1) 30 | if (std::string(argv[i]).find_first_not_of("0123456789 ") != std::string::npos) 31 | throw PmergeMe::InvalidElementException(); 32 | 33 | std::vectortemp; 34 | for (int i = 1; i < argc; i += 1) 35 | temp.push_back(ft_stou(argv[i])); 36 | 37 | std::vector::iterator itr; 38 | std::vector::iterator itr2; 39 | for (itr = temp.begin(); itr != temp.end(); itr++) 40 | { 41 | for (itr2 = itr + 1; itr2 != temp.end(); itr2++) 42 | if (*itr2 == *itr) 43 | throw PmergeMe::InvalidElementException(); 44 | } 45 | } 46 | catch (std::exception& e) { 47 | cerr << e.what() << '\n'; 48 | return EXIT_FAILURE; 49 | } 50 | 51 | pmm.sortVec(argc, argv); 52 | cout << endl; 53 | pmm.sortList(argc, argv); 54 | 55 | return EXIT_SUCCESS; 56 | } 57 | --------------------------------------------------------------------------------