diff --git a/cpp00/ex00/Makefile b/cpp00/ex00/Makefile new file mode 100644 index 0000000..9473fd1 --- /dev/null +++ b/cpp00/ex00/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/23 16:15:46 by fgras-ca #+# #+# # +# Updated: 2023/12/23 16:39:11 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = megaphone + +SRC = megaphone.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation megaphone... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./megaphone" with arguments or not!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp00/ex00/a.out b/cpp00/ex00/a.out new file mode 100755 index 0000000..5b33162 Binary files /dev/null and b/cpp00/ex00/a.out differ diff --git a/cpp00/ex00/megaphone b/cpp00/ex00/megaphone new file mode 100755 index 0000000..5b33162 Binary files /dev/null and b/cpp00/ex00/megaphone differ diff --git a/cpp00/ex00/megaphone.cpp b/cpp00/ex00/megaphone.cpp new file mode 100644 index 0000000..dac741e --- /dev/null +++ b/cpp00/ex00/megaphone.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* megaphone.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/23 16:14:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/23 16:28:52 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +int main(int argc, char **argv) +{ + if (argc == 1) + { + std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl; + return (0); + } + for (int i = 1; i < argc; i++) + { + char *str = argv[i]; + for (int j = 0; str[j]; j++) + { + std::cout << (char)toupper(str[j]); + } + std::cout << " "; + } + std::cout << std::endl; + return (0); +} \ No newline at end of file diff --git a/cpp00/ex00/megaphone.o b/cpp00/ex00/megaphone.o new file mode 100644 index 0000000..5090035 Binary files /dev/null and b/cpp00/ex00/megaphone.o differ diff --git a/cpp00/ex01/.vscode/c_cpp_properties.json b/cpp00/ex01/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp00/ex01/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp00/ex01/.vscode/launch.json b/cpp00/ex01/.vscode/launch.json new file mode 100644 index 0000000..7df13e0 --- /dev/null +++ b/cpp00/ex01/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp00/ex01", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp00/ex01/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp00/ex01/.vscode/settings.json b/cpp00/ex01/.vscode/settings.json new file mode 100644 index 0000000..93af90c --- /dev/null +++ b/cpp00/ex01/.vscode/settings.json @@ -0,0 +1,108 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp00/ex01/Contact.cpp b/cpp00/ex01/Contact.cpp new file mode 100644 index 0000000..cc78b39 --- /dev/null +++ b/cpp00/ex01/Contact.cpp @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Contact.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/23 17:17:14 by fgras-ca #+# #+# */ +/* Updated: 2023/12/23 20:52:55 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Contact.hpp" + +Contact::Contact() {} + +void Contact::SetDetails() +{ + std::cout << "First Name: "; + std::cin >> _first_name; + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::cout << "Last Name: "; + std::cin >> _last_name; + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::cout << "Nickname: "; + std::cin >> _nickname; + std::cin.ignore(std::numeric_limits::max(), '\n'); + while (true) + { + std::cout << "Phone Number: "; + std::cin >> _phone_number; + + bool isValid = true; + for (char c : _phone_number) + { + if (!std::isdigit(c)) + { + isValid = false; + break; + } + } + if (isValid) + { + std::cin.ignore(std::numeric_limits::max(), '\n'); + break; + } + else + { + std::cout << "Invalid input. A phone number should contain only digits (0-9)." << std::endl; + std::cin.ignore(std::numeric_limits::max(), '\n'); + } + } + std::cout << "Darkest Secret: "; + std::getline(std::cin, _darkest_secret); +} + +Contact::~Contact() {} + +void Contact::DisplaySummary(int index) const +{ + std::string redBar = "\033[31m|\033[0m"; + std::cout << std::setw(8) << index << redBar; + if (_first_name.length() > 10) + { + std::cout << std::setw(10) << _first_name.substr(0,9) + "." << redBar; + } + else + { + std::cout << std::setw(10) << _first_name << redBar; + } + if (_last_name.length() > 10) + { + std::cout << std::setw(10) << _last_name.substr(0,9) + "." << redBar; + } + else + { + std::cout << std::setw(10) << _last_name << redBar; + } + if (_nickname.length() > 10) + { + std::cout << std::setw(10) << _nickname.substr(0,9) + "." << redBar; + } + else + { + std::cout << std::setw(10) << _nickname << redBar; + } + std::cout << std::endl; +} + +void Contact::DisplayDetails() const +{ + std::cout << "First Name: " << _first_name << std::endl; + std::cout << "Last Name: " << _last_name << std::endl; + std::cout << "Nickname: " << _nickname << std::endl; + std::cout << "Phone Number: " << _phone_number << std::endl; + std::cout << "Darkest Secret: " << _darkest_secret << std::endl; +} \ No newline at end of file diff --git a/cpp00/ex01/Contact.hpp b/cpp00/ex01/Contact.hpp new file mode 100644 index 0000000..4650fa4 --- /dev/null +++ b/cpp00/ex01/Contact.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Contact.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/23 16:56:55 by fgras-ca #+# #+# */ +/* Updated: 2023/12/23 19:49:10 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CONTACT_HPP +# define CONTACT_HPP + +#include +#include +#include +#include + +class Contact +{ + public: + Contact(); + ~Contact(); + void SetDetails(); + void DisplaySummary(int index) const; + void DisplayDetails() const; + + private: + std::string _first_name; + std::string _last_name; + std::string _nickname; + std::string _phone_number; + std::string _darkest_secret; +}; + +#endif \ No newline at end of file diff --git a/cpp00/ex01/Contact.o b/cpp00/ex01/Contact.o new file mode 100644 index 0000000..7a3f931 Binary files /dev/null and b/cpp00/ex01/Contact.o differ diff --git a/cpp00/ex01/Makefile b/cpp00/ex01/Makefile new file mode 100644 index 0000000..9c546f2 --- /dev/null +++ b/cpp00/ex01/Makefile @@ -0,0 +1,63 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/23 18:31:36 by fgras-ca #+# #+# # +# Updated: 2023/12/23 19:06:37 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = phonebook + +SRC = PhoneBook.cpp \ + Contact.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation bookphone... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./phonebook"!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp00/ex01/PhoneBook.cpp b/cpp00/ex01/PhoneBook.cpp new file mode 100644 index 0000000..3923a9b --- /dev/null +++ b/cpp00/ex01/PhoneBook.cpp @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PhoneBook.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/23 18:01:39 by fgras-ca #+# #+# */ +/* Updated: 2023/12/23 20:52:57 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PhoneBook.hpp" + +PhoneBook::PhoneBook() : next_index(0), total_contacts(0) {} + +PhoneBook::~PhoneBook() {} + +void PhoneBook::AddContact() +{ + contacts[next_index].SetDetails(); + next_index = (next_index + 1) % 8; + if (total_contacts < 8) + { + total_contacts++; + } + std::cout << "Contact added successfully!" << std::endl; +} + +void PhoneBook::SearchContact() const +{ + std::cout << "\033[31m"; + std::cout << "------------------------------------------" << std::endl; + std::cout << std::setw(8) << "Index" << "|" + << std::setw(10) << "First" << "|" + << std::setw(10) << "Last" << "|" + << std::setw(10) << "Nickname" << "|" << std::endl; + std::cout << "------------------------------------------" << std::endl; + std::cout << "\033[0m"; + for (int i = 0; i < total_contacts; ++i) + { + contacts[i].DisplaySummary(i + 1); + } + std::cout << "\033[31m"; + std::cout << "------------------------------------------" << std::endl; + std::cout << "\033[0m"; + while (true) + { + std::cout << "Enter the index of the contact to display: "; + int index; + std::cin >> index; + + if (std::cin.fail()) + { + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::cout << "Invalid input. Please enter a number." << std::endl; + } + else if (index < 1 || index > total_contacts) + { + std::cout << "Invalid index. Please enter a number between 1 and " << total_contacts << "." << std::endl; + } + else + { + contacts[index - 1].DisplayDetails(); + break; + } + } +} \ No newline at end of file diff --git a/cpp00/ex01/PhoneBook.hpp b/cpp00/ex01/PhoneBook.hpp new file mode 100644 index 0000000..4287087 --- /dev/null +++ b/cpp00/ex01/PhoneBook.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PhoneBook.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/23 17:55:10 by fgras-ca #+# #+# */ +/* Updated: 2023/12/23 19:52:34 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PHONEBOOK_HPP +# define PHONEBOOK_HPP + +#include "Contact.hpp" +#include +#include +#include +#include + +class PhoneBook +{ + public: + PhoneBook(); + ~PhoneBook(); + + void AddContact(); + void SearchContact() const; + + private: + Contact contacts[8]; + int next_index; + int total_contacts; +}; + +#endif \ No newline at end of file diff --git a/cpp00/ex01/PhoneBook.o b/cpp00/ex01/PhoneBook.o new file mode 100644 index 0000000..ec4463e Binary files /dev/null and b/cpp00/ex01/PhoneBook.o differ diff --git a/cpp00/ex01/main.cpp b/cpp00/ex01/main.cpp new file mode 100644 index 0000000..093ad4e --- /dev/null +++ b/cpp00/ex01/main.cpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/23 18:16:58 by fgras-ca #+# #+# */ +/* Updated: 2023/12/23 18:31:05 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Contact.hpp" +#include "PhoneBook.hpp" + +int main() +{ + PhoneBook phonebook; + std::string command; + + while (true) + { + std::cout << "Enter command (ADD, SEARCH, or EXIT): "; + std::cin >> command; + + if (command == "ADD") + { + phonebook.AddContact(); + } + else if (command == "SEARCH") + { + phonebook.SearchContact(); + } + else if (command == "EXIT") + { + break; + } + else + { + std::cout << "Command not recognized." << std::endl; + } + } + return (0); +} \ No newline at end of file diff --git a/cpp00/ex01/main.o b/cpp00/ex01/main.o new file mode 100644 index 0000000..c173391 Binary files /dev/null and b/cpp00/ex01/main.o differ diff --git a/cpp00/ex01/phonebook b/cpp00/ex01/phonebook new file mode 100755 index 0000000..d1e55e6 Binary files /dev/null and b/cpp00/ex01/phonebook differ diff --git a/cpp00/ex02/.vscode/c_cpp_properties.json b/cpp00/ex02/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp00/ex02/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp00/ex02/.vscode/launch.json b/cpp00/ex02/.vscode/launch.json new file mode 100644 index 0000000..ef89af7 --- /dev/null +++ b/cpp00/ex02/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp00/ex02", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp00/ex02/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp00/ex02/.vscode/settings.json b/cpp00/ex02/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp00/ex02/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp00/ex02/19920104_091532.log b/cpp00/ex02/19920104_091532.log new file mode 100644 index 0000000..7263771 --- /dev/null +++ b/cpp00/ex02/19920104_091532.log @@ -0,0 +1,59 @@ +[19920104_091532] index:0;amount:42;created +[19920104_091532] index:1;amount:54;created +[19920104_091532] index:2;amount:957;created +[19920104_091532] index:3;amount:432;created +[19920104_091532] index:4;amount:1234;created +[19920104_091532] index:5;amount:0;created +[19920104_091532] index:6;amount:754;created +[19920104_091532] index:7;amount:16576;created +[19920104_091532] accounts:8;total:20049;deposits:0;withdrawals:0 +[19920104_091532] index:0;amount:42;deposits:0;withdrawals:0 +[19920104_091532] index:1;amount:54;deposits:0;withdrawals:0 +[19920104_091532] index:2;amount:957;deposits:0;withdrawals:0 +[19920104_091532] index:3;amount:432;deposits:0;withdrawals:0 +[19920104_091532] index:4;amount:1234;deposits:0;withdrawals:0 +[19920104_091532] index:5;amount:0;deposits:0;withdrawals:0 +[19920104_091532] index:6;amount:754;deposits:0;withdrawals:0 +[19920104_091532] index:7;amount:16576;deposits:0;withdrawals:0 +[19920104_091532] index:0;p_amount:42;deposit:5;amount:47;nb_deposits:1 +[19920104_091532] index:1;p_amount:54;deposit:765;amount:819;nb_deposits:1 +[19920104_091532] index:2;p_amount:957;deposit:564;amount:1521;nb_deposits:1 +[19920104_091532] index:3;p_amount:432;deposit:2;amount:434;nb_deposits:1 +[19920104_091532] index:4;p_amount:1234;deposit:87;amount:1321;nb_deposits:1 +[19920104_091532] index:5;p_amount:0;deposit:23;amount:23;nb_deposits:1 +[19920104_091532] index:6;p_amount:754;deposit:9;amount:763;nb_deposits:1 +[19920104_091532] index:7;p_amount:16576;deposit:20;amount:16596;nb_deposits:1 +[19920104_091532] accounts:8;total:21524;deposits:8;withdrawals:0 +[19920104_091532] index:0;amount:47;deposits:1;withdrawals:0 +[19920104_091532] index:1;amount:819;deposits:1;withdrawals:0 +[19920104_091532] index:2;amount:1521;deposits:1;withdrawals:0 +[19920104_091532] index:3;amount:434;deposits:1;withdrawals:0 +[19920104_091532] index:4;amount:1321;deposits:1;withdrawals:0 +[19920104_091532] index:5;amount:23;deposits:1;withdrawals:0 +[19920104_091532] index:6;amount:763;deposits:1;withdrawals:0 +[19920104_091532] index:7;amount:16596;deposits:1;withdrawals:0 +[19920104_091532] index:0;p_amount:47;withdrawal:refused +[19920104_091532] index:1;p_amount:819;withdrawal:34;amount:785;nb_withdrawals:1 +[19920104_091532] index:2;p_amount:1521;withdrawal:657;amount:864;nb_withdrawals:1 +[19920104_091532] index:3;p_amount:434;withdrawal:4;amount:430;nb_withdrawals:1 +[19920104_091532] index:4;p_amount:1321;withdrawal:76;amount:1245;nb_withdrawals:1 +[19920104_091532] index:5;p_amount:23;withdrawal:refused +[19920104_091532] index:6;p_amount:763;withdrawal:657;amount:106;nb_withdrawals:1 +[19920104_091532] index:7;p_amount:16596;withdrawal:7654;amount:8942;nb_withdrawals:1 +[19920104_091532] accounts:8;total:12442;deposits:8;withdrawals:6 +[19920104_091532] index:0;amount:47;deposits:1;withdrawals:0 +[19920104_091532] index:1;amount:785;deposits:1;withdrawals:1 +[19920104_091532] index:2;amount:864;deposits:1;withdrawals:1 +[19920104_091532] index:3;amount:430;deposits:1;withdrawals:1 +[19920104_091532] index:4;amount:1245;deposits:1;withdrawals:1 +[19920104_091532] index:5;amount:23;deposits:1;withdrawals:0 +[19920104_091532] index:6;amount:106;deposits:1;withdrawals:1 +[19920104_091532] index:7;amount:8942;deposits:1;withdrawals:1 +[19920104_091532] index:0;amount:47;closed +[19920104_091532] index:1;amount:785;closed +[19920104_091532] index:2;amount:864;closed +[19920104_091532] index:3;amount:430;closed +[19920104_091532] index:4;amount:1245;closed +[19920104_091532] index:5;amount:23;closed +[19920104_091532] index:6;amount:106;closed +[19920104_091532] index:7;amount:8942;closed diff --git a/cpp00/ex02/Account.cpp b/cpp00/ex02/Account.cpp new file mode 100644 index 0000000..ab12ad5 --- /dev/null +++ b/cpp00/ex02/Account.cpp @@ -0,0 +1,119 @@ +// ************************************************************************** // +// // +// Account.cpp for GlobalBanksters United // +// Created on : Sat Dec 23 19:43:15 2023 // +// // +// Made by : fgras-ca // +// // +// ************************************************************************** // + +#include "Account.hpp" +#include +#include +#include +#include + +// Initialisation des variables statiques +int Account::_nbAccounts = 0; +int Account::_totalAmount = 0; +int Account::_totalNbDeposits = 0; +int Account::_totalNbWithdrawals = 0; + +// Constructeur : Initialise un compte avec un dépôt initial et met à jour les informations globales +Account::Account(int initial_deposit) : _amount(initial_deposit), _nbDeposits(0), _nbWithdrawals(0) +{ + _accountIndex = _nbAccounts++; // Assignation d'un index unique au compte et incrémentation du nombre total de comptes + _totalAmount += _amount; // Mise à jour du montant total détenu par tous les comptes + _displayTimestamp(); // Affichage de l'horodatage de la création du compte + std::cout << "index:" << _accountIndex << ";amount:" << _amount << ";created\n"; // Enregistrement de la création du compte +} + +// Destructeur : Met à jour les informations globales lors de la fermeture d'un compte +Account::~Account() +{ + _nbAccounts--; // Décrémentation du nombre total de comptes + _totalAmount -= _amount; // Mise à jour du montant total détenu par tous les comptes + _displayTimestamp(); // Affichage de l'horodatage de la fermeture du compte + std::cout << "index:" << _accountIndex << ";amount:" << _amount << ";closed\n"; // Enregistrement de la fermeture du compte +} + +// Dépôts : Ajoute un montant au compte et met à jour les informations globales +void Account::makeDeposit(int deposit) +{ + _displayTimestamp(); // Affichage de l'horodatage de l'opération + std::cout << "index:" << _accountIndex << ";p_amount:" << _amount << ";deposit:" << deposit; + _amount += deposit; // Mise à jour du montant dans le compte + _nbDeposits++; // Incrémentation du nombre de dépôts pour ce compte + _totalAmount += deposit; // Mise à jour du montant total détenu par tous les comptes + _totalNbDeposits++; // Incrémentation du nombre total de dépôts effectués sur tous les comptes + std::cout << ";amount:" << _amount << ";nb_deposits:" << _nbDeposits << "\n"; // Enregistrement de l'opération de dépôt +} + +// Retraits : Diminue le montant du compte si possible et met à jour les informations globales +bool Account::makeWithdrawal(int withdrawal) +{ + _displayTimestamp(); // Affichage de l'horodatage de l'opération + std::cout << "index:" << _accountIndex << ";p_amount:" << _amount << ";withdrawal:"; + if (withdrawal > _amount) + { // Vérification si le retrait est possible (fonds suffisants) + std::cout << "refused\n"; // Enregistrement d'un retrait refusé + return (false); + } + _amount -= withdrawal; // Mise à jour du montant dans le compte + _nbWithdrawals++; // Incrémentation du nombre de retraits pour ce compte + _totalAmount -= withdrawal; // Mise à jour du montant total détenu par tous les comptes + _totalNbWithdrawals++; // Incrémentation du nombre total de retraits effectués sur tous les comptes + std::cout << withdrawal << ";amount:" << _amount << ";nb_withdrawals:" << _nbWithdrawals << "\n"; // Enregistrement de l'opération de retrait + return (true); +} + +// Vérifier le montant : Retourne le montant actuel du compte +int Account::checkAmount() const +{ + return (_amount); +} + +// Afficher le statut du compte : Affiche les informations détaillées sur le compte +void Account::displayStatus() const +{ + _displayTimestamp(); // Affichage de l'horodatage de l'opération + std::cout << "index:" << _accountIndex << ";amount:" << _amount + << ";deposits:" << _nbDeposits << ";withdrawals:" << _nbWithdrawals << "\n"; // Enregistrement de l'état actuel du compte +} + +// Retourner le nombre total de comptes +int Account::getNbAccounts() { return _nbAccounts; } + +// Retourner le montant total détenu par tous les comptes +int Account::getTotalAmount() { return _totalAmount; } + +// Retourner le nombre total de dépôts effectués sur tous les comptes +int Account::getNbDeposits() { return _totalNbDeposits; } + +// Retourner le nombre total de retraits effectués sur tous les comptes +int Account::getNbWithdrawals() { return _totalNbWithdrawals; } + +// Afficher les informations globales de tous les comptes +void Account::displayAccountsInfos() +{ + _displayTimestamp(); // Affichage de l'horodatage de l'opération + std::cout << "accounts:" << getNbAccounts() << ";total:" << getTotalAmount() + << ";deposits:" << getNbDeposits() << ";withdrawals:" << getNbWithdrawals() << "\n"; // Enregistrement des informations globales +} + +void Account::_displayTimestamp() +{ +// Obtenir l'heure et la date actuelles + std::time_t now = std::time(nullptr); + std::tm *ltm = std::localtime(&now); + + std::cout << "[" + << 1900 + ltm->tm_year // Ajouter 1900 pour obtenir l'année complète + << std::setw(2) << std::setfill('0') << 1 + ltm->tm_mon // Mois (0-11 donc +1 pour 1-12) + << std::setw(2) << std::setfill('0') << ltm->tm_mday // Jour (1-31) + << "_" + << std::setw(2) << std::setfill('0') << ltm->tm_hour // Heures (0-23) + << std::setw(2) << std::setfill('0') << ltm->tm_min // Minutes (0-59) + << std::setw(2) << std::setfill('0') << ltm->tm_sec // Secondes (0-59) + << "] "; +} diff --git a/cpp00/ex02/Account.hpp b/cpp00/ex02/Account.hpp new file mode 100644 index 0000000..cd31e06 --- /dev/null +++ b/cpp00/ex02/Account.hpp @@ -0,0 +1,69 @@ +// ************************************************************************** // +// // +// Account.hpp for GlobalBanksters United // +// Created on : Thu Nov 20 19:43:15 1989 // +// Last update : Wed Jan 04 14:54:06 1992 // +// Made by : Brad "Buddy" McLane // +// // +// ************************************************************************** // + + +#pragma once +#ifndef __ACCOUNT_H__ +#define __ACCOUNT_H__ + +// ************************************************************************** // +// Account Class // +// ************************************************************************** // + +class Account { + + +public: + + typedef Account t; + + static int getNbAccounts( void ); + static int getTotalAmount( void ); + static int getNbDeposits( void ); + static int getNbWithdrawals( void ); + static void displayAccountsInfos( void ); + + Account( int initial_deposit ); + ~Account( void ); + + void makeDeposit( int deposit ); + bool makeWithdrawal( int withdrawal ); + int checkAmount( void ) const; + void displayStatus( void ) const; + + +private: + + static int _nbAccounts; + static int _totalAmount; + static int _totalNbDeposits; + static int _totalNbWithdrawals; + + static void _displayTimestamp( void ); + + int _accountIndex; + int _amount; + int _nbDeposits; + int _nbWithdrawals; + + Account( void ); + +}; + + + +// ************************************************************************** // +// vim: set ts=4 sw=4 tw=80 noexpandtab: // +// -*- indent-tabs-mode:t; -*- +// -*- mode: c++-mode; -*- +// -*- fill-column: 75; comment-column: 75; -*- +// ************************************************************************** // + + +#endif /* __ACCOUNT_H__ */ diff --git a/cpp00/ex02/Account.o b/cpp00/ex02/Account.o new file mode 100644 index 0000000..dd9e3aa Binary files /dev/null and b/cpp00/ex02/Account.o differ diff --git a/cpp00/ex02/Makefile b/cpp00/ex02/Makefile new file mode 100644 index 0000000..c6e81da --- /dev/null +++ b/cpp00/ex02/Makefile @@ -0,0 +1,50 @@ +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = bank + +SRC = Account.cpp \ + tests.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation Account Bank... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./bank"$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp00/ex02/bank b/cpp00/ex02/bank new file mode 100755 index 0000000..1696dd3 Binary files /dev/null and b/cpp00/ex02/bank differ diff --git a/cpp00/ex02/tests.cpp b/cpp00/ex02/tests.cpp new file mode 100644 index 0000000..0999525 --- /dev/null +++ b/cpp00/ex02/tests.cpp @@ -0,0 +1,72 @@ +// ************************************************************************** // +// // +// tests.cpp for GlobalBanksters United // +// Created on : Thu Nov 20 23:45:02 1989 // +// Last update : Wed Jan 04 09:23:52 1992 // +// Made by : Brad "Buddy" McLane // +// // +// ************************************************************************** // + +#include +#include +#include +#include "Account.hpp" + + +int main( void ) { + + typedef std::vector accounts_t; + typedef std::vector ints_t; + typedef std::pair acc_int_t; + + int const amounts[] = { 42, 54, 957, 432, 1234, 0, 754, 16576 }; + size_t const amounts_size( sizeof(amounts) / sizeof(int) ); + accounts_t accounts( amounts, amounts + amounts_size ); + accounts_t::iterator acc_begin = accounts.begin(); + accounts_t::iterator acc_end = accounts.end(); + + int const d[] = { 5, 765, 564, 2, 87, 23, 9, 20 }; + size_t const d_size( sizeof(d) / sizeof(int) ); + ints_t deposits( d, d + d_size ); + ints_t::iterator dep_begin = deposits.begin(); + ints_t::iterator dep_end = deposits.end(); + + int const w[] = { 321, 34, 657, 4, 76, 275, 657, 7654 }; + size_t const w_size( sizeof(w) / sizeof(int) ); + ints_t withdrawals( w, w + w_size ); + ints_t::iterator wit_begin = withdrawals.begin(); + ints_t::iterator wit_end = withdrawals.end(); + + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + + for ( acc_int_t it( acc_begin, dep_begin ); + it.first != acc_end && it.second != dep_end; + ++(it.first), ++(it.second) ) { + + (*(it.first)).makeDeposit( *(it.second) ); + } + + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + + for ( acc_int_t it( acc_begin, wit_begin ); + it.first != acc_end && it.second != wit_end; + ++(it.first), ++(it.second) ) { + + (*(it.first)).makeWithdrawal( *(it.second) ); + } + + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + + return 0; +} + + +// ************************************************************************** // +// vim: set ts=4 sw=4 tw=80 noexpandtab: // +// -*- indent-tabs-mode:t; -*- +// -*- mode: c++-mode; -*- +// -*- fill-column: 75; comment-column: 75; -*- +// ************************************************************************** // diff --git a/cpp00/ex02/tests.o b/cpp00/ex02/tests.o new file mode 100644 index 0000000..cc5d644 Binary files /dev/null and b/cpp00/ex02/tests.o differ diff --git a/cpp01/ex00/.vscode/c_cpp_properties.json b/cpp01/ex00/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp01/ex00/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp01/ex00/.vscode/launch.json b/cpp01/ex00/.vscode/launch.json new file mode 100644 index 0000000..15f131d --- /dev/null +++ b/cpp01/ex00/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex00", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex00/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp01/ex00/.vscode/settings.json b/cpp01/ex00/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp01/ex00/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp01/ex00/Makefile b/cpp01/ex00/Makefile new file mode 100644 index 0000000..9f1b9e3 --- /dev/null +++ b/cpp01/ex00/Makefile @@ -0,0 +1,64 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/24 13:55:45 by fgras-ca #+# #+# # +# Updated: 2023/12/24 13:59:56 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = zombie + +SRC = newZombie.cpp \ + main.cpp \ + randomChump.cpp \ + Zombie.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation zombie... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./zombie" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp01/ex00/Zombie.cpp b/cpp01/ex00/Zombie.cpp new file mode 100644 index 0000000..1d2e41a --- /dev/null +++ b/cpp01/ex00/Zombie.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Zombie.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 13:31:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 14:06:52 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Zombie.hpp" + +Zombie::Zombie(void) +{ + std::cout << "\033[32m"; + std::cout << "Constructor called" << std::endl; + std::cout << "\033[0m"; +} + +Zombie::~Zombie(void) +{ + std::cout << "\033[31m"; + std::cout << "Destructor called" << std::endl; + std::cout << "\033[0m"; +} + +void Zombie::announce(void) +{ + std::cout << this->name << ": BraiiiiiiinnnZ..." << std::endl; +} + +void Zombie::setname(std::string name) +{ + this->name = name; +} \ No newline at end of file diff --git a/cpp01/ex00/Zombie.hpp b/cpp01/ex00/Zombie.hpp new file mode 100644 index 0000000..6ca7012 --- /dev/null +++ b/cpp01/ex00/Zombie.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Zombie.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 13:23:44 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 13:30:20 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ZOMBIE_HPP +# define ZOMBIE_HPP + +#include + +class Zombie +{ + public: + Zombie(void); + ~Zombie(void); + void announce(void); + void setname(std::string name); + + private: + std::string name; +}; + +Zombie* newZombie(std::string name); +void randomChump(std::string name); + +#endif diff --git a/cpp01/ex00/Zombie.o b/cpp01/ex00/Zombie.o new file mode 100644 index 0000000..2a5513b Binary files /dev/null and b/cpp01/ex00/Zombie.o differ diff --git a/cpp01/ex00/main.cpp b/cpp01/ex00/main.cpp new file mode 100644 index 0000000..3c8d144 --- /dev/null +++ b/cpp01/ex00/main.cpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 13:52:26 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 14:04:53 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Zombie.hpp" + +int main() +{ + Zombie *heapZombie = newZombie("HeapZombie"); + heapZombie->announce(); + delete heapZombie; + std::cout << "\033[31m"; + std::cout << "HeapZombie Deleted!!" << std::endl << std::endl; + std::cout << "\033[0m"; + randomChump("StackZombie"); + + return (0); +} \ No newline at end of file diff --git a/cpp01/ex00/main.o b/cpp01/ex00/main.o new file mode 100644 index 0000000..0f0b8ea Binary files /dev/null and b/cpp01/ex00/main.o differ diff --git a/cpp01/ex00/newZombie.cpp b/cpp01/ex00/newZombie.cpp new file mode 100644 index 0000000..6a1808f --- /dev/null +++ b/cpp01/ex00/newZombie.cpp @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* newZombie.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 13:38:34 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 13:46:55 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Zombie.hpp" + +Zombie* newZombie(std::string name) +{ + Zombie *z = new Zombie(); + z->setname(name); + return (z); +} \ No newline at end of file diff --git a/cpp01/ex00/newZombie.o b/cpp01/ex00/newZombie.o new file mode 100644 index 0000000..b42e90f Binary files /dev/null and b/cpp01/ex00/newZombie.o differ diff --git a/cpp01/ex00/randomChump.cpp b/cpp01/ex00/randomChump.cpp new file mode 100644 index 0000000..43ab0dc --- /dev/null +++ b/cpp01/ex00/randomChump.cpp @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* randomChump.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 13:23:25 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 13:50:16 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Zombie.hpp" + +void randomChump(std::string name) +{ + Zombie z; + z.setname(name); + z.announce(); +} \ No newline at end of file diff --git a/cpp01/ex00/randomChump.o b/cpp01/ex00/randomChump.o new file mode 100644 index 0000000..adddc6d Binary files /dev/null and b/cpp01/ex00/randomChump.o differ diff --git a/cpp01/ex00/zombie b/cpp01/ex00/zombie new file mode 100755 index 0000000..f8acbfb Binary files /dev/null and b/cpp01/ex00/zombie differ diff --git a/cpp01/ex01/.vscode/c_cpp_properties.json b/cpp01/ex01/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp01/ex01/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp01/ex01/.vscode/launch.json b/cpp01/ex01/.vscode/launch.json new file mode 100644 index 0000000..d18b655 --- /dev/null +++ b/cpp01/ex01/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex01", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex01/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp01/ex01/.vscode/settings.json b/cpp01/ex01/.vscode/settings.json new file mode 100644 index 0000000..ebd8700 --- /dev/null +++ b/cpp01/ex01/.vscode/settings.json @@ -0,0 +1,105 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp01/ex01/Makefile b/cpp01/ex01/Makefile new file mode 100644 index 0000000..b3a4a71 --- /dev/null +++ b/cpp01/ex01/Makefile @@ -0,0 +1,63 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/24 13:55:45 by fgras-ca #+# #+# # +# Updated: 2023/12/24 15:34:53 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = horde + +SRC = zombieHorde.cpp \ + main.cpp \ + Zombie.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation horde... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./horde" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/cpp01/ex01/Zombie.cpp b/cpp01/ex01/Zombie.cpp new file mode 100644 index 0000000..5c7dc9f --- /dev/null +++ b/cpp01/ex01/Zombie.cpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Zombie.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 13:31:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 15:30:35 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Zombie.hpp" + +int Zombie::count = 0; + +Zombie::Zombie(void) : id(++count) +{ + std::cout << "\033[32m"; + std::cout << "Constructor called for zombie [" << id << "]" << std::endl; + std::cout << "\033[0m"; +} + +Zombie::~Zombie(void) +{ + std::cout << "\033[31m"; + std::cout << "Destructor called for zombie [" << id << "]" << std::endl; + std::cout << "\033[0m"; +} + +void Zombie::announce(void) +{ + std::cout << this->name << ": BraiiiiiiinnnZ..." << std::endl; +} + +void Zombie::setname(std::string name) +{ + this->name = name; +} \ No newline at end of file diff --git a/cpp01/ex01/Zombie.hpp b/cpp01/ex01/Zombie.hpp new file mode 100644 index 0000000..fdc388f --- /dev/null +++ b/cpp01/ex01/Zombie.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Zombie.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 13:23:44 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 15:24:50 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ZOMBIE_HPP +# define ZOMBIE_HPP + +#include +#include + +class Zombie +{ + public: + Zombie(void); + ~Zombie(void); + void announce(void); + void setname(std::string name); + + private: + std::string name; + int id; + static int count; +}; + +Zombie* zombieHorde(int N, std::string name); + +#endif \ No newline at end of file diff --git a/cpp01/ex01/Zombie.o b/cpp01/ex01/Zombie.o new file mode 100644 index 0000000..e521af5 Binary files /dev/null and b/cpp01/ex01/Zombie.o differ diff --git a/cpp01/ex01/horde b/cpp01/ex01/horde new file mode 100755 index 0000000..abccd4b Binary files /dev/null and b/cpp01/ex01/horde differ diff --git a/cpp01/ex01/main.cpp b/cpp01/ex01/main.cpp new file mode 100644 index 0000000..c091f84 --- /dev/null +++ b/cpp01/ex01/main.cpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 15:17:37 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 15:32:08 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Zombie.hpp" + +int main() +{ + int N; + std::cout << "\033[32m"; + std::cout << "Entrez le nombre de zombies à créer: "; + std::cout << "\033[0m"; + std::cin >> N; + if(std::cin.fail() || N < 1) + { + std::cerr << "\033[31m"; + std::cerr << "Entrée invalide. Veuillez entrer un nombre entier positif!!" << std::endl; + std::cerr << "\033[0m"; + return (1); + } + Zombie* horde = zombieHorde(N, "Zombie"); + for (int i = 0; i < N; i++) + { + horde[i].announce(); + } + delete[] horde; + return (0); +} \ No newline at end of file diff --git a/cpp01/ex01/main.o b/cpp01/ex01/main.o new file mode 100644 index 0000000..4113626 Binary files /dev/null and b/cpp01/ex01/main.o differ diff --git a/cpp01/ex01/zombieHorde.cpp b/cpp01/ex01/zombieHorde.cpp new file mode 100644 index 0000000..270f78b --- /dev/null +++ b/cpp01/ex01/zombieHorde.cpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* zombieHorde.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 15:05:45 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 15:17:10 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Zombie.hpp" + +Zombie* zombieHorde(int N, std::string name) +{ + Zombie *horde = new Zombie[N]; + + for(int i = 0; i < N; i++) + horde[i].setname(name); + + return (horde); +} \ No newline at end of file diff --git a/cpp01/ex01/zombieHorde.o b/cpp01/ex01/zombieHorde.o new file mode 100644 index 0000000..d0b75e4 Binary files /dev/null and b/cpp01/ex01/zombieHorde.o differ diff --git a/cpp01/ex02/.vscode/c_cpp_properties.json b/cpp01/ex02/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp01/ex02/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp01/ex02/.vscode/launch.json b/cpp01/ex02/.vscode/launch.json new file mode 100644 index 0000000..5b63206 --- /dev/null +++ b/cpp01/ex02/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex02", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex02/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp01/ex02/.vscode/settings.json b/cpp01/ex02/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp01/ex02/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp01/ex02/Makefile b/cpp01/ex02/Makefile new file mode 100644 index 0000000..ce12042 --- /dev/null +++ b/cpp01/ex02/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/24 13:55:45 by fgras-ca #+# #+# # +# Updated: 2023/12/24 15:52:26 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = brain + +SRC = main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation brain... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./brain" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/cpp01/ex02/brain b/cpp01/ex02/brain new file mode 100755 index 0000000..5d39098 Binary files /dev/null and b/cpp01/ex02/brain differ diff --git a/cpp01/ex02/main.cpp b/cpp01/ex02/main.cpp new file mode 100644 index 0000000..e865fb3 --- /dev/null +++ b/cpp01/ex02/main.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 15:41:21 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 15:50:11 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +int main() +{ + std::string str = "HI THIS IS BRAIN"; + std::string *stringPTR = &str; + std::string &stringREF = str; + + std::cout << "Adresse de str : " << &str << std::endl; + std::cout << "Adresse contenue dans stringPTR : " << stringPTR << std::endl; + std::cout << "Adresse contenue dans stringREF : " << &stringREF << std::endl << std::endl; + + std::cout << "Valeur de str : " << str << std::endl; + std::cout << "Valeur pointée par stringPTR : " << *stringPTR << std::endl; + std::cout << "Valeur pointée par stringREF : " << stringREF << std::endl; + + return (0); +} \ No newline at end of file diff --git a/cpp01/ex02/main.o b/cpp01/ex02/main.o new file mode 100644 index 0000000..adfd723 Binary files /dev/null and b/cpp01/ex02/main.o differ diff --git a/cpp01/ex03/.vscode/c_cpp_properties.json b/cpp01/ex03/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp01/ex03/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp01/ex03/.vscode/launch.json b/cpp01/ex03/.vscode/launch.json new file mode 100644 index 0000000..733ff17 --- /dev/null +++ b/cpp01/ex03/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex03", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex03/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp01/ex03/.vscode/settings.json b/cpp01/ex03/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp01/ex03/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp01/ex03/HumanA.cpp b/cpp01/ex03/HumanA.cpp new file mode 100644 index 0000000..6cbaf5c --- /dev/null +++ b/cpp01/ex03/HumanA.cpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HumanA.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 17:25:06 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 17:47:18 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "HumanA.hpp" + +HumanA::HumanA(std::string name, Weapon &weapon) : _name(name), _weapon(weapon) {} + +void HumanA::attack(void) +{ + const std::string type = this->_weapon.getType(); + std::cout << this->_name << " attack with their " << type << std::endl; +} + +HumanA::~HumanA(void) {} \ No newline at end of file diff --git a/cpp01/ex03/HumanA.hpp b/cpp01/ex03/HumanA.hpp new file mode 100644 index 0000000..309e80a --- /dev/null +++ b/cpp01/ex03/HumanA.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HumanA.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 16:13:39 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 18:01:21 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HUMANA_HPP +# define HUMANA_HPP + +#include "Weapon.hpp" + +class HumanA +{ + public: + HumanA(std::string name, Weapon &weapon); + ~HumanA(void); + void attack(void); + + private: + std::string _name; + Weapon &_weapon; +}; + +#endif \ No newline at end of file diff --git a/cpp01/ex03/HumanA.o b/cpp01/ex03/HumanA.o new file mode 100644 index 0000000..caf9a56 Binary files /dev/null and b/cpp01/ex03/HumanA.o differ diff --git a/cpp01/ex03/HumanB.cpp b/cpp01/ex03/HumanB.cpp new file mode 100644 index 0000000..9fa0304 --- /dev/null +++ b/cpp01/ex03/HumanB.cpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HumanB.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 17:48:26 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 17:54:54 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "HumanB.hpp" + +HumanB::HumanB(std::string name) : _name(name) {} + +void HumanB::attack(void) +{ + const std::string type = this->_weapon->getType(); + std::cout << this->_name << " attacks with their " << type << std::endl; +} + +void HumanB::setWeapon(Weapon &weapon) +{ + this->_weapon = &weapon; +} + +HumanB::~HumanB(void) {} \ No newline at end of file diff --git a/cpp01/ex03/HumanB.hpp b/cpp01/ex03/HumanB.hpp new file mode 100644 index 0000000..a458a09 --- /dev/null +++ b/cpp01/ex03/HumanB.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HumanB.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 16:18:38 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 17:58:53 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HUMANB_HPP +# define HUMANB_HPP + +#include "Weapon.hpp" + +class HumanB +{ + public: + HumanB(std::string name); + ~HumanB(void); + void attack(void); + void setWeapon(Weapon &weapon); + + private: + std::string _name; + Weapon *_weapon; +}; + +#endif \ No newline at end of file diff --git a/cpp01/ex03/HumanB.o b/cpp01/ex03/HumanB.o new file mode 100644 index 0000000..c0f2132 Binary files /dev/null and b/cpp01/ex03/HumanB.o differ diff --git a/cpp01/ex03/Makefile b/cpp01/ex03/Makefile new file mode 100644 index 0000000..7584445 --- /dev/null +++ b/cpp01/ex03/Makefile @@ -0,0 +1,64 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/24 13:55:45 by fgras-ca #+# #+# # +# Updated: 2023/12/24 18:04:10 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = weapon + +SRC = Weapon.cpp \ + HumanB.cpp \ + HumanA.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation weapon... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./weapon" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/cpp01/ex03/Weapon.cpp b/cpp01/ex03/Weapon.cpp new file mode 100644 index 0000000..53323e5 --- /dev/null +++ b/cpp01/ex03/Weapon.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Weapon.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 17:16:10 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 17:24:34 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Weapon.hpp" + +const std::string &Weapon::getType(void) +{ + const std::string &type = this->_type; + return (type); +} + +void Weapon::setType(std::string type) +{ + this->_type = type; +} + +Weapon::Weapon(std::string type) +{ + this->_type = type; +} + +Weapon::~Weapon(void) {} \ No newline at end of file diff --git a/cpp01/ex03/Weapon.hpp b/cpp01/ex03/Weapon.hpp new file mode 100644 index 0000000..78b1e4f --- /dev/null +++ b/cpp01/ex03/Weapon.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Weapon.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 16:02:52 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 18:03:55 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WEAPON_HPP +# define WEAPON_HPP + +#include +#include + +class Weapon +{ + public: + Weapon(std::string type); + ~Weapon(void); + const std::string &getType(void); + void setType(std::string type); + + private: + std::string _type; +}; + +#endif diff --git a/cpp01/ex03/Weapon.o b/cpp01/ex03/Weapon.o new file mode 100644 index 0000000..7d70879 Binary files /dev/null and b/cpp01/ex03/Weapon.o differ diff --git a/cpp01/ex03/main.cpp b/cpp01/ex03/main.cpp new file mode 100644 index 0000000..1127f9e --- /dev/null +++ b/cpp01/ex03/main.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 15:58:41 by fgras-ca #+# #+# */ +/* Updated: 2023/12/24 18:05:04 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Weapon.hpp" +#include "HumanA.hpp" +#include "HumanB.hpp" + +int main() +{ + { + Weapon club = Weapon("crude spiked club"); + HumanA bob("Bob", club); + bob.attack(); + club.setType("some other type of club"); + bob.attack(); + } + { + Weapon club = Weapon("crude spiked club"); + HumanB jim("Jim"); + jim.setWeapon(club); + jim.attack(); + club.setType("some other type of club"); + jim.attack(); + } + return (0); +} diff --git a/cpp01/ex03/main.o b/cpp01/ex03/main.o new file mode 100644 index 0000000..49a8676 Binary files /dev/null and b/cpp01/ex03/main.o differ diff --git a/cpp01/ex03/weapon b/cpp01/ex03/weapon new file mode 100755 index 0000000..fbdd571 Binary files /dev/null and b/cpp01/ex03/weapon differ diff --git a/cpp01/ex04/.vscode/c_cpp_properties.json b/cpp01/ex04/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp01/ex04/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp01/ex04/.vscode/launch.json b/cpp01/ex04/.vscode/launch.json new file mode 100644 index 0000000..f5ddda4 --- /dev/null +++ b/cpp01/ex04/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex04", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex04/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp01/ex04/.vscode/settings.json b/cpp01/ex04/.vscode/settings.json new file mode 100644 index 0000000..d355f64 --- /dev/null +++ b/cpp01/ex04/.vscode/settings.json @@ -0,0 +1,107 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "iostream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp01/ex04/Makefile b/cpp01/ex04/Makefile new file mode 100644 index 0000000..19eae88 --- /dev/null +++ b/cpp01/ex04/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/24 13:55:45 by fgras-ca #+# #+# # +# Updated: 2023/12/27 14:52:31 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = replace + +SRC = main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation replace... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./replace" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/cpp01/ex04/main.cpp b/cpp01/ex04/main.cpp new file mode 100644 index 0000000..036af92 --- /dev/null +++ b/cpp01/ex04/main.cpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 14:20:39 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 14:55:50 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +int main(int argc, char **argv) +{ + if (argc != 4) + { + std::cerr << "\033[32m"; + std::cerr << "Usage: " << argv[0] << " " << std::endl; + std::cerr << "\033[0m"; + return (1); + } + + std::string filename = argv[1]; + std::string s1 = argv[2]; + std::string s2 = argv[3]; + + std::ifstream inputFile(filename); + if (!inputFile.is_open()) + { + std::cerr << "\031[33m"; + std::cerr << "Impossible d'ouvrir le fichier " << filename << std::endl; + std::cerr << "\033[0m"; + return (1); + } + + std::stringstream buffer; + buffer << inputFile.rdbuf(); + std::string content = buffer.str(); + inputFile.close(); + + size_t pos = 0; + while ((pos = content.find(s1, pos)) != std::string::npos) + { + content.replace(pos, s1.length(), s2); + pos += s2.length(); + } + + std::ofstream outputFile(filename + ".replace"); + if (!outputFile.is_open()) + { + std::cerr << "\033[33m"; + std::cerr << "Impossible de céer le fichier " << filename + ".replace" << std::endl; + std::cerr << "\033[30m"; + return (1); + } + + outputFile << content; + outputFile.close(); + + return (0); +} \ No newline at end of file diff --git a/cpp01/ex04/main.o b/cpp01/ex04/main.o new file mode 100644 index 0000000..1e2252e Binary files /dev/null and b/cpp01/ex04/main.o differ diff --git a/cpp01/ex04/replace b/cpp01/ex04/replace new file mode 100755 index 0000000..3c8c812 Binary files /dev/null and b/cpp01/ex04/replace differ diff --git a/cpp01/ex04/test b/cpp01/ex04/test new file mode 100644 index 0000000..1392c20 --- /dev/null +++ b/cpp01/ex04/test @@ -0,0 +1,4 @@ +Bonjour les amis. +Les bons comptes font les bon amis +Les meilleurs amis sont ceux qui excitent à bien faire. +Avoir des amis, c'est être riche. \ No newline at end of file diff --git a/cpp01/ex04/test.replace b/cpp01/ex04/test.replace new file mode 100644 index 0000000..c574195 --- /dev/null +++ b/cpp01/ex04/test.replace @@ -0,0 +1,4 @@ +Bonjour les poulets. +Les bons comptes font les bon poulets +Les meilleurs poulets sont ceux qui excitent à bien faire. +Avoir des poulets, c'est être riche. \ No newline at end of file diff --git a/cpp01/ex04/test.txt.replace b/cpp01/ex04/test.txt.replace new file mode 100644 index 0000000..c574195 --- /dev/null +++ b/cpp01/ex04/test.txt.replace @@ -0,0 +1,4 @@ +Bonjour les poulets. +Les bons comptes font les bon poulets +Les meilleurs poulets sont ceux qui excitent à bien faire. +Avoir des poulets, c'est être riche. \ No newline at end of file diff --git a/cpp01/ex05/.vscode/c_cpp_properties.json b/cpp01/ex05/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp01/ex05/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp01/ex05/.vscode/launch.json b/cpp01/ex05/.vscode/launch.json new file mode 100644 index 0000000..94c96ae --- /dev/null +++ b/cpp01/ex05/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex05", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex05/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp01/ex05/.vscode/settings.json b/cpp01/ex05/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp01/ex05/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp01/ex05/Harl.cpp b/cpp01/ex05/Harl.cpp new file mode 100644 index 0000000..a79fde7 --- /dev/null +++ b/cpp01/ex05/Harl.cpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 15:12:29 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 16:41:15 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Harl.hpp" + +Harl::Harl() {} + +void Harl::debug(void) +{ + std::cout << "[DEBUG]\nI love having extra bacon for my 7XL-double-cheese-triple-pickle-specialketchup burger.\nI really do!" << std::endl; +} + +void Harl::info(void) +{ + std::cout << "[INFO]\nI cannot believe adding extra bacon costs more money. You didn’t put enough bacon in my burger!\nIf you did, I wouldn’t be asking for more!" << std::endl; +} + +void Harl::warning(void) +{ + std::cout << "[WARNING]\nI think I deserve to have some extra bacon for free.\nI’ve been coming for years whereas you started working here since last month." << std::endl; +} + +void Harl::error(void) +{ + std::cout << "[ERROR]\nThis is unacceptable! I want to speak to the manager now." << std::endl; +} + +void Harl::complain(std::string level) +{ + void (Harl::*complaints[])(void) = {&Harl::debug, &Harl::info, &Harl::warning, &Harl::error}; + std::string levels[] = {"DEBUG", "INFO", "WARNING", "ERROR"}; + + for (int i = 0; i < 4; i++) + { + if (level == levels[i]) + { + (this->*complaints[i])(); + break; + } + } +} + +Harl::~Harl() {} diff --git a/cpp01/ex05/Harl.hpp b/cpp01/ex05/Harl.hpp new file mode 100644 index 0000000..bb76b64 --- /dev/null +++ b/cpp01/ex05/Harl.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 15:08:34 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 15:11:58 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HARL_HPP +# define HARL_HPP + +#include +#include + +class Harl +{ + public: + Harl(); + ~Harl(); + void complain(std::string level); + + private: + void debug(void); + void info(void); + void warning(void); + void error(void); +}; + +#endif \ No newline at end of file diff --git a/cpp01/ex05/Harl.o b/cpp01/ex05/Harl.o new file mode 100644 index 0000000..b0ffc35 Binary files /dev/null and b/cpp01/ex05/Harl.o differ diff --git a/cpp01/ex05/Makefile b/cpp01/ex05/Makefile new file mode 100644 index 0000000..a4d8f22 --- /dev/null +++ b/cpp01/ex05/Makefile @@ -0,0 +1,62 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/24 13:55:45 by fgras-ca #+# #+# # +# Updated: 2023/12/27 15:39:55 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = harl + +SRC = Harl.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation harl... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./harl" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/cpp01/ex05/harl b/cpp01/ex05/harl new file mode 100755 index 0000000..50c8e02 Binary files /dev/null and b/cpp01/ex05/harl differ diff --git a/cpp01/ex05/main.cpp b/cpp01/ex05/main.cpp new file mode 100644 index 0000000..720d1b2 --- /dev/null +++ b/cpp01/ex05/main.cpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 15:34:03 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 15:38:28 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Harl.hpp" + +int main() +{ + Harl harl; + + std::cout << "Calling with DEBUG:" << std::endl; + harl.complain("DEBUG"); + + std::cout << "\nCalling with INFO:" << std::endl; + harl.complain("INFO"); + + std::cout << "\nCalling with WARNING:" << std::endl; + harl.complain("WARNING"); + + std::cout << "\nCalling with ERROR:" << std::endl; + harl.complain("ERROR"); + + return (0); +} \ No newline at end of file diff --git a/cpp01/ex05/main.o b/cpp01/ex05/main.o new file mode 100644 index 0000000..6e04088 Binary files /dev/null and b/cpp01/ex05/main.o differ diff --git a/cpp01/ex06/.vscode/c_cpp_properties.json b/cpp01/ex06/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp01/ex06/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp01/ex06/.vscode/launch.json b/cpp01/ex06/.vscode/launch.json new file mode 100644 index 0000000..4181885 --- /dev/null +++ b/cpp01/ex06/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex06", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp01/ex06/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp01/ex06/.vscode/settings.json b/cpp01/ex06/.vscode/settings.json new file mode 100644 index 0000000..ebd8700 --- /dev/null +++ b/cpp01/ex06/.vscode/settings.json @@ -0,0 +1,105 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp01/ex06/Harl.cpp b/cpp01/ex06/Harl.cpp new file mode 100644 index 0000000..c7b4da6 --- /dev/null +++ b/cpp01/ex06/Harl.cpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 15:12:29 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 16:35:25 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Harl.hpp" + +Harl::Harl() {} + +void Harl::debug(void) +{ + std::cout << "[DEBUG]\nI love having extra bacon for my 7XL-double-cheese-triple-pickle-specialketchup burger.\nI really do!" << std::endl; +} + +void Harl::info(void) +{ + std::cout << "[INFO]\n 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!" << std::endl; +} + +void Harl::warning(void) +{ + std::cout << "[WARNING]\n 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." << std::endl; +} + +void Harl::error(void) +{ + std::cout << "[ERROR]\nThis is unacceptable! I want to speak to the manager now." << std::endl; +} + +void Harl::complain(std::string level) +{ + void (Harl::*complaints[])(void) = {&Harl::debug, &Harl::info, &Harl::warning, &Harl::error}; + std::string levels[4] = {"DEBUG", "INFO", "WARNING", "ERROR"}; + + int i = 0; + while (levels[i] != level and i < 4) + i++; + + switch (i) + { + case 0: + (this->*complaints[0])(); + case 1: + (this->*complaints[1])(); + case 2: + (this->*complaints[2])(); + case 3: + { + (this->*complaints[3])(); + break; + } + default: + std::cout << "[ Probably complaining about insignifiant problems ]" << std::endl; + break; + } +} + +Harl::~Harl() {} diff --git a/cpp01/ex06/Harl.hpp b/cpp01/ex06/Harl.hpp new file mode 100644 index 0000000..bb76b64 --- /dev/null +++ b/cpp01/ex06/Harl.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 15:08:34 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 15:11:58 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HARL_HPP +# define HARL_HPP + +#include +#include + +class Harl +{ + public: + Harl(); + ~Harl(); + void complain(std::string level); + + private: + void debug(void); + void info(void); + void warning(void); + void error(void); +}; + +#endif \ No newline at end of file diff --git a/cpp01/ex06/Harl.o b/cpp01/ex06/Harl.o new file mode 100644 index 0000000..421da45 Binary files /dev/null and b/cpp01/ex06/Harl.o differ diff --git a/cpp01/ex06/Makefile b/cpp01/ex06/Makefile new file mode 100644 index 0000000..0126eb8 --- /dev/null +++ b/cpp01/ex06/Makefile @@ -0,0 +1,62 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 16:19:19 by fgras-ca #+# #+# # +# Updated: 2023/12/27 16:20:59 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = harlFilter + +SRC = Harl.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation harlFilter... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./harlFilter" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp01/ex06/harlFilter b/cpp01/ex06/harlFilter new file mode 100755 index 0000000..6d2b277 Binary files /dev/null and b/cpp01/ex06/harlFilter differ diff --git a/cpp01/ex06/main.cpp b/cpp01/ex06/main.cpp new file mode 100644 index 0000000..0838515 --- /dev/null +++ b/cpp01/ex06/main.cpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 16:05:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 16:08:43 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Harl.hpp" + +int main(int argc, char **argv) +{ + Harl harl; + + if (argc != 2) + { + std::cerr << "Usage: ./harlFilter " << std::endl; + return (1); + } + + std::string level = argv[1]; + harl.complain(level); + + return (0); +} \ No newline at end of file diff --git a/cpp01/ex06/main.o b/cpp01/ex06/main.o new file mode 100644 index 0000000..c91be4c Binary files /dev/null and b/cpp01/ex06/main.o differ diff --git a/cpp02/ex00/.vscode/c_cpp_properties.json b/cpp02/ex00/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp02/ex00/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp02/ex00/.vscode/launch.json b/cpp02/ex00/.vscode/launch.json new file mode 100644 index 0000000..5a586e1 --- /dev/null +++ b/cpp02/ex00/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex00", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex00/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp02/ex00/.vscode/settings.json b/cpp02/ex00/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp02/ex00/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp02/ex00/Fixed.cpp b/cpp02/ex00/Fixed.cpp new file mode 100644 index 0000000..211e1cc --- /dev/null +++ b/cpp02/ex00/Fixed.cpp @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 19:02:33 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 19:23:02 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +Fixed::Fixed() : value(0) +{ + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(const Fixed &other) +{ + std::cout << "Copy contructor called" << std::endl; + *this = other; +} + +Fixed &Fixed::operator=(const Fixed &other) +{ + std::cout << "Copy assignment called" << std::endl; + if (this != &other) + { + this->value = other.getRawBits(); + } + return (*this); +} + +Fixed::~Fixed() +{ + std::cout << "Destructor called" << std::endl; +} + +int Fixed::getRawBits(void) const +{ + std::cout << "getRawBits member function called" << std::endl; + return (this->value); +} + +void Fixed::setRawBits(int const raw) +{ + this->value = raw; +} \ No newline at end of file diff --git a/cpp02/ex00/Fixed.hpp b/cpp02/ex00/Fixed.hpp new file mode 100644 index 0000000..d63141a --- /dev/null +++ b/cpp02/ex00/Fixed.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 18:53:10 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 19:02:13 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_HPP +# define FIXED_HPP + +#include + +class Fixed +{ + public: + Fixed(); // Constructeur par défaut + Fixed(const Fixed &other); // Constructeur de recopie + Fixed & operator=(const Fixed &other); // Opérateur d'affectation + ~Fixed(); // Destructeur + + + int getRawBits(void) const; // Retourne la valeur brute + void setRawBits(int const raw); // Initialise la valeur brute + + private: + int value; // Valeur du nombre à virgule fixe + static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire +}; + +#endif \ No newline at end of file diff --git a/cpp02/ex00/Makefile b/cpp02/ex00/Makefile new file mode 100644 index 0000000..4714ffb --- /dev/null +++ b/cpp02/ex00/Makefile @@ -0,0 +1,62 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 19:17:28 by fgras-ca #+# #+# # +# Updated: 2023/12/27 19:18:51 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = fixed + +SRC = Fixed.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./fixed" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp02/ex00/main.cpp b/cpp02/ex00/main.cpp new file mode 100644 index 0000000..edff3f4 --- /dev/null +++ b/cpp02/ex00/main.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 19:12:29 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 19:21:28 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +int main() +{ + std::cout << "Création de l'objet a avec le constructeur par défaut." << std::endl; + Fixed a; + std::cout << "Valeur de a après création : " << a.getRawBits() << std::endl; + + std::cout << "\nCréation de l'objet b par copie de l'objet a." << std::endl; + Fixed b(a); + std::cout << "Valeur de b après la copie : " << b.getRawBits() << std::endl; + + std::cout << "\nAssignation de l'objet c à l'objet b." << std::endl; + Fixed c; + c = b; + std::cout << "Valeur de c après l'assignation : " << c.getRawBits() << std::endl << std::endl; + + return (0); +} diff --git a/cpp02/ex01/.vscode/c_cpp_properties.json b/cpp02/ex01/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp02/ex01/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp02/ex01/.vscode/launch.json b/cpp02/ex01/.vscode/launch.json new file mode 100644 index 0000000..5a7ff89 --- /dev/null +++ b/cpp02/ex01/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex01", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex01/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp02/ex01/.vscode/settings.json b/cpp02/ex01/.vscode/settings.json new file mode 100644 index 0000000..ae340aa --- /dev/null +++ b/cpp02/ex01/.vscode/settings.json @@ -0,0 +1,105 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "system_error": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp02/ex01/Fixed.cpp b/cpp02/ex01/Fixed.cpp new file mode 100644 index 0000000..f323fe1 --- /dev/null +++ b/cpp02/ex01/Fixed.cpp @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 20:32:07 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 20:54:35 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define CYAN "\033[36m" + +Fixed::Fixed() : value(0) +{ + std::cout << GREEN << "Default constructor called" << RESET << std::endl; +} + +Fixed::Fixed(const int intValue) : value(intValue << fractionalBits)//Ce décalage vers la gauche revient à multiplier l'entier par 2^fractionalBits, plaçant ainsi la virgule virtuellement après les 8 bits de droite. +{ + std::cout << CYAN << "Int constructor called" << RESET << std::endl; +} + +Fixed::Fixed(const float floatValue) : value(roundf(floatValue * (1 << fractionalBits)))//Vous multipliez le flottant par 2^fractionalBits pour décaler la virgule vers la droite de 8 positions, puis arrondissez le résultat à l'entier le plus proche pour ne pas perdre la précision. +{ + std::cout << CYAN << "Float constructor called" << RESET << std::endl; +} + +Fixed::Fixed(const Fixed &other) : value(other.value) +{ + std::cout << YELLOW << "Copy constructor called" << RESET << std::endl; +} + +Fixed &Fixed::operator=(const Fixed &other) +{ + std::cout << YELLOW << "Copy assignment operator called" << RESET << std::endl; + value = other.value; + return (*this); +} + +Fixed::~Fixed() +{ + std::cout << RED << "Destructor called" << RESET << std::endl; +} + +float Fixed::toFloat(void) const +{ + return ((float)value / (float)(1 << fractionalBits)); //Vous divisez la valeur entière représentant le nombre à virgule fixe par 2^fractionalBits. Cela décale la virgule virtuellement vers la gauche de 8 positions, vous donnant la valeur flottante originale. +} + +int Fixed::toInt(void) const +{ + return (value / (1 << fractionalBits)); // Convertit en entier +} + +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) +{ + stream << fixed.toFloat(); // Affiche en tant que flottant + return (stream); +} diff --git a/cpp02/ex01/Fixed.hpp b/cpp02/ex01/Fixed.hpp new file mode 100644 index 0000000..101a4d7 --- /dev/null +++ b/cpp02/ex01/Fixed.hpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 20:12:09 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 20:28:25 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_H +#define FIXED_H + +#include +#include // Pour roundf + +class Fixed +{ + public: + Fixed(); // Constructeur par défaut + Fixed(const int intValue); // Constructeur avec un entier + Fixed(const float floatValue); // Constructeur avec un flottant + Fixed(const Fixed &other); // Constructeur de recopie + Fixed &operator=(const Fixed &other); // Opérateur d'affectation + ~Fixed(); // Destructeur + + float toFloat(void) const; // Convertit en flottant + int toInt(void) const; // Convertit en entier + + private: + int value; // Valeur du nombre à virgule fixe + static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire +}; + +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); // Surcharge de l'opérateur d’insertion << ésentation en virgule flottante du nombre à virgule fixe dans le flux de sortie (objet output stream) + +#endif diff --git a/cpp02/ex01/Fixed.o b/cpp02/ex01/Fixed.o new file mode 100644 index 0000000..429bc3d Binary files /dev/null and b/cpp02/ex01/Fixed.o differ diff --git a/cpp02/ex01/Makefile b/cpp02/ex01/Makefile new file mode 100644 index 0000000..2bbdcf2 --- /dev/null +++ b/cpp02/ex01/Makefile @@ -0,0 +1,62 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/27 20:58:29 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = fixed + +SRC = Fixed.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./fixed" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp02/ex01/fixed b/cpp02/ex01/fixed new file mode 100755 index 0000000..dc01bc2 Binary files /dev/null and b/cpp02/ex01/fixed differ diff --git a/cpp02/ex01/main.cpp b/cpp02/ex01/main.cpp new file mode 100644 index 0000000..8036183 --- /dev/null +++ b/cpp02/ex01/main.cpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 20:46:29 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 21:06:18 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +int main(void) +{ + Fixed a; + Fixed const b(10); + Fixed const c(42.42f); + Fixed const d(b); + a = Fixed(1234.4321f); + + std::cout << "a is " << a << std::endl; + std::cout << "b is " << b << std::endl; + std::cout << "c is " << c << std::endl; + std::cout << "d is " << d << std::endl; + + std::cout << "a is " << a.toInt() << " as integer" << std::endl; + std::cout << "b is " << b.toInt() << " as integer" << std::endl; + std::cout << "c is " << c.toInt() << " as integer" << std::endl; + std::cout << "d is " << d.toInt() << " as integer" << std::endl; + + return (0); +} + +/*a) a is 1234.43 +a a été initialisé avec la valeur 1234.4321f (un flottant). +Lors de la conversion en virgule fixe, cette valeur est multipliée par 2^8 (256, car 8 bits sont utilisés pour la partie fractionnaire) et arrondie à l'entier le plus proche pour stocker sous forme entière. +Lors de la conversion pour l'affichage (.toFloat() utilisée dans la surcharge de l'opérateur <<), cette valeur entière est divisée par 256 pour la reconvertir en flottant. Le résultat est 1234.43 après l'arrondi pour l'affichage. +b) b is 10 +b a été initialisé avec la valeur entière 10. +Comme c'est un entier sans partie fractionnaire, sa représentation à virgule fixe est juste 10 * 256 = 2560. C'est la valeur stockée en interne. +Lors de la conversion pour l'affichage, 2560 est divisé par 256, ce qui redonne 10. Aucun arrondi n'est nécessaire car c'était une valeur entière pour commencer. +c) c is 42.4219 +c a été initialisé avec la valeur flottante 42.42f. +Lors de la conversion en virgule fixe, cette valeur est multipliée par 256 et arrondie. Cela peut entraîner une petite imprécision due à l'arrondi. +Lors de la conversion pour l'affichage, cette valeur arrondie est divisée par 256, résultant en 42.4219 après l'arrondi pour l'affichage. +d) d is 10 +d est une copie de b, donc il a exactement les mêmes valeurs internes que b. +Ainsi, tout comme b, d affiche 10 lorsqu'il est converti en flottant pour l'affichage. +Conversion en entiers : +Lorsque vous convertissez ces valeurs en entiers (toInt()), la partie fractionnaire est simplement supprimée (en divisant par 256). + +a as integer: 1234 provient de 1234.43 avec la partie fractionnaire supprimée. +b as integer: 10 est déjà un entier, donc pas de changement. +c as integer: 42 provient de 42.4219 avec la partie fractionnaire supprimée. +d as integer: Identique à b, donc 10.*/ \ No newline at end of file diff --git a/cpp02/ex01/main.o b/cpp02/ex01/main.o new file mode 100644 index 0000000..9b32acc Binary files /dev/null and b/cpp02/ex01/main.o differ diff --git a/cpp02/ex02/.vscode/c_cpp_properties.json b/cpp02/ex02/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp02/ex02/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp02/ex02/.vscode/launch.json b/cpp02/ex02/.vscode/launch.json new file mode 100644 index 0000000..dbc7b0f --- /dev/null +++ b/cpp02/ex02/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex02", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex02/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp02/ex02/.vscode/settings.json b/cpp02/ex02/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp02/ex02/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp02/ex02/Fixed.cpp b/cpp02/ex02/Fixed.cpp new file mode 100644 index 0000000..90ad7ce --- /dev/null +++ b/cpp02/ex02/Fixed.cpp @@ -0,0 +1,180 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 20:32:07 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 21:41:52 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define CYAN "\033[36m" +#define BLUE "\033[34m" + +// Constructeur par défaut +Fixed::Fixed() : value(0) +{ + std::cout << GREEN << "Default constructor called" << RESET << std::endl; +} + +// Constructeur avec un entier +Fixed::Fixed(const int intValue) : value(intValue << fractionalBits) +{ + std::cout << CYAN << "Int constructor called" << RESET << std::endl; +} + +// Constructeur avec un flottant +Fixed::Fixed(const float floatValue) : value(roundf(floatValue * (1 << fractionalBits))) +{ + std::cout << CYAN << "Float constructor called" << RESET << std::endl; +} + +// Constructeur de recopie +Fixed::Fixed(const Fixed &other) : value(other.value) +{ + std::cout << YELLOW << "Copy constructor called" << RESET << std::endl; +} + +// Opérateur d'affectation +Fixed &Fixed::operator=(const Fixed &other) +{ + std::cout << YELLOW << "Copy assignment operator called" << RESET << std::endl; + if (this != &other) + { + this->value = other.value; + } + return (*this); +} + +// Destructeur +Fixed::~Fixed() +{ + std::cout << RED << "Destructor called" << RESET << std::endl; +} + +// Conversion en flottant +float Fixed::toFloat(void) const +{ + return ((float)value / (float)(1 << fractionalBits)); +} + +// Conversion en entier +int Fixed::toInt(void) const +{ + return (value >> fractionalBits); +} + +// Opérateur de sortie +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) +{ + stream << fixed.toFloat(); + return (stream); +} + +// Opérateurs de comparaison +bool Fixed::operator>(const Fixed &other) const +{ + return (this->value > other.value); +} + +bool Fixed::operator<(const Fixed &other) const +{ + return (this->value < other.value); +} + +bool Fixed::operator<=(const Fixed &other) const +{ + return (this->value <= other.value); +} + +bool Fixed::operator>=(const Fixed &other) const +{ + return (this->value >= other.value); +} + +bool Fixed::operator==(const Fixed &other) const +{ + return (this->value == other.value); +} + +bool Fixed::operator!=(const Fixed &other) const +{ + return (this->value != other.value); +} + +// Opérateurs arithmétiques (exemple avec +, -, *, /) +Fixed Fixed::operator+(const Fixed &other) const +{ + return (Fixed(this->toFloat() + other.toFloat())); +} + +Fixed Fixed::operator-(const Fixed &other) const +{ + return (Fixed(this->toFloat() - other.toFloat())); +} + +Fixed Fixed::operator*(const Fixed &other) const +{ + return (Fixed(this->toFloat() * other.toFloat())); +} + +Fixed Fixed::operator/(const Fixed &other) const +{ + return (Fixed(this->toFloat() / other.toFloat())); +} + +// Opérateurs d'incrémentation et de décrémentation +Fixed Fixed::operator++() +{ + this->value++; + return (*this); +} + +Fixed Fixed::operator++(int) +{ + Fixed temp = *this; + ++(*this); + return (temp); +} + +Fixed Fixed::operator--() +{ + this->value--; + return (*this); +} + +Fixed Fixed::operator--(int) +{ + Fixed temp = *this; + --(*this); + return (temp); +} + +// Fonctions membres statiques min/max +Fixed &Fixed::min(Fixed &a, Fixed &b) +{ + return ((a < b) ? a : b); +} + +const Fixed &Fixed::min(const Fixed &a, const Fixed &b) +{ + return ((a < b) ? a : b); +} + +Fixed &Fixed::max(Fixed &a, Fixed &b) +{ + return ((a > b) ? a : b); +} + +const Fixed &Fixed::max(const Fixed &a, const Fixed &b) +{ + return ((a > b) ? a : b); +} diff --git a/cpp02/ex02/Fixed.hpp b/cpp02/ex02/Fixed.hpp new file mode 100644 index 0000000..5cbecd5 --- /dev/null +++ b/cpp02/ex02/Fixed.hpp @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 20:12:09 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 21:26:38 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_H +#define FIXED_H + +#include +#include // Pour roundf + +class Fixed +{ + public: + Fixed(); // Constructeur par défaut + Fixed(const int intValue); // Constructeur avec un entier + Fixed(const float floatValue); // Constructeur avec un flottant + Fixed(const Fixed &other); // Constructeur de recopie + Fixed &operator=(const Fixed &other); // Opérateur d'affectation + ~Fixed(); // Destructeur + + float toFloat(void) const; // Convertit en flottant + int toInt(void) const; // Convertit en entier + // Surcharge des opérateurs de comparaison + bool operator>(const Fixed &other) const; + bool operator<(const Fixed &other) const; + bool operator<=(const Fixed &other) const; + bool operator>=(const Fixed &other) const; + bool operator==(const Fixed &other) const; + bool operator!=(const Fixed &other) const; + + // Surcharge des opérateurs arithmétiques + Fixed operator+(const Fixed &other) const; + Fixed operator-(const Fixed &other) const; + Fixed operator*(const Fixed &other) const; + Fixed operator/(const Fixed &other) const; + + // Surcharge des opérateurs d'incrémentation et de décrémentation + Fixed operator++(); // Pré-incrémentation + Fixed operator++(int); // Post-incrémentation + Fixed operator--(int); + Fixed operator--(); + + // Fonctions membres statiques + static Fixed &min(Fixed &a, Fixed &b); + static const Fixed &min(const Fixed &a, const Fixed &b); + static Fixed &max(Fixed &a, Fixed &b); + static const Fixed &max(const Fixed &a, const Fixed &b); + + private: + int value; // Valeur du nombre à virgule fixe + static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire +}; + +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); // Surcharge de l'opérateur d’insertion << ésentation en virgule flottante du nombre à virgule fixe dans le flux de sortie (objet output stream) + +#endif diff --git a/cpp02/ex02/Fixed.o b/cpp02/ex02/Fixed.o new file mode 100644 index 0000000..bb12611 Binary files /dev/null and b/cpp02/ex02/Fixed.o differ diff --git a/cpp02/ex02/Makefile b/cpp02/ex02/Makefile new file mode 100644 index 0000000..2bbdcf2 --- /dev/null +++ b/cpp02/ex02/Makefile @@ -0,0 +1,62 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/27 20:58:29 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = fixed + +SRC = Fixed.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./fixed" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp02/ex02/fixed b/cpp02/ex02/fixed new file mode 100755 index 0000000..9d50c9e Binary files /dev/null and b/cpp02/ex02/fixed differ diff --git a/cpp02/ex02/main.cpp b/cpp02/ex02/main.cpp new file mode 100644 index 0000000..d5cf48f --- /dev/null +++ b/cpp02/ex02/main.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 21:42:40 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 21:43:55 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +int main(void) +{ + Fixed a; + Fixed const b( Fixed( 5.05f ) * Fixed( 2 ) ); + std::cout << a << std::endl; + std::cout << ++a << std::endl; + std::cout << a << std::endl; + std::cout << a++ << std::endl; + std::cout << a << std::endl; + std::cout << b << std::endl; + std::cout << Fixed::max( a, b ) << std::endl; +return (0); +} \ No newline at end of file diff --git a/cpp02/ex02/main.o b/cpp02/ex02/main.o new file mode 100644 index 0000000..1398d16 Binary files /dev/null and b/cpp02/ex02/main.o differ diff --git a/cpp02/ex03/.vscode/c_cpp_properties.json b/cpp02/ex03/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp02/ex03/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp02/ex03/.vscode/launch.json b/cpp02/ex03/.vscode/launch.json new file mode 100644 index 0000000..dfaf23a --- /dev/null +++ b/cpp02/ex03/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex03", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex03/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp02/ex03/.vscode/settings.json b/cpp02/ex03/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp02/ex03/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp02/ex03/Fixed.cpp b/cpp02/ex03/Fixed.cpp new file mode 100644 index 0000000..e7e72fb --- /dev/null +++ b/cpp02/ex03/Fixed.cpp @@ -0,0 +1,171 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 20:32:07 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 22:42:03 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define CYAN "\033[36m" +#define BLUE "\033[34m" + +// Constructeur par défaut +Fixed::Fixed() : value(0) +{ + std::cout << GREEN << "Default constructor called" << RESET << std::endl; +} + +// Constructeur avec un entier +Fixed::Fixed(const int intValue) : value(intValue << fractionalBits) +{ + std::cout << CYAN << "Int constructor called" << RESET << std::endl; +} + +// Constructeur avec un flottant +Fixed::Fixed(const float floatValue) : value(roundf(floatValue * (1 << fractionalBits))) {} + +// Constructeur de recopie +Fixed::Fixed(const Fixed &other) : value(other.value) {} + +// Opérateur d'affectation +Fixed &Fixed::operator=(const Fixed &other) +{ + std::cout << YELLOW << "Copy assignment operator called" << RESET << std::endl; + if (this != &other) + { + this->value = other.value; + } + return (*this); +} + +// Destructeur +Fixed::~Fixed() {} + +// Conversion en flottant +float Fixed::toFloat(void) const +{ + return ((float)value / (float)(1 << fractionalBits)); +} + +// Conversion en entier +int Fixed::toInt(void) const +{ + return (value >> fractionalBits); +} + +// Opérateur de sortie +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) +{ + stream << fixed.toFloat(); + return (stream); +} + +// Opérateurs de comparaison +bool Fixed::operator>(const Fixed &other) const +{ + return (this->value > other.value); +} + +bool Fixed::operator<(const Fixed &other) const +{ + return (this->value < other.value); +} + +bool Fixed::operator<=(const Fixed &other) const +{ + return (this->value <= other.value); +} + +bool Fixed::operator>=(const Fixed &other) const +{ + return (this->value >= other.value); +} + +bool Fixed::operator==(const Fixed &other) const +{ + return (this->value == other.value); +} + +bool Fixed::operator!=(const Fixed &other) const +{ + return (this->value != other.value); +} + +// Opérateurs arithmétiques (exemple avec +, -, *, /) +Fixed Fixed::operator+(const Fixed &other) const +{ + return (Fixed(this->toFloat() + other.toFloat())); +} + +Fixed Fixed::operator-(const Fixed &other) const +{ + return (Fixed(this->toFloat() - other.toFloat())); +} + +Fixed Fixed::operator*(const Fixed &other) const +{ + return (Fixed(this->toFloat() * other.toFloat())); +} + +Fixed Fixed::operator/(const Fixed &other) const +{ + return (Fixed(this->toFloat() / other.toFloat())); +} + +// Opérateurs d'incrémentation et de décrémentation +Fixed Fixed::operator++() +{ + this->value++; + return (*this); +} + +Fixed Fixed::operator++(int) +{ + Fixed temp = *this; + ++(*this); + return (temp); +} + +Fixed Fixed::operator--() +{ + this->value--; + return (*this); +} + +Fixed Fixed::operator--(int) +{ + Fixed temp = *this; + --(*this); + return (temp); +} + +// Fonctions membres statiques min/max +Fixed &Fixed::min(Fixed &a, Fixed &b) +{ + return ((a < b) ? a : b); +} + +const Fixed &Fixed::min(const Fixed &a, const Fixed &b) +{ + return ((a < b) ? a : b); +} + +Fixed &Fixed::max(Fixed &a, Fixed &b) +{ + return ((a > b) ? a : b); +} + +const Fixed &Fixed::max(const Fixed &a, const Fixed &b) +{ + return ((a > b) ? a : b); +} diff --git a/cpp02/ex03/Fixed.hpp b/cpp02/ex03/Fixed.hpp new file mode 100644 index 0000000..5cbecd5 --- /dev/null +++ b/cpp02/ex03/Fixed.hpp @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 20:12:09 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 21:26:38 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_H +#define FIXED_H + +#include +#include // Pour roundf + +class Fixed +{ + public: + Fixed(); // Constructeur par défaut + Fixed(const int intValue); // Constructeur avec un entier + Fixed(const float floatValue); // Constructeur avec un flottant + Fixed(const Fixed &other); // Constructeur de recopie + Fixed &operator=(const Fixed &other); // Opérateur d'affectation + ~Fixed(); // Destructeur + + float toFloat(void) const; // Convertit en flottant + int toInt(void) const; // Convertit en entier + // Surcharge des opérateurs de comparaison + bool operator>(const Fixed &other) const; + bool operator<(const Fixed &other) const; + bool operator<=(const Fixed &other) const; + bool operator>=(const Fixed &other) const; + bool operator==(const Fixed &other) const; + bool operator!=(const Fixed &other) const; + + // Surcharge des opérateurs arithmétiques + Fixed operator+(const Fixed &other) const; + Fixed operator-(const Fixed &other) const; + Fixed operator*(const Fixed &other) const; + Fixed operator/(const Fixed &other) const; + + // Surcharge des opérateurs d'incrémentation et de décrémentation + Fixed operator++(); // Pré-incrémentation + Fixed operator++(int); // Post-incrémentation + Fixed operator--(int); + Fixed operator--(); + + // Fonctions membres statiques + static Fixed &min(Fixed &a, Fixed &b); + static const Fixed &min(const Fixed &a, const Fixed &b); + static Fixed &max(Fixed &a, Fixed &b); + static const Fixed &max(const Fixed &a, const Fixed &b); + + private: + int value; // Valeur du nombre à virgule fixe + static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire +}; + +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); // Surcharge de l'opérateur d’insertion << ésentation en virgule flottante du nombre à virgule fixe dans le flux de sortie (objet output stream) + +#endif diff --git a/cpp02/ex03/Fixed.o b/cpp02/ex03/Fixed.o new file mode 100644 index 0000000..18e68da Binary files /dev/null and b/cpp02/ex03/Fixed.o differ diff --git a/cpp02/ex03/Makefile b/cpp02/ex03/Makefile new file mode 100644 index 0000000..a5e8e54 --- /dev/null +++ b/cpp02/ex03/Makefile @@ -0,0 +1,64 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/27 22:09:54 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = bsp + +SRC = Fixed.cpp \ + bsp.cpp \ + Point.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./bsp" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp02/ex03/Point.cpp b/cpp02/ex03/Point.cpp new file mode 100644 index 0000000..a1f512c --- /dev/null +++ b/cpp02/ex03/Point.cpp @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Point.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 22:06:12 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 22:38:12 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Point.hpp" + +// Définitions de couleur pour les logs +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define CYAN "\033[36m" + +// Constructeur par défaut +Point::Point() : x(0), y(0) +{ + std::cout << GREEN << "Default constructor called (Point):\n x(0), y(0)" << RESET << std::endl; +} + +// Constructeur qui initialise x et y avec des flottants +Point::Point(float const xVal, float const yVal) : x(xVal), y(yVal) +{ + std::cout << CYAN << "Float constructor called (Point):\n x(" << xVal << "), y(" << yVal << ")" << RESET << std::endl; +} + +// Constructeur de recopie +Point::Point(Point const &other) : x(other.getX()), y(other.getY()) +{ + std::cout << YELLOW << "Copy constructor called (Point):\n x(" << other.getX() << "), y(" << other.getY() << ")" << RESET << std::endl; +} + +// Opérateur d'affectation +Point &Point::operator=(Point const &other) +{ + // Comme x et y sont constants, ils ne peuvent être réaffectés. + std::cout << RED << "Attempt to use copy assignment operator on Point, which is not allowed due to const members." << RESET << std::endl; + return (*this); // Retourne une référence à l'instance actuelle +} + +// Destructeur +Point::~Point() +{ + std::cout << RED << "Destructor called (Point):\n x(" << this->x << "), y(" << this->y << ")" << RESET << std::endl; +} + +// Getters pour accéder aux coordonnées x et y +Fixed Point::getX() const +{ + return (this->x); +} + +Fixed Point::getY() const +{ + return (this->y); +} diff --git a/cpp02/ex03/Point.hpp b/cpp02/ex03/Point.hpp new file mode 100644 index 0000000..23af4d4 --- /dev/null +++ b/cpp02/ex03/Point.hpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Point.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 21:52:45 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 22:23:40 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef POINT_HPP +#define POINT_HPP + +#include "Fixed.hpp" + +class Point +{ + public: + Point(); // Constructeur par défaut + Point(float const x, float const y); // Constructeur avec deux flottants + Point(Point const &other); // Constructeur de recopie + Point &operator=(Point const &other); // Opérateur d'affectation + ~Point(); // Destructeur + + // Getters pour accéder aux coordonnées x et y + Fixed getX() const; + Fixed getY() const; + + private: + Fixed const x; // Coordonnée x du point + Fixed const y; // Coordonnée y du point +}; + +#endif diff --git a/cpp02/ex03/Point.o b/cpp02/ex03/Point.o new file mode 100644 index 0000000..9c1ce6a Binary files /dev/null and b/cpp02/ex03/Point.o differ diff --git a/cpp02/ex03/bsp b/cpp02/ex03/bsp new file mode 100755 index 0000000..037591e Binary files /dev/null and b/cpp02/ex03/bsp differ diff --git a/cpp02/ex03/bsp.cpp b/cpp02/ex03/bsp.cpp new file mode 100644 index 0000000..631ecae --- /dev/null +++ b/cpp02/ex03/bsp.cpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bsp.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 21:59:43 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 22:22:35 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "bsp.hpp" + +float sign(Point p1, Point p2, Point p3) +{ + Fixed result = (p1.getX() - p3.getX()) * (p2.getY() - p3.getY()) - (p2.getX() - p3.getX()) * (p1.getY() - p3.getY()); + return (result.toFloat()); // Convertit le résultat Fixed en float +} + +bool bsp(Point const a, Point const b, Point const c, Point const point) +{ + float d1, d2, d3; + bool has_neg, has_pos; + + d1 = sign(point, a, b); + d2 = sign(point, b, c); + d3 = sign(point, c, a); + + has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0); + has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0); + + return (!(has_neg && has_pos)); +} diff --git a/cpp02/ex03/bsp.hpp b/cpp02/ex03/bsp.hpp new file mode 100644 index 0000000..75c2c5c --- /dev/null +++ b/cpp02/ex03/bsp.hpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bsp.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 22:15:54 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 22:17:48 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BSP_HPP +# define BSP_HPP + +#include "Point.hpp" + +float sign(Point p1, Point p2, Point p3); +bool bsp(Point const a, Point const b, Point const c, Point const point); + +#endif diff --git a/cpp02/ex03/bsp.o b/cpp02/ex03/bsp.o new file mode 100644 index 0000000..e71ff93 Binary files /dev/null and b/cpp02/ex03/bsp.o differ diff --git a/cpp02/ex03/main.cpp b/cpp02/ex03/main.cpp new file mode 100644 index 0000000..20caed2 --- /dev/null +++ b/cpp02/ex03/main.cpp @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 22:11:13 by fgras-ca #+# #+# */ +/* Updated: 2023/12/27 22:42:40 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Point.hpp" +#include "bsp.hpp" + +// Fonction pour demander un point à l'utilisateur +Point demanderPoint(const std::string &nomPoint) +{ + float x, y; + std::cout << "Entrez la coordonnée x de " << nomPoint << ": "; + std::cin >> x; + std::cout << "Entrez la coordonnée y de " << nomPoint << ": "; + std::cin >> y; + return (Point(x, y)); +} + +int main() +{ + // Test fixe : Créer un triangle et un point, puis tester avec la fonction bsp + Point a(0.0f, 0.0f); + Point b(1.0f, 0.0f); + Point c(0.5f, 1.0f); + Point pointFixe(0.5f, 0.5f); + + std::cout << "Test fixe:" << std::endl; + std::cout << "Point à l'intérieur du triangle (0.5, 0.5) : \n" << (bsp(a, b, c, pointFixe) ? "Oui" : "Non") << std::endl; + + // Test dynamique : Demander à l'utilisateur de saisir les points + std::cout << "\nTest dynamique:" << std::endl; + Point aUser = demanderPoint("A"); + Point bUser = demanderPoint("B"); + Point cUser = demanderPoint("C"); + Point pointUser = demanderPoint("Point à tester"); + + // Afficher si le point est à l'intérieur du triangle ou non + std::cout << "Le point est à l'intérieur du triangle : " << (bsp(aUser, bUser, cUser, pointUser) ? "Oui" : "Non") << std::endl; + + return (0); +} diff --git a/cpp02/ex03/main.o b/cpp02/ex03/main.o new file mode 100644 index 0000000..33d8c01 Binary files /dev/null and b/cpp02/ex03/main.o differ diff --git a/cpp03/ex00/.vscode/c_cpp_properties.json b/cpp03/ex00/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp03/ex00/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp03/ex00/.vscode/launch.json b/cpp03/ex00/.vscode/launch.json new file mode 100644 index 0000000..3c5a244 --- /dev/null +++ b/cpp03/ex00/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex00", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex00/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp03/ex00/.vscode/settings.json b/cpp03/ex00/.vscode/settings.json new file mode 100644 index 0000000..ebd8700 --- /dev/null +++ b/cpp03/ex00/.vscode/settings.json @@ -0,0 +1,105 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp03/ex00/ClapTrap.cpp b/cpp03/ex00/ClapTrap.cpp new file mode 100644 index 0000000..43b37d7 --- /dev/null +++ b/cpp03/ex00/ClapTrap.cpp @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:59:36 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:29:13 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap():Name(""), HitPoints(10), EnergyPoints(10), AttackDamage(0) {} + +ClapTrap::ClapTrap(std::string name) : Name(name), HitPoints(10), EnergyPoints(10), AttackDamage(0) +{ + std::cout << GREEN << "ClapTrap " << this->Name << " is created!\n" << RESET << std::endl; +} + +ClapTrap::ClapTrap(const ClapTrap ©) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for ClapTrap " << this->Name << RESET << std::endl; + return; +} + +ClapTrap &ClapTrap::operator = (const ClapTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for ClapTrap " << this->Name << RESET << std::endl; + return (*this); +} + +ClapTrap::~ClapTrap() +{ + std::cout << RED << "\nClapTrap " << this->Name << " is detroyed!" << RESET << std::endl; +} + +void ClapTrap::attack(const std::string& target) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + std::cout << MAGENTA << "ClapTrap " << Name << " attacks " << target << ", causing " << AttackDamage << " points of damage!" << RESET << std::endl; + EnergyPoints--; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (HitPoints > amount) + { + HitPoints -= amount; + std::cout << RED << "ClapTrap " << Name << " takes " << amount << " points of damage!" << RESET << std::endl; + } + else + { + HitPoints = 0; + std::cout << "ClapTrap " << Name << " is out of hit points and can't take more damage!" << std::endl; + } +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + HitPoints += amount; + EnergyPoints--; + std::cout << BLUE << "ClapTrap " << Name << " repairs itself, recovering " << amount << " hit points!" << RESET << std::endl; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to repair itself!" << std::endl; + } +} + +std::string ClapTrap::getName() const +{ + return (this->Name); +} + +unsigned int ClapTrap::getHitPoints() const +{ + return (this->HitPoints); +} + +unsigned int ClapTrap::getEnergyPoints() const +{ + return (this->EnergyPoints); +} + +unsigned int ClapTrap::getAttackDamage() const +{ + return (this->AttackDamage); +} + +void ClapTrap::printStats() const +{ + std::cout << GREEN << "\nStatistiques pour " << Name << ":" << RESET << std::endl; + std::cout << "Points de vie: " << HitPoints << std::endl; + std::cout << "Points d'énergie: " << EnergyPoints << std::endl; + std::cout << "Dégâts d'attaque: " << AttackDamage << std::endl; +} diff --git a/cpp03/ex00/ClapTrap.hpp b/cpp03/ex00/ClapTrap.hpp new file mode 100644 index 0000000..a81f8cb --- /dev/null +++ b/cpp03/ex00/ClapTrap.hpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:53:03 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:11:24 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLAPTRAP_HPP +#define CLAPTRAP_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class ClapTrap +{ + private: + std::string Name; + unsigned int HitPoints; + unsigned int EnergyPoints; + unsigned int AttackDamage; + + public: + ClapTrap(); + ClapTrap(std::string name); + ClapTrap(const ClapTrap ©); + ClapTrap & operator = (const ClapTrap &operator_aff); + ~ClapTrap(); + + void attack(const std::string& target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); + + std::string getName() const; + unsigned int getHitPoints() const; + unsigned int getEnergyPoints() const; + unsigned int getAttackDamage() const; + void printStats() const; +}; + +#endif \ No newline at end of file diff --git a/cpp03/ex00/ClapTrap.o b/cpp03/ex00/ClapTrap.o new file mode 100644 index 0000000..052d807 Binary files /dev/null and b/cpp03/ex00/ClapTrap.o differ diff --git a/cpp03/ex00/Makefile b/cpp03/ex00/Makefile new file mode 100644 index 0000000..84c5057 --- /dev/null +++ b/cpp03/ex00/Makefile @@ -0,0 +1,62 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/30 14:46:09 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = claptrap + +SRC = ClapTrap.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./claptrap" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp03/ex00/claptrap b/cpp03/ex00/claptrap new file mode 100755 index 0000000..a14e7f0 Binary files /dev/null and b/cpp03/ex00/claptrap differ diff --git a/cpp03/ex00/main.cpp b/cpp03/ex00/main.cpp new file mode 100644 index 0000000..82276e5 --- /dev/null +++ b/cpp03/ex00/main.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 14:27:32 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 20:33:44 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +int main() +{ + ClapTrap claptrap1("ClapTrap1"); + ClapTrap claptrap2("ClapTrap2"); + + claptrap1.printStats(); + claptrap2.printStats(); + + claptrap1.attack(claptrap2.getName()); + claptrap2.takeDamage(claptrap1.getAttackDamage()); + + claptrap2.attack(claptrap1.getName()); + claptrap1.takeDamage(claptrap2.getAttackDamage()); + + claptrap1.beRepaired(5); + + claptrap2.beRepaired(3); + + for (int i = 0; i < 3; i++) + { + claptrap1.attack(claptrap2.getName()); + claptrap2.takeDamage(claptrap1.getAttackDamage()); + + claptrap2.attack(claptrap1.getName()); + claptrap1.takeDamage(claptrap2.getAttackDamage()); + } + claptrap1.printStats(); + claptrap2.printStats(); + + return (0); +} diff --git a/cpp03/ex00/main.o b/cpp03/ex00/main.o new file mode 100644 index 0000000..8e1720d Binary files /dev/null and b/cpp03/ex00/main.o differ diff --git a/cpp03/ex01/.vscode/c_cpp_properties.json b/cpp03/ex01/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp03/ex01/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp03/ex01/.vscode/launch.json b/cpp03/ex01/.vscode/launch.json new file mode 100644 index 0000000..4767990 --- /dev/null +++ b/cpp03/ex01/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex01", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex01/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp03/ex01/.vscode/settings.json b/cpp03/ex01/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp03/ex01/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp03/ex01/ClapTrap.cpp b/cpp03/ex01/ClapTrap.cpp new file mode 100644 index 0000000..8704bd3 --- /dev/null +++ b/cpp03/ex01/ClapTrap.cpp @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:59:36 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:28:06 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap():Name(""), HitPoints(10), EnergyPoints(10), AttackDamage(0) {} + +ClapTrap::ClapTrap(std::string name) : Name(name), HitPoints(10), EnergyPoints(10), AttackDamage(0) +{ + std::cout << GREEN << "ClapTrap contructor " << this->Name << " is created!\n" << RESET << std::endl; +} + +ClapTrap::ClapTrap(const ClapTrap ©) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for ClapTrap " << this->Name << RESET << std::endl; + return; +} + +ClapTrap &ClapTrap::operator = (const ClapTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for ClapTrap " << this->Name << RESET << std::endl; + return (*this); +} + +ClapTrap::~ClapTrap() +{ + std::cout << RED << "\nClapTrap " << this->Name << " is detroyed!" << RESET << std::endl; +} + +void ClapTrap::attack(const std::string& target) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + std::cout << MAGENTA << "ClapTrap " << Name << " attacks " << target << ", causing " << AttackDamage << " points of damage!" << RESET << std::endl; + EnergyPoints--; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (HitPoints > amount) + { + HitPoints -= amount; + std::cout << RED << "ClapTrap " << Name << " takes " << amount << " points of damage!" << RESET << std::endl; + } + else + { + HitPoints = 0; + std::cout << "ClapTrap " << Name << " is out of hit points and can't take more damage!" << std::endl; + } +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + HitPoints += amount; + EnergyPoints--; + std::cout << BLUE << "ClapTrap " << Name << " repairs itself, recovering " << amount << " hit points!" << RESET << std::endl; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to repair itself!" << std::endl; + } +} + +std::string ClapTrap::getName() const +{ + return (this->Name); +} + +unsigned int ClapTrap::getHitPoints() const +{ + return (this->HitPoints); +} + +unsigned int ClapTrap::getEnergyPoints() const +{ + return (this->EnergyPoints); +} + +unsigned int ClapTrap::getAttackDamage() const +{ + return (this->AttackDamage); +} + +void ClapTrap::printStats() const +{ + std::cout << GREEN << "\nStatistiques pour " << Name << ":" << RESET << std::endl; + std::cout << "Points de vie: " << HitPoints << std::endl; + std::cout << "Points d'énergie: " << EnergyPoints << std::endl; + std::cout << "Dégâts d'attaque: " << AttackDamage << std::endl; +} diff --git a/cpp03/ex01/ClapTrap.hpp b/cpp03/ex01/ClapTrap.hpp new file mode 100644 index 0000000..b7e3336 --- /dev/null +++ b/cpp03/ex01/ClapTrap.hpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:53:03 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:24:32 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLAPTRAP_HPP +#define CLAPTRAP_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class ClapTrap +{ + protected: + std::string Name; + unsigned int HitPoints; + unsigned int EnergyPoints; + unsigned int AttackDamage; + + public: + ClapTrap(); + ClapTrap(std::string name); + ClapTrap(const ClapTrap ©); + ClapTrap & operator = (const ClapTrap &operator_aff); + ~ClapTrap(); + + void attack(const std::string& target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); + + std::string getName() const; + unsigned int getHitPoints() const; + unsigned int getEnergyPoints() const; + unsigned int getAttackDamage() const; + void printStats() const; +}; + +#endif \ No newline at end of file diff --git a/cpp03/ex01/Makefile b/cpp03/ex01/Makefile new file mode 100644 index 0000000..489e671 --- /dev/null +++ b/cpp03/ex01/Makefile @@ -0,0 +1,63 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/30 16:04:30 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = scavtrap + +SRC = ClapTrap.cpp \ + ScavTrap.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./scavtrap" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp03/ex01/ScavTrap.cpp b/cpp03/ex01/ScavTrap.cpp new file mode 100644 index 0000000..e7a4c2f --- /dev/null +++ b/cpp03/ex01/ScavTrap.cpp @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:42:16 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:28:16 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScavTrap.hpp" + +ScavTrap::ScavTrap() : ClapTrap() +{ + this->HitPoints = 100; + this->EnergyPoints = 50; + this->AttackDamage = 20; +} + +ScavTrap::ScavTrap(std::string name) : ClapTrap(name) +{ + this->HitPoints = 100; + this->EnergyPoints = 50; + this->AttackDamage = 20; + std::cout << GREEN << "ScavTrap constructor " << this->Name << " has been created." << RESET << std::endl; +} + +ScavTrap::ScavTrap(const ScavTrap ©) : ClapTrap(copy) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for ScavTrap " << this->Name << RESET << std::endl; +} + +ScavTrap &ScavTrap::operator=(const ScavTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for ScavTrap " << this->Name << RESET << std::endl; + return (*this); +} + +ScavTrap::~ScavTrap() +{ + std::cout << RED << "ScavTrap " << this->Name << " has been destroyed." << RESET << std::endl; +} + +void ScavTrap::attack(const std::string& target) +{ + if (this->EnergyPoints > 0 && this->HitPoints > 0) + { + std::cout << CYAN << "ScavTrap " << this->Name << " attacks " << target << ", causing " << this->AttackDamage << " points of damage!" << RESET << std::endl; + this->EnergyPoints--; + } + else + { + std::cout << "ScavTrap " << this->Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void ScavTrap::guardGate() +{ + std::cout << "ScavTrap " << this->Name << " is now in Gate keeper mode." << std::endl; +} diff --git a/cpp03/ex01/ScavTrap.hpp b/cpp03/ex01/ScavTrap.hpp new file mode 100644 index 0000000..8ecd43f --- /dev/null +++ b/cpp03/ex01/ScavTrap.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:34:27 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:24:07 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SCAVTRAP_HPP +#define SCAVTRAP_HPP + +#include "ClapTrap.hpp" + +class ScavTrap : public ClapTrap +{ + public: + ScavTrap(); + ScavTrap(std::string name); + ScavTrap(const ScavTrap ©); + ScavTrap & operator = (const ScavTrap &operator_aff); + ~ScavTrap(); + + void attack(const std::string& target); + void guardGate(); +}; + +#endif diff --git a/cpp03/ex01/main.cpp b/cpp03/ex01/main.cpp new file mode 100644 index 0000000..a536d82 --- /dev/null +++ b/cpp03/ex01/main.cpp @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:59:39 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 20:40:16 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" +#include "ScavTrap.hpp" + +int main() +{ + ClapTrap claptrap1("Clappy"); + ScavTrap scavtrap1("Serena"); + + claptrap1.printStats(); + scavtrap1.printStats(); + + std::cout << "\n--- Tests ClapTrap ---" << std::endl; + claptrap1.attack("un ennemi"); + claptrap1.takeDamage(3); + claptrap1.beRepaired(2); + + std::cout << "\n--- Tests ScavTrap ---" << std::endl; + scavtrap1.attack("un autre ennemi"); + scavtrap1.takeDamage(15); + scavtrap1.beRepaired(10); + scavtrap1.guardGate(); + + std::cout << "\n--- Tests avancés : Interactions entre ClapTrap et ScavTrap ---" << std::endl; + claptrap1.attack(scavtrap1.getName()); + scavtrap1.takeDamage(claptrap1.getAttackDamage()); + + scavtrap1.attack(claptrap1.getName()); + claptrap1.takeDamage(scavtrap1.getAttackDamage()); + + std::cout << "\n--- Tests des limites ---" << std::endl; + while (scavtrap1.getEnergyPoints() > 0) + { + scavtrap1.attack(claptrap1.getName()); + claptrap1.takeDamage(scavtrap1.getAttackDamage()); + } + + std::cout << "\n--- ScavTrap essaie d'attaquer sans points d'énergie ---" << std::endl; + scavtrap1.attack(claptrap1.getName()); + + std::cout << "\n--- ClapTrap essaie de se réparer avec des points de vie au maximum ---" << std::endl; + claptrap1.beRepaired(1); + + claptrap1.printStats(); + scavtrap1.printStats(); + + return (0); +} diff --git a/cpp03/ex02/.vscode/c_cpp_properties.json b/cpp03/ex02/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp03/ex02/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp03/ex02/.vscode/launch.json b/cpp03/ex02/.vscode/launch.json new file mode 100644 index 0000000..e8e4e77 --- /dev/null +++ b/cpp03/ex02/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex02", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex02/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp03/ex02/.vscode/settings.json b/cpp03/ex02/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp03/ex02/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp03/ex02/ClapTrap.cpp b/cpp03/ex02/ClapTrap.cpp new file mode 100644 index 0000000..8704bd3 --- /dev/null +++ b/cpp03/ex02/ClapTrap.cpp @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:59:36 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:28:06 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap():Name(""), HitPoints(10), EnergyPoints(10), AttackDamage(0) {} + +ClapTrap::ClapTrap(std::string name) : Name(name), HitPoints(10), EnergyPoints(10), AttackDamage(0) +{ + std::cout << GREEN << "ClapTrap contructor " << this->Name << " is created!\n" << RESET << std::endl; +} + +ClapTrap::ClapTrap(const ClapTrap ©) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for ClapTrap " << this->Name << RESET << std::endl; + return; +} + +ClapTrap &ClapTrap::operator = (const ClapTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for ClapTrap " << this->Name << RESET << std::endl; + return (*this); +} + +ClapTrap::~ClapTrap() +{ + std::cout << RED << "\nClapTrap " << this->Name << " is detroyed!" << RESET << std::endl; +} + +void ClapTrap::attack(const std::string& target) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + std::cout << MAGENTA << "ClapTrap " << Name << " attacks " << target << ", causing " << AttackDamage << " points of damage!" << RESET << std::endl; + EnergyPoints--; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (HitPoints > amount) + { + HitPoints -= amount; + std::cout << RED << "ClapTrap " << Name << " takes " << amount << " points of damage!" << RESET << std::endl; + } + else + { + HitPoints = 0; + std::cout << "ClapTrap " << Name << " is out of hit points and can't take more damage!" << std::endl; + } +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + HitPoints += amount; + EnergyPoints--; + std::cout << BLUE << "ClapTrap " << Name << " repairs itself, recovering " << amount << " hit points!" << RESET << std::endl; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to repair itself!" << std::endl; + } +} + +std::string ClapTrap::getName() const +{ + return (this->Name); +} + +unsigned int ClapTrap::getHitPoints() const +{ + return (this->HitPoints); +} + +unsigned int ClapTrap::getEnergyPoints() const +{ + return (this->EnergyPoints); +} + +unsigned int ClapTrap::getAttackDamage() const +{ + return (this->AttackDamage); +} + +void ClapTrap::printStats() const +{ + std::cout << GREEN << "\nStatistiques pour " << Name << ":" << RESET << std::endl; + std::cout << "Points de vie: " << HitPoints << std::endl; + std::cout << "Points d'énergie: " << EnergyPoints << std::endl; + std::cout << "Dégâts d'attaque: " << AttackDamage << std::endl; +} diff --git a/cpp03/ex02/ClapTrap.hpp b/cpp03/ex02/ClapTrap.hpp new file mode 100644 index 0000000..b7e3336 --- /dev/null +++ b/cpp03/ex02/ClapTrap.hpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:53:03 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:24:32 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLAPTRAP_HPP +#define CLAPTRAP_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class ClapTrap +{ + protected: + std::string Name; + unsigned int HitPoints; + unsigned int EnergyPoints; + unsigned int AttackDamage; + + public: + ClapTrap(); + ClapTrap(std::string name); + ClapTrap(const ClapTrap ©); + ClapTrap & operator = (const ClapTrap &operator_aff); + ~ClapTrap(); + + void attack(const std::string& target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); + + std::string getName() const; + unsigned int getHitPoints() const; + unsigned int getEnergyPoints() const; + unsigned int getAttackDamage() const; + void printStats() const; +}; + +#endif \ No newline at end of file diff --git a/cpp03/ex02/FragTrap.cpp b/cpp03/ex02/FragTrap.cpp new file mode 100644 index 0000000..1532423 --- /dev/null +++ b/cpp03/ex02/FragTrap.cpp @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FragTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 16:22:04 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:35:21 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "FragTrap.hpp" + +FragTrap::FragTrap() : ClapTrap() +{ + this->HitPoints = 100; + this->EnergyPoints = 100; + this->AttackDamage = 30; +} + +FragTrap::FragTrap(std::string name) : ClapTrap(name) +{ + this->HitPoints = 100; + this->EnergyPoints = 100; + this->AttackDamage = 30; + std::cout << GREEN << "FragTrap contructor " << this->Name << " has been created." << RESET << std::endl; +} + +FragTrap::FragTrap(const FragTrap ©) : ClapTrap(copy) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for FragTrap " << this->Name << RESET << std::endl; +} + +FragTrap &FragTrap::operator=(const FragTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for FragTrap " << this->Name << RESET << std::endl; + return (*this); +} + +FragTrap::~FragTrap() +{ + std::cout << RED << "FragTrap " << this->Name << " has been destroyed." << RESET << std::endl; +} + +void FragTrap::attack(const std::string& target) +{ + if (this->EnergyPoints > 0 && this->HitPoints > 0) + { + std::cout << CYAN << "FragTrap " << this->Name << " attacks " << target << ", causing " << this->AttackDamage << " points of damage!" << RESET << std::endl; + this->EnergyPoints--; + } + else + { + std::cout << "FragTrap " << this->Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void FragTrap::highFivesGuys(void) +{ + std::cout << YELLOW << "FragTrap " << this->Name << " requests a positive high five!" << RESET << std::endl; +} \ No newline at end of file diff --git a/cpp03/ex02/FragTrap.hpp b/cpp03/ex02/FragTrap.hpp new file mode 100644 index 0000000..ebe7eed --- /dev/null +++ b/cpp03/ex02/FragTrap.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FragTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 16:19:16 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:33:38 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FRAGTRAP_HPP +#define FRAGTRAP_HPP + +#include "ClapTrap.hpp" + +class FragTrap : public ClapTrap +{ + public: + FragTrap(); + FragTrap(std::string name); + FragTrap(const FragTrap ©); + FragTrap & operator = (const FragTrap &operator_aff); + ~FragTrap(); + + void attack(const std::string& target); + void highFivesGuys(void); +}; + +#endif diff --git a/cpp03/ex02/Makefile b/cpp03/ex02/Makefile new file mode 100644 index 0000000..0dd750b --- /dev/null +++ b/cpp03/ex02/Makefile @@ -0,0 +1,64 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/30 16:29:47 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = fragtrap + +SRC = ClapTrap.cpp \ + ScavTrap.cpp \ + FragTrap.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./fragtrap" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp03/ex02/ScavTrap.cpp b/cpp03/ex02/ScavTrap.cpp new file mode 100644 index 0000000..e7a4c2f --- /dev/null +++ b/cpp03/ex02/ScavTrap.cpp @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:42:16 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:28:16 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScavTrap.hpp" + +ScavTrap::ScavTrap() : ClapTrap() +{ + this->HitPoints = 100; + this->EnergyPoints = 50; + this->AttackDamage = 20; +} + +ScavTrap::ScavTrap(std::string name) : ClapTrap(name) +{ + this->HitPoints = 100; + this->EnergyPoints = 50; + this->AttackDamage = 20; + std::cout << GREEN << "ScavTrap constructor " << this->Name << " has been created." << RESET << std::endl; +} + +ScavTrap::ScavTrap(const ScavTrap ©) : ClapTrap(copy) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for ScavTrap " << this->Name << RESET << std::endl; +} + +ScavTrap &ScavTrap::operator=(const ScavTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for ScavTrap " << this->Name << RESET << std::endl; + return (*this); +} + +ScavTrap::~ScavTrap() +{ + std::cout << RED << "ScavTrap " << this->Name << " has been destroyed." << RESET << std::endl; +} + +void ScavTrap::attack(const std::string& target) +{ + if (this->EnergyPoints > 0 && this->HitPoints > 0) + { + std::cout << CYAN << "ScavTrap " << this->Name << " attacks " << target << ", causing " << this->AttackDamage << " points of damage!" << RESET << std::endl; + this->EnergyPoints--; + } + else + { + std::cout << "ScavTrap " << this->Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void ScavTrap::guardGate() +{ + std::cout << "ScavTrap " << this->Name << " is now in Gate keeper mode." << std::endl; +} diff --git a/cpp03/ex02/ScavTrap.hpp b/cpp03/ex02/ScavTrap.hpp new file mode 100644 index 0000000..8ecd43f --- /dev/null +++ b/cpp03/ex02/ScavTrap.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:34:27 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:24:07 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SCAVTRAP_HPP +#define SCAVTRAP_HPP + +#include "ClapTrap.hpp" + +class ScavTrap : public ClapTrap +{ + public: + ScavTrap(); + ScavTrap(std::string name); + ScavTrap(const ScavTrap ©); + ScavTrap & operator = (const ScavTrap &operator_aff); + ~ScavTrap(); + + void attack(const std::string& target); + void guardGate(); +}; + +#endif diff --git a/cpp03/ex02/main.cpp b/cpp03/ex02/main.cpp new file mode 100644 index 0000000..ddb340e --- /dev/null +++ b/cpp03/ex02/main.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:59:39 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 21:15:23 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" +#include "ScavTrap.hpp" +#include "FragTrap.hpp" + +int main() +{ + ClapTrap claptrap("Clappy"); + ScavTrap scavtrap("Serena"); + FragTrap fragtrap("Fraggy"); + + claptrap.printStats(); + scavtrap.printStats(); + fragtrap.printStats(); + + std::cout << "\n--- Tests ClapTrap ---" << std::endl; + claptrap.attack("un ennemi"); + claptrap.takeDamage(3); + claptrap.beRepaired(2); + + std::cout << "\n--- Tests ScavTrap ---" << std::endl; + scavtrap.attack("un autre ennemi"); + scavtrap.takeDamage(15); + scavtrap.beRepaired(10); + scavtrap.guardGate(); + + std::cout << "\n--- Tests FragTrap ---" << std::endl; + fragtrap.attack("un ennemi lointain"); + fragtrap.takeDamage(20); + fragtrap.beRepaired(15); + fragtrap.highFivesGuys(); + + std::cout << "\n--- Tests avancés : Interactions entre différents types de Traps ---" << std::endl; + scavtrap.attack(fragtrap.getName()); + fragtrap.takeDamage(scavtrap.getAttackDamage()); + + fragtrap.attack(scavtrap.getName()); + scavtrap.takeDamage(fragtrap.getAttackDamage()); + + std::cout << "\n--- Tests des limites de ClapTrap ---" << std::endl; + while (claptrap.getEnergyPoints() > 0) + { + claptrap.attack(fragtrap.getName()); + fragtrap.takeDamage(claptrap.getAttackDamage()); + } + claptrap.attack(fragtrap.getName()); + + std::cout << "\n--- Tester les limites de ScavTrap et FragTrap ---" << std::endl; + scavtrap.takeDamage(100); + fragtrap.takeDamage(150); + + claptrap.printStats(); + scavtrap.printStats(); + fragtrap.printStats(); + + return (0); +} diff --git a/cpp03/ex03/.vscode/c_cpp_properties.json b/cpp03/ex03/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp03/ex03/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp03/ex03/.vscode/launch.json b/cpp03/ex03/.vscode/launch.json new file mode 100644 index 0000000..61b63ea --- /dev/null +++ b/cpp03/ex03/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex03", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp03/ex03/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp03/ex03/.vscode/settings.json b/cpp03/ex03/.vscode/settings.json new file mode 100644 index 0000000..ebd8700 --- /dev/null +++ b/cpp03/ex03/.vscode/settings.json @@ -0,0 +1,105 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp03/ex03/ClapTrap.cpp b/cpp03/ex03/ClapTrap.cpp new file mode 100644 index 0000000..d5502d0 --- /dev/null +++ b/cpp03/ex03/ClapTrap.cpp @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:59:36 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 00:01:21 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" + +ClapTrap::ClapTrap():Name(""), HitPoints(10), EnergyPoints(10), AttackDamage(0) +{ + std::cout << GREEN << "ClapTrap default constructor called!\n" << RESET << std::endl; +} + +ClapTrap::ClapTrap(std::string name) : Name(name), HitPoints(10), EnergyPoints(10), AttackDamage(0) +{ + std::cout << GREEN << "ClapTrap contructor " << this->Name << " is created!\n" << RESET << std::endl; +} + +ClapTrap::ClapTrap(const ClapTrap ©) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for ClapTrap " << this->Name << RESET << std::endl; + return; +} + +ClapTrap &ClapTrap::operator = (const ClapTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for ClapTrap " << this->Name << RESET << std::endl; + return (*this); +} + +ClapTrap::~ClapTrap() +{ + std::cout << RED << "\nClapTrap " << this->Name << " is detroyed!" << RESET << std::endl; +} + +void ClapTrap::setHitPoints(int points) +{ + HitPoints = points; +} +void ClapTrap::setEnergypoints(int points) +{ + EnergyPoints = points; +} +void ClapTrap::setAttackDamage(int points) +{ + AttackDamage = points; +} + +void ClapTrap::attack(const std::string& target) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + std::cout << MAGENTA << "ClapTrap " << Name << " attacks " << target << ", causing " << AttackDamage << " points of damage!" << RESET << std::endl; + EnergyPoints--; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void ClapTrap::takeDamage(unsigned int amount) +{ + if (HitPoints > amount) + { + HitPoints -= amount; + std::cout << RED << "ClapTrap " << Name << " takes " << amount << " points of damage!" << RESET << std::endl; + } + else + { + HitPoints = 0; + std::cout << "ClapTrap " << Name << " is out of hit points and can't take more damage!" << std::endl; + } +} + +void ClapTrap::beRepaired(unsigned int amount) +{ + if (EnergyPoints > 0 && HitPoints > 0) + { + HitPoints += amount; + EnergyPoints--; + std::cout << BLUE << "ClapTrap " << Name << " repairs itself, recovering " << amount << " hit points!" << RESET << std::endl; + } + else + { + std::cout << "ClapTrap " << Name << " has no energy or hit points to repair itself!" << std::endl; + } +} + +std::string ClapTrap::getName() const +{ + return (this->Name); +} + +unsigned int ClapTrap::getHitPoints() const +{ + return (this->HitPoints); +} + +unsigned int ClapTrap::getEnergyPoints() const +{ + return (this->EnergyPoints); +} + +unsigned int ClapTrap::getAttackDamage() const +{ + return (this->AttackDamage); +} + +void ClapTrap::printStats() const +{ + std::cout << GREEN << "\nStatistiques pour " << Name << ":" << RESET << std::endl; + std::cout << "Points de vie: " << HitPoints << std::endl; + std::cout << "Points d'énergie: " << EnergyPoints << std::endl; + std::cout << "Dégâts d'attaque: " << AttackDamage << std::endl; +} diff --git a/cpp03/ex03/ClapTrap.hpp b/cpp03/ex03/ClapTrap.hpp new file mode 100644 index 0000000..5d7d188 --- /dev/null +++ b/cpp03/ex03/ClapTrap.hpp @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ClapTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:53:03 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 23:59:57 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLAPTRAP_HPP +#define CLAPTRAP_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class ClapTrap +{ + protected: + std::string Name; + unsigned int HitPoints; + unsigned int EnergyPoints; + unsigned int AttackDamage; + + public: + ClapTrap(); + ClapTrap(std::string name); + ClapTrap(const ClapTrap ©); + ClapTrap & operator = (const ClapTrap &operator_aff); + ~ClapTrap(); + + void attack(const std::string& target); + void takeDamage(unsigned int amount); + void beRepaired(unsigned int amount); + void setHitPoints(int points); + void setEnergypoints(int points); + void setAttackDamage(int points); + + std::string getName() const; + unsigned int getHitPoints() const; + unsigned int getEnergyPoints() const; + unsigned int getAttackDamage() const; + void printStats() const; +}; + +#endif \ No newline at end of file diff --git a/cpp03/ex03/DiamondTrap.cpp b/cpp03/ex03/DiamondTrap.cpp new file mode 100644 index 0000000..158c255 --- /dev/null +++ b/cpp03/ex03/DiamondTrap.cpp @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* DiamondTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 16:57:52 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 00:09:39 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "DiamondTrap.hpp" + +DiamondTrap::DiamondTrap() : ClapTrap(), ScavTrap(), FragTrap() +{ + this->HitPoints = FragTrap::HitPoints; // Défini par FragTrap + this->EnergyPoints = ScavTrap::EnergyPoints; // Défini par ScavTrap + this->AttackDamage = FragTrap::AttackDamage; // Défini par FragTrap +} + +DiamondTrap::DiamondTrap(std::string name) : ClapTrap(name +"_clap_trap"), ScavTrap(name), FragTrap (name), _name(name) +{ + this->HitPoints = FragTrap::HitPoints; // Défini par FragTrap + this->EnergyPoints = ScavTrap::EnergyPoints; // Défini par ScavTrap + this->AttackDamage = FragTrap::AttackDamage; // Défini par FragTrap + std::cout << GREEN << "DiamondTrap " << this->Name << " est créé." << RESET << std::endl; +} + +DiamondTrap::DiamondTrap(const DiamondTrap ©) : ClapTrap(copy), FragTrap(copy), ScavTrap(copy) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for DiamondTrap " << this->Name << RESET << std::endl; +} + +DiamondTrap &DiamondTrap::operator=(const DiamondTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for FragTrap " << this->Name << RESET << std::endl; + return (*this); +} + +DiamondTrap::~DiamondTrap() +{ + std::cout << RED <<"DiamondTrap " << this->Name << " est détruit." << RESET << std::endl; +} + +void DiamondTrap::whoAmI() +{ + std::cout << "Je suis " << this->Name << " et mon nom ClapTrap est " << ClapTrap::Name << "." << std::endl; +} + +void DiamondTrap::attack(const std::string& target) +{ + ScavTrap::attack(target); // Utilise l'attaque de ScavTrap +} diff --git a/cpp03/ex03/DiamondTrap.hpp b/cpp03/ex03/DiamondTrap.hpp new file mode 100644 index 0000000..c7c0eda --- /dev/null +++ b/cpp03/ex03/DiamondTrap.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* DiamondTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 16:56:02 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 23:04:37 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DIAMONDTRAP_H +#define DIAMONDTRAP_H + +#include "FragTrap.hpp" +#include "ScavTrap.hpp" + +class DiamondTrap : public FragTrap, public ScavTrap +{ + private: + std::string _name; + + public: + DiamondTrap(); + DiamondTrap(std::string name); + DiamondTrap(const DiamondTrap ©); + DiamondTrap & operator=(const DiamondTrap &operator_aff); + ~DiamondTrap(); + + void whoAmI(); + void attack(const std::string& target); +}; + +#endif diff --git a/cpp03/ex03/FragTrap.cpp b/cpp03/ex03/FragTrap.cpp new file mode 100644 index 0000000..1532423 --- /dev/null +++ b/cpp03/ex03/FragTrap.cpp @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FragTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 16:22:04 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:35:21 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "FragTrap.hpp" + +FragTrap::FragTrap() : ClapTrap() +{ + this->HitPoints = 100; + this->EnergyPoints = 100; + this->AttackDamage = 30; +} + +FragTrap::FragTrap(std::string name) : ClapTrap(name) +{ + this->HitPoints = 100; + this->EnergyPoints = 100; + this->AttackDamage = 30; + std::cout << GREEN << "FragTrap contructor " << this->Name << " has been created." << RESET << std::endl; +} + +FragTrap::FragTrap(const FragTrap ©) : ClapTrap(copy) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for FragTrap " << this->Name << RESET << std::endl; +} + +FragTrap &FragTrap::operator=(const FragTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for FragTrap " << this->Name << RESET << std::endl; + return (*this); +} + +FragTrap::~FragTrap() +{ + std::cout << RED << "FragTrap " << this->Name << " has been destroyed." << RESET << std::endl; +} + +void FragTrap::attack(const std::string& target) +{ + if (this->EnergyPoints > 0 && this->HitPoints > 0) + { + std::cout << CYAN << "FragTrap " << this->Name << " attacks " << target << ", causing " << this->AttackDamage << " points of damage!" << RESET << std::endl; + this->EnergyPoints--; + } + else + { + std::cout << "FragTrap " << this->Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void FragTrap::highFivesGuys(void) +{ + std::cout << YELLOW << "FragTrap " << this->Name << " requests a positive high five!" << RESET << std::endl; +} \ No newline at end of file diff --git a/cpp03/ex03/FragTrap.hpp b/cpp03/ex03/FragTrap.hpp new file mode 100644 index 0000000..7c48303 --- /dev/null +++ b/cpp03/ex03/FragTrap.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FragTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 16:19:16 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 23:02:55 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FRAGTRAP_HPP +#define FRAGTRAP_HPP + +#include "ClapTrap.hpp" + +class FragTrap : virtual public ClapTrap +{ + public: + FragTrap(); + FragTrap(std::string name); + FragTrap(const FragTrap ©); + FragTrap & operator = (const FragTrap &operator_aff); + ~FragTrap(); + + void attack(const std::string& target); + void highFivesGuys(void); +}; + +#endif diff --git a/cpp03/ex03/Makefile b/cpp03/ex03/Makefile new file mode 100644 index 0000000..93c9fad --- /dev/null +++ b/cpp03/ex03/Makefile @@ -0,0 +1,65 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/30 17:12:42 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = diamondtrap + +SRC = ClapTrap.cpp \ + ScavTrap.cpp \ + FragTrap.cpp \ + DiamondTrap.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./diamondtrap" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp03/ex03/ScavTrap.cpp b/cpp03/ex03/ScavTrap.cpp new file mode 100644 index 0000000..e7a4c2f --- /dev/null +++ b/cpp03/ex03/ScavTrap.cpp @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:42:16 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 22:28:16 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScavTrap.hpp" + +ScavTrap::ScavTrap() : ClapTrap() +{ + this->HitPoints = 100; + this->EnergyPoints = 50; + this->AttackDamage = 20; +} + +ScavTrap::ScavTrap(std::string name) : ClapTrap(name) +{ + this->HitPoints = 100; + this->EnergyPoints = 50; + this->AttackDamage = 20; + std::cout << GREEN << "ScavTrap constructor " << this->Name << " has been created." << RESET << std::endl; +} + +ScavTrap::ScavTrap(const ScavTrap ©) : ClapTrap(copy) +{ + *this = copy; + std::cout << CYAN << "Copy constructor called for ScavTrap " << this->Name << RESET << std::endl; +} + +ScavTrap &ScavTrap::operator=(const ScavTrap &operator_aff) +{ + if (this != &operator_aff) + { + this->Name = operator_aff.Name; + this->HitPoints = operator_aff.HitPoints; + this->EnergyPoints = operator_aff.EnergyPoints; + this->AttackDamage = operator_aff.AttackDamage; + } + std::cout << MAGENTA << "Assignment operator called for ScavTrap " << this->Name << RESET << std::endl; + return (*this); +} + +ScavTrap::~ScavTrap() +{ + std::cout << RED << "ScavTrap " << this->Name << " has been destroyed." << RESET << std::endl; +} + +void ScavTrap::attack(const std::string& target) +{ + if (this->EnergyPoints > 0 && this->HitPoints > 0) + { + std::cout << CYAN << "ScavTrap " << this->Name << " attacks " << target << ", causing " << this->AttackDamage << " points of damage!" << RESET << std::endl; + this->EnergyPoints--; + } + else + { + std::cout << "ScavTrap " << this->Name << " has no energy or hit points to attack!" << std::endl; + } +} + +void ScavTrap::guardGate() +{ + std::cout << "ScavTrap " << this->Name << " is now in Gate keeper mode." << std::endl; +} diff --git a/cpp03/ex03/ScavTrap.hpp b/cpp03/ex03/ScavTrap.hpp new file mode 100644 index 0000000..fc72585 --- /dev/null +++ b/cpp03/ex03/ScavTrap.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScavTrap.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 15:34:27 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 23:03:04 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SCAVTRAP_HPP +#define SCAVTRAP_HPP + +#include "ClapTrap.hpp" + +class ScavTrap : virtual public ClapTrap +{ + public: + ScavTrap(); + ScavTrap(std::string name); + ScavTrap(const ScavTrap ©); + ScavTrap & operator = (const ScavTrap &operator_aff); + ~ScavTrap(); + + void attack(const std::string& target); + void guardGate(); +}; + +#endif diff --git a/cpp03/ex03/main.cpp b/cpp03/ex03/main.cpp new file mode 100644 index 0000000..74fcdca --- /dev/null +++ b/cpp03/ex03/main.cpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 16:27:47 by fgras-ca #+# #+# */ +/* Updated: 2023/12/30 23:48:31 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ClapTrap.hpp" +#include "ScavTrap.hpp" +#include "FragTrap.hpp" +#include "DiamondTrap.hpp" + +int main() +{ + // Création des instances + ClapTrap claptrap_default; + //ScavTrap scavtrap_default; + //FragTrap fragtrap_dafault; + DiamondTrap diamondtrap_default; + + ClapTrap claptrap("Clappy"); + //ScavTrap scavtrap("Serena"); + //FragTrap fragtrap("Fraggy"); + DiamondTrap diamondtrap("Diamy"); + + diamondtrap_default = diamondtrap; + + claptrap.printStats(); + //scavtrap.printStats(); + //fragtrap.printStats(); + diamondtrap.printStats(); + + // Tests basiques pour ClapTrap + std::cout << "\n--- Tests ClapTrap ---" << std::endl; + claptrap.attack("un ennemi"); + claptrap.takeDamage(3); + claptrap.beRepaired(2); + + // Tests basiques pour ScavTrap + //std::cout << "\n--- Tests ScavTrap ---" << std::endl; + //scavtrap.attack("un autre ennemi"); + //scavtrap.takeDamage(15); + //scavtrap.beRepaired(10); + //scavtrap.guardGate(); + + // Tests basiques pour FragTrap + //std::cout << "\n--- Tests FragTrap ---" << std::endl; + //fragtrap.attack("un ennemi lointain"); + //fragtrap.takeDamage(20); + //fragtrap.beRepaired(15); + //fragtrap.highFivesGuys(); + + // Tests spécifiques pour DiamondTrap + std::cout << "\n--- Tests DiamondTrap ---" << std::endl; + diamondtrap.attack("un ennemi mystérieux"); // Doit utiliser l'attaque de ScavTrap + diamondtrap.takeDamage(40); + diamondtrap.beRepaired(30); + diamondtrap.guardGate(); // Doit utiliser la capacité de ScavTrap + diamondtrap.highFivesGuys(); // Doit utiliser la capacité de FragTrap + diamondtrap.whoAmI(); // Affiche les noms DiamondTrap et ClapTrap + + // Tests des limites de DiamondTrap + std::cout << "\n--- Tests des limites de DiamondTrap ---" << std::endl; + while (diamondtrap.getEnergyPoints() > 0) + { + diamondtrap.attack("un ennemi lointain"); + } + std::cout << "DiamondTrap essaie d'attaquer sans points d'énergie." << std::endl; + diamondtrap.attack("un ennemi lointain"); + + claptrap.printStats(); + //scavtrap.printStats(); + //fragtrap.printStats(); + diamondtrap.printStats(); + + diamondtrap.whoAmI(); + + DiamondTrap dt("Test"); + DiamondTrap dtt; + + dt.whoAmI(); + dtt = dt; + dtt.highFivesGuys(); + + return (0); +} diff --git a/cpp04/ex00/.vscode/c_cpp_properties.json b/cpp04/ex00/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp04/ex00/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp04/ex00/.vscode/launch.json b/cpp04/ex00/.vscode/launch.json new file mode 100644 index 0000000..37ccc12 --- /dev/null +++ b/cpp04/ex00/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex00", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex00/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp04/ex00/.vscode/settings.json b/cpp04/ex00/.vscode/settings.json new file mode 100644 index 0000000..a01459e --- /dev/null +++ b/cpp04/ex00/.vscode/settings.json @@ -0,0 +1,67 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "array": "cpp", + "compare": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/cpp04/ex00/Animal.cpp b/cpp04/ex00/Animal.cpp new file mode 100644 index 0000000..0dee674 --- /dev/null +++ b/cpp04/ex00/Animal.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 19:25:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:06:06 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" + +Animal::Animal() : type("Undefined") +{ + std::cout << GREEN << "Animal constructor called" << RESET << std::endl; +} + +Animal::Animal(const Animal ©) : type(copy.type) +{ + std::cout << GREEN << "Animal copy constructor called. Type is copied as " << type << "." << RESET << std::endl; +} + +Animal& Animal::operator=(const Animal &operator_aff)// Protection contre l'auto-affectation +{ + if (this != &operator_aff) + { + this->type = operator_aff.type; + } + std::cout << "Animal assignment operator called. Type is set to " << type << "." << std::endl; + return (*this); +} + +Animal::~Animal() +{ + std::cout << RED << "Animal destructor called" << RESET << std::endl; +} + +std::string Animal::getType() const +{ + return (this->type); +} + +void Animal::makeSound() const +{ + std::cout << MAGENTA << "Some animal sound!" << RESET << std::endl; +} diff --git a/cpp04/ex00/Animal.hpp b/cpp04/ex00/Animal.hpp new file mode 100644 index 0000000..c77614f --- /dev/null +++ b/cpp04/ex00/Animal.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 19:09:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:31:25 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ANIMAL_HPP +#define ANIMAL_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class Animal +{ + protected: + std::string type; + + public: + Animal(); + Animal(const Animal ©); + virtual Animal& operator=(const Animal &operator_aff); + virtual ~Animal(); + + std::string getType() const; + virtual void makeSound() const; +}; + +#endif diff --git a/cpp04/ex00/Cat.cpp b/cpp04/ex00/Cat.cpp new file mode 100644 index 0000000..7d73376 --- /dev/null +++ b/cpp04/ex00/Cat.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:52:48 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:54:23 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Cat.hpp" + +Cat::Cat() +{ + type = "Cat"; + std::cout << GREEN << "Cat default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +Cat::Cat(const Cat ©) : Animal(copy) +{ + std::cout << CYAN << "Cat copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +Cat& Cat::operator=(const Cat &operator_aff) +{ + Animal::operator=(operator_aff); + std::cout << MAGENTA << "Cat assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +Cat::~Cat() +{ + std::cout << RED << "Cat destructor called. Type was " << type << "." << RESET << std::endl; +} + +void Cat::makeSound() const +{ + std::cout << "Miaouhh! Miaouhh!" << std::endl; +} diff --git a/cpp04/ex00/Cat.hpp b/cpp04/ex00/Cat.hpp new file mode 100644 index 0000000..c89fef8 --- /dev/null +++ b/cpp04/ex00/Cat.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:17:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:18:06 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CAT_HPP +#define CAT_HPP + +#include "Animal.hpp" + +class Cat : public Animal +{ + public: + Cat(); + Cat(const Cat ©); + virtual Cat& operator=(const Cat &operator_aff); + virtual ~Cat(); + + void makeSound() const override; +}; + +#endif diff --git a/cpp04/ex00/Dog.cpp b/cpp04/ex00/Dog.cpp new file mode 100644 index 0000000..6521c13 --- /dev/null +++ b/cpp04/ex00/Dog.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:50:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:54:33 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Dog.hpp" + +Dog::Dog() +{ + type = "Dog"; + std::cout << GREEN << "Dog default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +Dog::Dog(const Dog ©) : Animal(copy) +{ + std::cout << CYAN << "Dog copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +Dog& Dog::operator=(const Dog &operator_aff) +{ + Animal::operator=(operator_aff); + std::cout << MAGENTA << "Dog assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +Dog::~Dog() +{ + std::cout << RED << "Dog destructor called. Type was " << type << "." << RESET << std::endl; +} + +void Dog::makeSound() const +{ + std::cout << "Woof! Woof!" << std::endl; +} diff --git a/cpp04/ex00/Dog.hpp b/cpp04/ex00/Dog.hpp new file mode 100644 index 0000000..a038395 --- /dev/null +++ b/cpp04/ex00/Dog.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:06:42 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:16:31 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DOG_HPP +#define DOG_HPP + +#include "Animal.hpp" + +class Dog : public Animal +{ + public: + Dog(); + Dog(const Dog ©); + virtual Dog& operator=(const Dog &operator_aff); + virtual ~Dog(); + + void makeSound() const override; +}; + +#endif diff --git a/cpp04/ex00/Makefile b/cpp04/ex00/Makefile new file mode 100644 index 0000000..ddec2e7 --- /dev/null +++ b/cpp04/ex00/Makefile @@ -0,0 +1,66 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/31 13:03:33 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\ + echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\ + echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\ + echo "( ( ( ( ( ( ( (" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = animal + +SRC = Animal.cpp \ + Cat.cpp \ + Dog.cpp \ + WrongAnimal.cpp \ + WrongCat.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./animal" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp04/ex00/WrongAnimal.cpp b/cpp04/ex00/WrongAnimal.cpp new file mode 100644 index 0000000..10d0e0b --- /dev/null +++ b/cpp04/ex00/WrongAnimal.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:55:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:57:57 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongAnimal.hpp" + +WrongAnimal::WrongAnimal() : type("WrongAnimal") +{ + std::cout << GREEN << "WrongAnimal default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +WrongAnimal::WrongAnimal(const WrongAnimal ©) : type(copy.type) +{ + std::cout << CYAN << "WrongAnimal copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +WrongAnimal& WrongAnimal::operator=(const WrongAnimal &operator_aff) +{ + if (this != &operator_aff) + { + this->type = operator_aff.type; + } + std::cout << MAGENTA << "WrongAnimal assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +WrongAnimal::~WrongAnimal() +{ + std::cout << RED << "WrongAnimal destructor called. Type was " << type << "." << RESET << std::endl; +} + +std::string WrongAnimal::getType() const +{ + return (this->type); +} + +void WrongAnimal::makeSound() const +{ + std::cout << "WrongAnimal makes a wrong sound!" << std::endl; +} diff --git a/cpp04/ex00/WrongAnimal.hpp b/cpp04/ex00/WrongAnimal.hpp new file mode 100644 index 0000000..1a0e2b2 --- /dev/null +++ b/cpp04/ex00/WrongAnimal.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:19:34 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:55:37 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGANIMAL_HPP +#define WRONGANIMAL_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class WrongAnimal +{ + protected: + std::string type; + + public: + WrongAnimal(); + WrongAnimal(const WrongAnimal ©); + WrongAnimal& operator=(const WrongAnimal &operator_aff); + ~WrongAnimal(); + + std::string getType() const; + void makeSound() const; +}; + +#endif diff --git a/cpp04/ex00/WrongCat.cpp b/cpp04/ex00/WrongCat.cpp new file mode 100644 index 0000000..d12add2 --- /dev/null +++ b/cpp04/ex00/WrongCat.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:58:15 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:59:48 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongCat.hpp" + +WrongCat::WrongCat() +{ + type = "WrongCat"; + std::cout << GREEN << "WrongCat default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +WrongCat::WrongCat(const WrongCat ©) : WrongAnimal(copy) +{ + std::cout << CYAN << "WrongCat copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +WrongCat& WrongCat::operator=(const WrongCat &operator_aff) +{ + WrongAnimal::operator=(operator_aff); + std::cout << MAGENTA << "WrongCat assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +WrongCat::~WrongCat() +{ + std::cout << RED << "WrongCat destructor called. Type was " << type << "." << RESET << std::endl; +} + +void WrongCat::makeSound() const +{ + std::cout << "WrongCat makes a wrong sound!" << std::endl; +} diff --git a/cpp04/ex00/WrongCat.hpp b/cpp04/ex00/WrongCat.hpp new file mode 100644 index 0000000..83051bd --- /dev/null +++ b/cpp04/ex00/WrongCat.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:22:35 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:31:44 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGCAT_HPP +#define WRONGCAT_HPP + +#include "WrongAnimal.hpp" + +class WrongCat : public WrongAnimal +{ + public: + WrongCat(); + WrongCat(const WrongCat ©); + WrongCat& operator=(const WrongCat &operator_aff); + ~WrongCat(); + + void makeSound() const; +}; + +#endif diff --git a/cpp04/ex00/main.cpp b/cpp04/ex00/main.cpp new file mode 100644 index 0000000..fd22c31 --- /dev/null +++ b/cpp04/ex00/main.cpp @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 13:00:09 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 13:19:56 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" +#include "WrongAnimal.hpp" +#include "WrongCat.hpp" + +int main() +{ + const Animal* meta = new Animal(); + const Animal* j = new Dog(); + const Animal* i = new Cat(); + + std::cout << j->getType() << " " << std::endl; + std::cout << i->getType() << " " << std::endl; + i->makeSound(); + j->makeSound(); + meta->makeSound(); + + delete meta; + delete j; + delete i; + + std::cout << "\n--- Tests WrongAnimal & WrongCat ---\n" << std::endl; + + const WrongAnimal* wrongMeta = new WrongAnimal(); + const WrongAnimal* wrongCat = new WrongCat(); + + std::cout << wrongCat->getType() << " " << std::endl; + wrongCat->makeSound(); + wrongMeta->makeSound(); + + delete wrongMeta; + delete wrongCat; + + std::cout << "\n--- Tests supplémentaires pour Dog & Cat ---\n" << std::endl; + + Dog dog1, dog2; + Cat cat1, cat2; + + dog1.makeSound(); + cat1.makeSound(); + + Dog dog3(dog1); + dog3.makeSound(); + + cat2 = cat1; + cat2.makeSound(); + + return (0); +} diff --git a/cpp04/ex01/.vscode/c_cpp_properties.json b/cpp04/ex01/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp04/ex01/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp04/ex01/.vscode/launch.json b/cpp04/ex01/.vscode/launch.json new file mode 100644 index 0000000..cf7a0a0 --- /dev/null +++ b/cpp04/ex01/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex01", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex01/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp04/ex01/.vscode/settings.json b/cpp04/ex01/.vscode/settings.json new file mode 100644 index 0000000..ebd8700 --- /dev/null +++ b/cpp04/ex01/.vscode/settings.json @@ -0,0 +1,105 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/cpp04/ex01/Animal.cpp b/cpp04/ex01/Animal.cpp new file mode 100644 index 0000000..0dee674 --- /dev/null +++ b/cpp04/ex01/Animal.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 19:25:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:06:06 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" + +Animal::Animal() : type("Undefined") +{ + std::cout << GREEN << "Animal constructor called" << RESET << std::endl; +} + +Animal::Animal(const Animal ©) : type(copy.type) +{ + std::cout << GREEN << "Animal copy constructor called. Type is copied as " << type << "." << RESET << std::endl; +} + +Animal& Animal::operator=(const Animal &operator_aff)// Protection contre l'auto-affectation +{ + if (this != &operator_aff) + { + this->type = operator_aff.type; + } + std::cout << "Animal assignment operator called. Type is set to " << type << "." << std::endl; + return (*this); +} + +Animal::~Animal() +{ + std::cout << RED << "Animal destructor called" << RESET << std::endl; +} + +std::string Animal::getType() const +{ + return (this->type); +} + +void Animal::makeSound() const +{ + std::cout << MAGENTA << "Some animal sound!" << RESET << std::endl; +} diff --git a/cpp04/ex01/Animal.hpp b/cpp04/ex01/Animal.hpp new file mode 100644 index 0000000..e54cd80 --- /dev/null +++ b/cpp04/ex01/Animal.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Animal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 19:09:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:32:05 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ANIMAL_HPP +#define ANIMAL_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class Animal +{ + protected: + std::string type; + + public: + Animal(); + Animal(const Animal ©); + virtual Animal& operator=(const Animal &operator_aff); + virtual ~Animal(); + + std::string getType() const; + virtual void makeSound() const; +}; + +#endif diff --git a/cpp04/ex01/Brain.cpp b/cpp04/ex01/Brain.cpp new file mode 100644 index 0000000..1de362b --- /dev/null +++ b/cpp04/ex01/Brain.cpp @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 14:06:58 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 15:09:21 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Brain.hpp" + +Brain::Brain() +{ + // Initialisation des idées + for (int i = 0; i < 100; ++i) + { + ideas[i] = "Idea " + std::to_string(i); + //std::cout << GREEN << "Initialising Brain Idea #" << i << ": " << ideas[i] << RESET << std::endl; + } + std::cout << GREEN << "Brain constructed with 100 ideas." << RESET << std::endl; +} + +Brain::Brain(const Brain ©) +{ + *this = copy; + std::cout << CYAN << "Brain copy constructor called." << RESET << std::endl; +} + +Brain& Brain::operator=(const Brain &operator_aff) +{ + std::cout << MAGENTA << "Brain assignment operator called." << RESET << std::endl; + if (this != &operator_aff) + { + for (int i = 0; i < 100; ++i) + { + this->ideas[i] = operator_aff.ideas[i]; + std::cout << "Copying Idea #" << i << ": " << this->ideas[i] << std::endl; + } + } + return (*this); +} + +Brain::~Brain() +{ + std::cout << RED << "Brain destructor called" << RESET << std::endl; +} + +void Brain::setIdea(std::string idea, int i) +{ + this->ideas[i]=idea; +} + +std::string Brain::getIdea(int i) const +{ + return (this->ideas[i]); +} diff --git a/cpp04/ex01/Brain.hpp b/cpp04/ex01/Brain.hpp new file mode 100644 index 0000000..09880c3 --- /dev/null +++ b/cpp04/ex01/Brain.hpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 14:01:08 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:32:17 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BRAIN_HPP +#define BRAIN_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class Brain +{ + private: + std::string ideas[100]; + + public: + Brain(); + Brain(const Brain ©); + Brain& operator=(const Brain &operator_aff); + ~Brain(); + void setIdea(std::string idea, int i); + std::string getIdea(int i) const; +}; + +#endif diff --git a/cpp04/ex01/Cat.cpp b/cpp04/ex01/Cat.cpp new file mode 100644 index 0000000..3d0833a --- /dev/null +++ b/cpp04/ex01/Cat.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:52:48 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 15:26:47 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Cat.hpp" + +Cat::Cat() +{ + this->type = "Cat"; + this->brain = new Brain(); + std::cout << GREEN << "Cat default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +Cat::Cat(const Cat ©) : Animal(copy), brain(new Brain(*copy.brain)) +{ + std::cout << CYAN << "Cat copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +Cat& Cat::operator=(const Cat &operator_aff) +{ + Animal::operator=(operator_aff); + std::cout << MAGENTA << "Cat assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +Cat::~Cat() +{ + std::cout << RED << "Cat destructor called. Type was " << type << "." << RESET << std::endl; +} + +void Cat::makeSound() const +{ + std::cout << "Miaouhh! Miaouhh!" << std::endl; +} + +Brain* Cat::getBrain(void) const +{ + return (this->brain); +} diff --git a/cpp04/ex01/Cat.hpp b/cpp04/ex01/Cat.hpp new file mode 100644 index 0000000..11e5c20 --- /dev/null +++ b/cpp04/ex01/Cat.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:17:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 15:11:08 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CAT_HPP +#define CAT_HPP + +#include "Animal.hpp" +#include "Brain.hpp" + +class Cat : public Animal +{ + private: + Brain* brain; + + public: + Cat(); + Cat(const Cat ©); + virtual Cat& operator=(const Cat &operator_aff); + virtual ~Cat(); + + void makeSound() const override; + Brain* getBrain(void) const; +}; + +#endif diff --git a/cpp04/ex01/Dog.cpp b/cpp04/ex01/Dog.cpp new file mode 100644 index 0000000..e4f8c64 --- /dev/null +++ b/cpp04/ex01/Dog.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:50:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 15:28:27 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Dog.hpp" + +Dog::Dog() +{ + this->type = "Dog"; + this->brain = new Brain(); + std::cout << GREEN << "Dog default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +Dog::Dog(const Dog ©) : Animal(copy), brain(new Brain(*copy.brain)) +{ + std::cout << CYAN << "Dog copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +Dog& Dog::operator=(const Dog &operator_aff) +{ + Animal::operator=(operator_aff); + std::cout << MAGENTA << "Dog assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +Dog::~Dog() +{ + std::cout << RED << "Dog destructor called. Type was " << type << "." << RESET << std::endl; +} + +void Dog::makeSound() const +{ + std::cout << "Woof! Woof!" << std::endl; +} + +Brain* Dog::getBrain(void) const +{ + return (this->brain); +} \ No newline at end of file diff --git a/cpp04/ex01/Dog.hpp b/cpp04/ex01/Dog.hpp new file mode 100644 index 0000000..ba9ee2e --- /dev/null +++ b/cpp04/ex01/Dog.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:06:42 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 15:12:01 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DOG_HPP +#define DOG_HPP + +#include "Animal.hpp" +#include "Brain.hpp" + +class Dog : public Animal +{ + private: + Brain* brain; + + public: + Dog(); + Dog(const Dog ©); + virtual Dog& operator=(const Dog &operator_aff); + virtual ~Dog(); + + void makeSound() const override; + Brain* getBrain(void) const; +}; + +#endif diff --git a/cpp04/ex01/Makefile b/cpp04/ex01/Makefile new file mode 100644 index 0000000..a593587 --- /dev/null +++ b/cpp04/ex01/Makefile @@ -0,0 +1,73 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/31 14:46:49 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " ██▓ ▄▄▄ ▓█████▄▓█████ ▄▄▄▄ ▓█████▒███████▓█████";\ + echo " ▓██▒ ▒████▄ ▒██▀ ██▓█ ▀▓█████▄▓█ ▀▒ ▒ ▒ ▄▀▓█ ▀";\ + echo " ▒██░ ▒██ ▀█▄ ░██ █▒███ ▒██▒ ▄█▒███ ░ ▒ ▄▀▒░▒███";\ + echo " ▒██░ ░██▄▄▄▄██░▓█▄ ▒▓█ ▄▒██░█▀ ▒▓█ ▄ ▄▀▒ ▒▓█ ▄";\ + echo " ░██████▓█ ▓██░▒████▓░▒████░▓█ ▀█░▒████▒███████░▒████▒";\ + echo " ░ ▒░▓ ▒▒ ▓▒█░▒▒▓ ▒░░ ▒░ ░▒▓███▀░░ ▒░ ░▒▒ ▓░▒░░░ ▒░ ░";\ + echo " ░ ░ ▒ ░▒ ▒▒ ░░ ▒ ▒ ░ ░ ▒░▒ ░ ░ ░ ░░▒ ▒ ░ ▒░ ░ ░";\ + echo " ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░";\ + echo " ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ ░";\ + echo " ░ ░ ░" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = brain + +SRC = Animal.cpp \ + Cat.cpp \ + Dog.cpp \ + WrongAnimal.cpp \ + WrongCat.cpp \ + Brain.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./brain" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp04/ex01/WrongAnimal.cpp b/cpp04/ex01/WrongAnimal.cpp new file mode 100644 index 0000000..10d0e0b --- /dev/null +++ b/cpp04/ex01/WrongAnimal.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:55:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:57:57 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongAnimal.hpp" + +WrongAnimal::WrongAnimal() : type("WrongAnimal") +{ + std::cout << GREEN << "WrongAnimal default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +WrongAnimal::WrongAnimal(const WrongAnimal ©) : type(copy.type) +{ + std::cout << CYAN << "WrongAnimal copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +WrongAnimal& WrongAnimal::operator=(const WrongAnimal &operator_aff) +{ + if (this != &operator_aff) + { + this->type = operator_aff.type; + } + std::cout << MAGENTA << "WrongAnimal assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +WrongAnimal::~WrongAnimal() +{ + std::cout << RED << "WrongAnimal destructor called. Type was " << type << "." << RESET << std::endl; +} + +std::string WrongAnimal::getType() const +{ + return (this->type); +} + +void WrongAnimal::makeSound() const +{ + std::cout << "WrongAnimal makes a wrong sound!" << std::endl; +} diff --git a/cpp04/ex01/WrongAnimal.hpp b/cpp04/ex01/WrongAnimal.hpp new file mode 100644 index 0000000..1a0e2b2 --- /dev/null +++ b/cpp04/ex01/WrongAnimal.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:19:34 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:55:37 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGANIMAL_HPP +#define WRONGANIMAL_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class WrongAnimal +{ + protected: + std::string type; + + public: + WrongAnimal(); + WrongAnimal(const WrongAnimal ©); + WrongAnimal& operator=(const WrongAnimal &operator_aff); + ~WrongAnimal(); + + std::string getType() const; + void makeSound() const; +}; + +#endif diff --git a/cpp04/ex01/WrongCat.cpp b/cpp04/ex01/WrongCat.cpp new file mode 100644 index 0000000..d12add2 --- /dev/null +++ b/cpp04/ex01/WrongCat.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:58:15 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:59:48 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongCat.hpp" + +WrongCat::WrongCat() +{ + type = "WrongCat"; + std::cout << GREEN << "WrongCat default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +WrongCat::WrongCat(const WrongCat ©) : WrongAnimal(copy) +{ + std::cout << CYAN << "WrongCat copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +WrongCat& WrongCat::operator=(const WrongCat &operator_aff) +{ + WrongAnimal::operator=(operator_aff); + std::cout << MAGENTA << "WrongCat assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +WrongCat::~WrongCat() +{ + std::cout << RED << "WrongCat destructor called. Type was " << type << "." << RESET << std::endl; +} + +void WrongCat::makeSound() const +{ + std::cout << "WrongCat makes a wrong sound!" << std::endl; +} diff --git a/cpp04/ex01/WrongCat.hpp b/cpp04/ex01/WrongCat.hpp new file mode 100644 index 0000000..c65deeb --- /dev/null +++ b/cpp04/ex01/WrongCat.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:22:35 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:32:31 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGCAT_HPP +#define WRONGCAT_HPP + +#include "WrongAnimal.hpp" + +class WrongCat : public WrongAnimal +{ + public: + WrongCat(); + WrongCat(const WrongCat ©); + WrongCat& operator=(const WrongCat &operator_aff); + ~WrongCat(); + + void makeSound() const; +}; + +#endif diff --git a/cpp04/ex01/main.cpp b/cpp04/ex01/main.cpp new file mode 100644 index 0000000..c017596 --- /dev/null +++ b/cpp04/ex01/main.cpp @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 13:00:09 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 15:30:45 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Animal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" + +int main() +{ + const Animal* j = new Dog(); + const Animal* i = new Cat(); + + delete j; + delete i; + + Animal* animals[10]; + + for (int i = 0; i < 10; i++) + { + if (i < 5) + { + animals[i] = new Dog(); + } + else + { + animals[i] = new Cat(); + } + } + + // Suppression des animaux + for (int i = 0; i < 10; i++) + { + delete animals[i]; + } + + // Test de copie profonde + Dog originalDog; + Dog copyDog(originalDog); + + std::cout << "\n--- Test Deep Copy for Cat ---\n" << std::endl; + + Cat originalCat; + originalCat.getBrain()->setIdea("Jouer avec une balle", 0); // Définir une idée + + // Copie profonde + Cat copyCat(originalCat); + + // Modifier l'original après la copie + originalCat.getBrain()->setIdea("Dormir sur le canapé", 0); + + // Afficher les idées pour vérifier la copie profonde + std::cout << "Original Cat's Idea #0: " << originalCat.getBrain()->getIdea(0) << std::endl; + std::cout << "Copy Cat's Idea #0: " << copyCat.getBrain()->getIdea(0) << std::endl; + + if (originalCat.getBrain()->getIdea(0) != copyCat.getBrain()->getIdea(0)) + { + std::cout << "Deep Copy Test Passed: Modifications to the original do not affect the copy." << std::endl; + } + else + { + std::cout << "Deep Copy Test Failed: Modifications to the original affect the copy." << std::endl; + } + + return (0); +} diff --git a/cpp04/ex02/.vscode/c_cpp_properties.json b/cpp04/ex02/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp04/ex02/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp04/ex02/.vscode/launch.json b/cpp04/ex02/.vscode/launch.json new file mode 100644 index 0000000..2898211 --- /dev/null +++ b/cpp04/ex02/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex02", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex02/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp04/ex02/.vscode/settings.json b/cpp04/ex02/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp04/ex02/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp04/ex02/AAnimal.cpp b/cpp04/ex02/AAnimal.cpp new file mode 100644 index 0000000..d03b8b6 --- /dev/null +++ b/cpp04/ex02/AAnimal.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AAnimal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 19:25:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:18:10 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "AAnimal.hpp" + +AAnimal::AAnimal() : type("Undefined") +{ + std::cout << GREEN << "AAnimal constructor called" << RESET << std::endl; +} + +AAnimal::AAnimal(const AAnimal ©) : type(copy.type) +{ + std::cout << GREEN << "AAnimal copy constructor called. Type is copied as " << type << "." << RESET << std::endl; +} + +AAnimal& AAnimal::operator=(const AAnimal &operator_aff)// Protection contre l'auto-affectation +{ + if (this != &operator_aff) + { + this->type = operator_aff.type; + } + std::cout << "AAnimal assignment operator called. Type is set to " << type << "." << std::endl; + return (*this); +} + +AAnimal::~AAnimal() +{ + std::cout << RED << "AAnimal destructor called" << RESET << std::endl; +} + +std::string AAnimal::getType() const +{ + return (this->type); +} + +void AAnimal::makeSound() const +{ + std::cout << MAGENTA << "Some animal sound!" << RESET << std::endl; +} diff --git a/cpp04/ex02/AAnimal.hpp b/cpp04/ex02/AAnimal.hpp new file mode 100644 index 0000000..48a15c5 --- /dev/null +++ b/cpp04/ex02/AAnimal.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AAnimal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 19:09:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:19:56 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef AANIMAL_HPP +#define AANIMAL_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class AAnimal +{ + protected: + std::string type; + + public: + AAnimal(); + AAnimal(const AAnimal ©); + virtual AAnimal& operator=(const AAnimal &operator_aff); + virtual ~AAnimal(); + + std::string getType() const; + virtual void makeSound() const = 0; +}; + +#endif diff --git a/cpp04/ex02/Brain.cpp b/cpp04/ex02/Brain.cpp new file mode 100644 index 0000000..a97bb33 --- /dev/null +++ b/cpp04/ex02/Brain.cpp @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 14:06:58 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:30:23 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Brain.hpp" + +Brain::Brain() +{ + // Initialisation des idées + for (int i = 0; i < 100; ++i) + { + ideas[i] = "Idea " + std::to_string(i); + //std::cout << GREEN << "Initialising Brain Idea #" << i << ": " << ideas[i] << RESET << std::endl; + } + std::cout << GREEN << "Brain constructed with 100 ideas." << RESET << std::endl; +} + +Brain::Brain(const Brain ©) +{ + *this = copy; + std::cout << CYAN << "Brain copy constructor called." << RESET << std::endl; +} + +Brain& Brain::operator=(const Brain &operator_aff) +{ + std::cout << MAGENTA << "Brain assignment operator called." << RESET << std::endl; + if (this != &operator_aff) + { + for (int i = 0; i < 100; ++i) + { + this->ideas[i] = operator_aff.ideas[i]; + //std::cout << "Copying Idea #" << i << ": " << this->ideas[i] << std::endl; + } + } + return (*this); +} + +Brain::~Brain() +{ + std::cout << RED << "Brain destructor called" << RESET << std::endl; +} + +void Brain::setIdea(std::string idea, int i) +{ + this->ideas[i]=idea; +} + +std::string Brain::getIdea(int i) const +{ + return (this->ideas[i]); +} diff --git a/cpp04/ex02/Brain.hpp b/cpp04/ex02/Brain.hpp new file mode 100644 index 0000000..19f039c --- /dev/null +++ b/cpp04/ex02/Brain.hpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Brain.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 14:01:08 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:18:37 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BRAIN_HPP +#define BRAIN_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class Brain +{ + private: + std::string ideas[100]; + + public: + Brain(); + Brain(const Brain ©); + Brain& operator=(const Brain &operator_aff); + ~Brain(); + void setIdea(std::string idea, int i); + std::string getIdea(int i) const; +}; + +#endif diff --git a/cpp04/ex02/Cat.cpp b/cpp04/ex02/Cat.cpp new file mode 100644 index 0000000..0876639 --- /dev/null +++ b/cpp04/ex02/Cat.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:52:48 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:19:07 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Cat.hpp" + +Cat::Cat() +{ + this->type = "Cat"; + this->brain = new Brain(); + std::cout << GREEN << "Cat default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +Cat::Cat(const Cat ©) : AAnimal(copy), brain(new Brain(*copy.brain)) +{ + std::cout << CYAN << "Cat copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +Cat& Cat::operator=(const Cat &operator_aff) +{ + AAnimal::operator=(operator_aff); + std::cout << MAGENTA << "Cat assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +Cat::~Cat() +{ + std::cout << RED << "Cat destructor called. Type was " << type << "." << RESET << std::endl; +} + +void Cat::makeSound() const +{ + std::cout << "Miaouhh! Miaouhh!" << std::endl; +} + +Brain* Cat::getBrain(void) const +{ + return (this->brain); +} diff --git a/cpp04/ex02/Cat.hpp b/cpp04/ex02/Cat.hpp new file mode 100644 index 0000000..e73a111 --- /dev/null +++ b/cpp04/ex02/Cat.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:17:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:20:30 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CAT_HPP +#define CAT_HPP + +#include "AAnimal.hpp" +#include "Brain.hpp" + +class Cat : public AAnimal +{ + private: + Brain* brain; + + public: + Cat(); + Cat(const Cat ©); + virtual Cat& operator=(const Cat &operator_aff); + virtual ~Cat(); + + void makeSound() const override; + Brain* getBrain(void) const; +}; + +#endif diff --git a/cpp04/ex02/Dog.cpp b/cpp04/ex02/Dog.cpp new file mode 100644 index 0000000..82344dc --- /dev/null +++ b/cpp04/ex02/Dog.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:50:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:19:31 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Dog.hpp" + +Dog::Dog() +{ + this->type = "Dog"; + this->brain = new Brain(); + std::cout << GREEN << "Dog default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +Dog::Dog(const Dog ©) : AAnimal(copy), brain(new Brain(*copy.brain)) +{ + std::cout << CYAN << "Dog copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +Dog& Dog::operator=(const Dog &operator_aff) +{ + AAnimal::operator=(operator_aff); + std::cout << MAGENTA << "Dog assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +Dog::~Dog() +{ + std::cout << RED << "Dog destructor called. Type was " << type << "." << RESET << std::endl; +} + +void Dog::makeSound() const +{ + std::cout << "Woof! Woof!" << std::endl; +} + +Brain* Dog::getBrain(void) const +{ + return (this->brain); +} \ No newline at end of file diff --git a/cpp04/ex02/Dog.hpp b/cpp04/ex02/Dog.hpp new file mode 100644 index 0000000..0fb1fe6 --- /dev/null +++ b/cpp04/ex02/Dog.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Dog.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:06:42 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:20:45 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DOG_HPP +#define DOG_HPP + +#include "AAnimal.hpp" +#include "Brain.hpp" + +class Dog : public AAnimal +{ + private: + Brain* brain; + + public: + Dog(); + Dog(const Dog ©); + virtual Dog& operator=(const Dog &operator_aff); + virtual ~Dog(); + + void makeSound() const override; + Brain* getBrain(void) const; +}; + +#endif diff --git a/cpp04/ex02/Makefile b/cpp04/ex02/Makefile new file mode 100644 index 0000000..9af85e4 --- /dev/null +++ b/cpp04/ex02/Makefile @@ -0,0 +1,73 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/31 16:25:51 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " ██▓ ▄▄▄ ▓█████▄▓█████ ▄▄▄▄ ▓█████▒███████▓█████";\ + echo " ▓██▒ ▒████▄ ▒██▀ ██▓█ ▀▓█████▄▓█ ▀▒ ▒ ▒ ▄▀▓█ ▀";\ + echo " ▒██░ ▒██ ▀█▄ ░██ █▒███ ▒██▒ ▄█▒███ ░ ▒ ▄▀▒░▒███";\ + echo " ▒██░ ░██▄▄▄▄██░▓█▄ ▒▓█ ▄▒██░█▀ ▒▓█ ▄ ▄▀▒ ▒▓█ ▄";\ + echo " ░██████▓█ ▓██░▒████▓░▒████░▓█ ▀█░▒████▒███████░▒████▒";\ + echo " ░ ▒░▓ ▒▒ ▓▒█░▒▒▓ ▒░░ ▒░ ░▒▓███▀░░ ▒░ ░▒▒ ▓░▒░░░ ▒░ ░";\ + echo " ░ ░ ▒ ░▒ ▒▒ ░░ ▒ ▒ ░ ░ ▒░▒ ░ ░ ░ ░░▒ ▒ ░ ▒░ ░ ░";\ + echo " ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░";\ + echo " ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ ░";\ + echo " ░ ░ ░" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = abstract + +SRC = AAnimal.cpp \ + Cat.cpp \ + Dog.cpp \ + WrongAnimal.cpp \ + WrongCat.cpp \ + Brain.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./abstract" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp04/ex02/WrongAnimal.cpp b/cpp04/ex02/WrongAnimal.cpp new file mode 100644 index 0000000..10d0e0b --- /dev/null +++ b/cpp04/ex02/WrongAnimal.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:55:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:57:57 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongAnimal.hpp" + +WrongAnimal::WrongAnimal() : type("WrongAnimal") +{ + std::cout << GREEN << "WrongAnimal default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +WrongAnimal::WrongAnimal(const WrongAnimal ©) : type(copy.type) +{ + std::cout << CYAN << "WrongAnimal copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +WrongAnimal& WrongAnimal::operator=(const WrongAnimal &operator_aff) +{ + if (this != &operator_aff) + { + this->type = operator_aff.type; + } + std::cout << MAGENTA << "WrongAnimal assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +WrongAnimal::~WrongAnimal() +{ + std::cout << RED << "WrongAnimal destructor called. Type was " << type << "." << RESET << std::endl; +} + +std::string WrongAnimal::getType() const +{ + return (this->type); +} + +void WrongAnimal::makeSound() const +{ + std::cout << "WrongAnimal makes a wrong sound!" << std::endl; +} diff --git a/cpp04/ex02/WrongAnimal.hpp b/cpp04/ex02/WrongAnimal.hpp new file mode 100644 index 0000000..1a0e2b2 --- /dev/null +++ b/cpp04/ex02/WrongAnimal.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongAnimal.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:19:34 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:55:37 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGANIMAL_HPP +#define WRONGANIMAL_HPP + +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +class WrongAnimal +{ + protected: + std::string type; + + public: + WrongAnimal(); + WrongAnimal(const WrongAnimal ©); + WrongAnimal& operator=(const WrongAnimal &operator_aff); + ~WrongAnimal(); + + std::string getType() const; + void makeSound() const; +}; + +#endif diff --git a/cpp04/ex02/WrongCat.cpp b/cpp04/ex02/WrongCat.cpp new file mode 100644 index 0000000..d12add2 --- /dev/null +++ b/cpp04/ex02/WrongCat.cpp @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:58:15 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 12:59:48 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "WrongCat.hpp" + +WrongCat::WrongCat() +{ + type = "WrongCat"; + std::cout << GREEN << "WrongCat default constructor called. Type is set to " << type << "." << RESET << std::endl; +} + +WrongCat::WrongCat(const WrongCat ©) : WrongAnimal(copy) +{ + std::cout << CYAN << "WrongCat copy constructor called. Type is " << type << "." << RESET << std::endl; +} + +WrongCat& WrongCat::operator=(const WrongCat &operator_aff) +{ + WrongAnimal::operator=(operator_aff); + std::cout << MAGENTA << "WrongCat assignment operator called. Type is now " << type << "." << RESET << std::endl; + return (*this); +} + +WrongCat::~WrongCat() +{ + std::cout << RED << "WrongCat destructor called. Type was " << type << "." << RESET << std::endl; +} + +void WrongCat::makeSound() const +{ + std::cout << "WrongCat makes a wrong sound!" << std::endl; +} diff --git a/cpp04/ex02/WrongCat.hpp b/cpp04/ex02/WrongCat.hpp new file mode 100644 index 0000000..37e9951 --- /dev/null +++ b/cpp04/ex02/WrongCat.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WrongCat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:22:35 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:21:11 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRONGCAT_HPP +#define WRONGCAT_HPP + +#include "WrongAnimal.hpp" + +class WrongCat : public WrongAnimal +{ + public: + WrongCat(); + WrongCat(const WrongCat ©); + WrongCat& operator=(const WrongCat &operator_aff); + ~WrongCat(); + + void makeSound() const; +}; + +#endif diff --git a/cpp04/ex02/main.cpp b/cpp04/ex02/main.cpp new file mode 100644 index 0000000..facabed --- /dev/null +++ b/cpp04/ex02/main.cpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 13:00:09 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:28:40 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "AAnimal.hpp" +#include "Dog.hpp" +#include "Cat.hpp" +// Ajoutez d'autres inclusions si nécessaire + +int main() +{ + // Tentative d'instanciation d'un Animal (doit échouer) + // AAnimal animal; // Décommentez pour vérifier que cela génère une erreur de compilation. + + // Création d'un tableau d'animaux + const int numAnimals = 4; + AAnimal* animals[numAnimals]; + + // Remplir le tableau avec des Dogs et Cats + for (int i = 0; i < numAnimals; ++i) + { + if (i % 2 == 0) + { + animals[i] = new Dog(); // Utilise Dog pour les indices pairs + } + else + { + animals[i] = new Cat(); // Utilise Cat pour les indices impairs + } + } + + // Faire du bruit avec chaque animal + for (int i = 0; i < numAnimals; ++i) + { + std::cout << "Animal #" << i + 1 << " sound: "; + animals[i]->makeSound(); + } + + // Libérer la mémoire allouée pour les animaux + for (int i = 0; i < numAnimals; ++i) + { + delete animals[i]; + } + + // Tests supplémentaires pour la copie profonde + std::cout << "\n--- Test Deep Copy ---\n"; + Dog originalDog; + Dog copyDog(originalDog); // Utiliser le constructeur de copie + originalDog.makeSound(); + copyDog.makeSound(); + std::cout << "\n--- Test Deep Copy for Cat ---\n" << std::endl; + + Cat originalCat; + originalCat.getBrain()->setIdea("Jouer avec une balle", 0); // Définir une idée + + // Copie profonde + Cat copyCat(originalCat); + + // Modifier l'original après la copie + originalCat.getBrain()->setIdea("Dormir sur le canapé", 0); + + // Afficher les idées pour vérifier la copie profonde + std::cout << "Original Cat's Idea #0: " << originalCat.getBrain()->getIdea(0) << std::endl; + std::cout << "Copy Cat's Idea #0: " << copyCat.getBrain()->getIdea(0) << std::endl; + + if (originalCat.getBrain()->getIdea(0) != copyCat.getBrain()->getIdea(0)) + { + std::cout << "Deep Copy Test Passed: Modifications to the original do not affect the copy." << std::endl; + } + else + { + std::cout << "Deep Copy Test Failed: Modifications to the original affect the copy." << std::endl; + } + + // Test de l'assignation + Dog anotherDog; + anotherDog = originalDog; // Utiliser l'opérateur d'assignation + anotherDog.makeSound(); + + + + return (0); +} diff --git a/cpp04/ex03/.vscode/c_cpp_properties.json b/cpp04/ex03/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp04/ex03/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp04/ex03/.vscode/launch.json b/cpp04/ex03/.vscode/launch.json new file mode 100644 index 0000000..c57c86a --- /dev/null +++ b/cpp04/ex03/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex03", + "program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp04/ex03/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp04/ex03/.vscode/settings.json b/cpp04/ex03/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/cpp04/ex03/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp04/ex03/AMateria.cpp b/cpp04/ex03/AMateria.cpp new file mode 100644 index 0000000..0676ff5 --- /dev/null +++ b/cpp04/ex03/AMateria.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AMateria.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 16:47:27 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:48:56 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "AMateria.hpp" + +AMateria::AMateria(std::string const & type) : type(type) {} + +AMateria::~AMateria() {} + +std::string const & AMateria::getType() const +{ + return (this->type); +} + +void AMateria::use(ICharacter& target) +{ + std::cout << "* uses some unknown materia on " << target.getName() << " *" << std::endl; +} diff --git a/cpp04/ex03/AMateria.hpp b/cpp04/ex03/AMateria.hpp new file mode 100644 index 0000000..6ecf210 --- /dev/null +++ b/cpp04/ex03/AMateria.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AMateria.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 16:44:47 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:09:18 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef AMATERIA_HPP +#define AMATERIA_HPP + +#include "ICharacter.hpp" +#include +#include + +class AMateria +{ + protected: + std::string type; + + public: + AMateria(std::string const & type); + virtual ~AMateria(); + + std::string const & getType() const; // Renvoie le type de la materia + virtual AMateria* clone() const = 0; // Doit être implémenté dans les sous-classes + virtual void use(ICharacter& target); // Peut être redéfini dans les sous-classes +}; + +#endif diff --git a/cpp04/ex03/AMateria.o b/cpp04/ex03/AMateria.o new file mode 100644 index 0000000..103ca59 Binary files /dev/null and b/cpp04/ex03/AMateria.o differ diff --git a/cpp04/ex03/Character.cpp b/cpp04/ex03/Character.cpp new file mode 100644 index 0000000..4fafb92 --- /dev/null +++ b/cpp04/ex03/Character.cpp @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Character.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 16:58:59 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:28:36 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Character.hpp" +#include "AMateria.hpp" + +Character::Character(std::string name) : name(name) +{ + for (int i = 0; i < 4; ++i) + { + inventory[i] = nullptr; + } +} + +Character::Character(const Character& other) +{ + *this = other; +} + +Character& Character::operator=(const Character& other) +{ + if (this != &other) + { + name = other.name; + for (int i = 0; i < 4; ++i) + { + delete inventory[i]; // Assurez-vous de libérer la mémoire de l'inventaire actuel + inventory[i] = other.inventory[i] ? other.inventory[i]->clone() : nullptr; + } + } + return (*this); +} + +Character::~Character() +{ + for (int i = 0; i < 4; ++i) + { + delete inventory[i]; + } +} + +std::string const & Character::getName() const +{ + return (name); +} + +void Character::equip(AMateria* m) +{ + for (int i = 0; i < 4; ++i) + { + if (!inventory[i]) + { + inventory[i] = m; + break; + } + } +} + +void Character::unequip(int idx) +{ + if (idx >= 0 && idx < 4) + { + inventory[idx] = nullptr; + } +} + +void Character::use(int idx, ICharacter& target) +{ + if (idx >= 0 && idx < 4 && inventory[idx]) + { + inventory[idx]->use(target); + } +} diff --git a/cpp04/ex03/Character.hpp b/cpp04/ex03/Character.hpp new file mode 100644 index 0000000..c9bec8f --- /dev/null +++ b/cpp04/ex03/Character.hpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Character.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 16:58:01 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:58:40 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CHARACTER_HPP +#define CHARACTER_HPP + +#include "ICharacter.hpp" + +class Character : public ICharacter +{ + private: + std::string name; + AMateria* inventory[4]; // Inventaire avec 4 emplacements pour les Materias + + public: + Character(std::string name); + Character(const Character& other); + Character& operator=(const Character& other); + virtual ~Character(); + + virtual std::string const & getName() const override; + virtual void equip(AMateria* m) override; + virtual void unequip(int idx) override; + virtual void use(int idx, ICharacter& target) override; +}; + +#endif diff --git a/cpp04/ex03/Character.o b/cpp04/ex03/Character.o new file mode 100644 index 0000000..eb50065 Binary files /dev/null and b/cpp04/ex03/Character.o differ diff --git a/cpp04/ex03/Cure.cpp b/cpp04/ex03/Cure.cpp new file mode 100644 index 0000000..7f1f136 --- /dev/null +++ b/cpp04/ex03/Cure.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cure.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 17:03:56 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:04:37 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Cure.hpp" + +Cure::Cure() : AMateria("cure") {} + +Cure::Cure(const Cure& other) : AMateria(other) {} + +Cure& Cure::operator=(const Cure& other) +{ + AMateria::operator=(other); + return (*this); +} + +Cure::~Cure() {} + +AMateria* Cure::clone() const +{ + return new Cure(*this); +} + +void Cure::use(ICharacter& target) +{ + std::cout << "* heals " << target.getName() << "’s wounds *" << std::endl; +} diff --git a/cpp04/ex03/Cure.hpp b/cpp04/ex03/Cure.hpp new file mode 100644 index 0000000..d53cc88 --- /dev/null +++ b/cpp04/ex03/Cure.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cure.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 17:03:19 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:03:43 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CURE_HPP +#define CURE_HPP + +#include "AMateria.hpp" + +class Cure : public AMateria +{ + public: + Cure(); + Cure(const Cure& other); + Cure& operator=(const Cure& other); + virtual ~Cure(); + + virtual AMateria* clone() const override; + virtual void use(ICharacter& target) override; +}; + +#endif diff --git a/cpp04/ex03/Cure.o b/cpp04/ex03/Cure.o new file mode 100644 index 0000000..029640a Binary files /dev/null and b/cpp04/ex03/Cure.o differ diff --git a/cpp04/ex03/ICharacter.hpp b/cpp04/ex03/ICharacter.hpp new file mode 100644 index 0000000..12ec995 --- /dev/null +++ b/cpp04/ex03/ICharacter.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ICharacter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 16:52:43 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:25:11 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ICHARACTER_HPP +#define ICHARACTER_HPP + +#include + +class AMateria; + +class ICharacter +{ + public: + virtual ~ICharacter() {} + + virtual std::string const & getName() const = 0; // Doit retourner le nom du personnage + virtual void equip(AMateria* m) = 0; // Équipe une AMateria + virtual void unequip(int idx) = 0; // Déséquipe une AMateria sans la détruire + virtual void use(int idx, ICharacter& target) = 0; // Utilise une AMateria sur un ICharacter +}; + +#endif diff --git a/cpp04/ex03/IMateriaSource.hpp b/cpp04/ex03/IMateriaSource.hpp new file mode 100644 index 0000000..a262bc7 --- /dev/null +++ b/cpp04/ex03/IMateriaSource.hpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* IMateriaSource.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 17:06:19 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:07:06 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef IMATERIASOURCE_HPP +#define IMATERIASOURCE_HPP + +#include "AMateria.hpp" + +class IMateriaSource +{ + public: + virtual ~IMateriaSource() {} + virtual void learnMateria(AMateria*) = 0; + virtual AMateria* createMateria(std::string const & type) = 0; +}; + +#endif diff --git a/cpp04/ex03/Ice.cpp b/cpp04/ex03/Ice.cpp new file mode 100644 index 0000000..c9370c7 --- /dev/null +++ b/cpp04/ex03/Ice.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Ice.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 16:49:57 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:50:52 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Ice.hpp" + +Ice::Ice() : AMateria("ice") {} + +Ice::Ice(const Ice& other) : AMateria(other) {} + +Ice& Ice::operator=(const Ice& other) +{ + AMateria::operator=(other); + return (*this); +} + +Ice::~Ice() {} + +AMateria* Ice::clone() const +{ + return new Ice(*this); +} + +void Ice::use(ICharacter& target) +{ + std::cout << "* shoots an ice bolt at " << target.getName() << " *" << std::endl; +} diff --git a/cpp04/ex03/Ice.hpp b/cpp04/ex03/Ice.hpp new file mode 100644 index 0000000..a351e66 --- /dev/null +++ b/cpp04/ex03/Ice.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Ice.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 16:49:13 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 16:49:44 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ICE_HPP +#define ICE_HPP + +#include "AMateria.hpp" + +class Ice : public AMateria +{ + public: + Ice(); + Ice(const Ice& other); + Ice& operator=(const Ice& other); + virtual ~Ice(); + + virtual AMateria* clone() const override; + virtual void use(ICharacter& target) override; +}; + +#endif diff --git a/cpp04/ex03/Ice.o b/cpp04/ex03/Ice.o new file mode 100644 index 0000000..4355332 Binary files /dev/null and b/cpp04/ex03/Ice.o differ diff --git a/cpp04/ex03/Makefile b/cpp04/ex03/Makefile new file mode 100644 index 0000000..42bbaf5 --- /dev/null +++ b/cpp04/ex03/Makefile @@ -0,0 +1,72 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2023/12/31 17:14:41 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo " ██▓ ▄▄▄ ▓█████▄▓█████ ▄▄▄▄ ▓█████▒███████▓█████";\ + echo " ▓██▒ ▒████▄ ▒██▀ ██▓█ ▀▓█████▄▓█ ▀▒ ▒ ▒ ▄▀▓█ ▀";\ + echo " ▒██░ ▒██ ▀█▄ ░██ █▒███ ▒██▒ ▄█▒███ ░ ▒ ▄▀▒░▒███";\ + echo " ▒██░ ░██▄▄▄▄██░▓█▄ ▒▓█ ▄▒██░█▀ ▒▓█ ▄ ▄▀▒ ▒▓█ ▄";\ + echo " ░██████▓█ ▓██░▒████▓░▒████░▓█ ▀█░▒████▒███████░▒████▒";\ + echo " ░ ▒░▓ ▒▒ ▓▒█░▒▒▓ ▒░░ ▒░ ░▒▓███▀░░ ▒░ ░▒▒ ▓░▒░░░ ▒░ ░";\ + echo " ░ ░ ▒ ░▒ ▒▒ ░░ ▒ ▒ ░ ░ ▒░▒ ░ ░ ░ ░░▒ ▒ ░ ▒░ ░ ░";\ + echo " ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░";\ + echo " ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ ░";\ + echo " ░ ░ ░" + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = materia + +SRC = AMateria.cpp \ + Character.cpp \ + Cure.cpp \ + Ice.cpp \ + MateriaSource.cpp \ + main.cpp \ + +OBJS = ${SRC:.cpp=.o} + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f + +all : $(NAME) + +$(NAME) : $(OBJS) + @echo "$(RED)Compilation fixed... $(DEF_COLOR)" + $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./materia" to execute the program!!$(DEF_COLOR)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp04/ex03/MateriaSource.cpp b/cpp04/ex03/MateriaSource.cpp new file mode 100644 index 0000000..78550b7 --- /dev/null +++ b/cpp04/ex03/MateriaSource.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MateriaSource.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 17:08:37 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:11:59 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "MateriaSource.hpp" + +MateriaSource::MateriaSource() +{ + materias.fill(nullptr); +} + +MateriaSource::MateriaSource(const MateriaSource& other) +{ + *this = other; +} + +MateriaSource& MateriaSource::operator=(const MateriaSource& other) +{ + if (this != &other) + { + for (int i = 0; i < 4; ++i) + { + delete materias[i]; + materias[i] = other.materias[i] ? other.materias[i]->clone() : nullptr; + } + } + return (*this); +} + +MateriaSource::~MateriaSource() +{ + for (auto& materia : materias) + { + delete materia; + } +} + +void MateriaSource::learnMateria(AMateria* m) +{ + for (auto& materia : materias) + { + if (!materia) + { + materia = m; + break; + } + } +} + +AMateria* MateriaSource::createMateria(std::string const & type) +{ + for (auto& materia : materias) + { + if (materia && materia->getType() == type) + { + return materia->clone(); + } + } + return (nullptr); +} diff --git a/cpp04/ex03/MateriaSource.hpp b/cpp04/ex03/MateriaSource.hpp new file mode 100644 index 0000000..f11655c --- /dev/null +++ b/cpp04/ex03/MateriaSource.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MateriaSource.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 17:07:33 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:08:05 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MATERIASOURCE_HPP +#define MATERIASOURCE_HPP + +#include "IMateriaSource.hpp" +#include + +class MateriaSource : public IMateriaSource +{ + private: + std::array materias; // Peut contenir jusqu'à 4 types de Materias + + public: + MateriaSource(); + MateriaSource(const MateriaSource& other); + MateriaSource& operator=(const MateriaSource& other); + virtual ~MateriaSource(); + + virtual void learnMateria(AMateria* m) override; + virtual AMateria* createMateria(std::string const & type) override; +}; + +#endif diff --git a/cpp04/ex03/MateriaSource.o b/cpp04/ex03/MateriaSource.o new file mode 100644 index 0000000..c964909 Binary files /dev/null and b/cpp04/ex03/MateriaSource.o differ diff --git a/cpp04/ex03/main.cpp b/cpp04/ex03/main.cpp new file mode 100644 index 0000000..f0a7cf3 --- /dev/null +++ b/cpp04/ex03/main.cpp @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 17:14:54 by fgras-ca #+# #+# */ +/* Updated: 2023/12/31 17:36:22 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "MateriaSource.hpp" +#include "Character.hpp" +#include "Ice.hpp" +#include "Cure.hpp" + +int main() +{ + IMateriaSource* src = new MateriaSource(); + src->learnMateria(new Ice()); + src->learnMateria(new Cure()); + ICharacter* me = new Character("me"); + ICharacter* bob = new Character("bob"); + + std::string input; + while (true) + { + std::cout << "Enter command (equip, use, or quit): "; + std::cin >> input; + + if (input == "equip") + { + std::string materiaType; + std::cout << "Enter materia type (ice or cure): "; + std::cin >> materiaType; + AMateria* tmp = src->createMateria(materiaType); + if (tmp) + { + me->equip(tmp); + std::cout << materiaType << " equipped." << std::endl; + } + else + { + std::cout << "Unknown materia type." << std::endl; + } + } + else if (input == "use") + { + int idx; + std::cout << "Enter inventory index (0-3): "; + std::cin >> idx; + me->use(idx, *bob); + } + else if (input == "quit") + { + break; + } + else + { + std::cout << "Unknown command." << std::endl; + } + } + + delete bob; + delete me; + delete src; + + return (0); +} diff --git a/cpp04/ex03/main.o b/cpp04/ex03/main.o new file mode 100644 index 0000000..b25cbf4 Binary files /dev/null and b/cpp04/ex03/main.o differ diff --git a/cpp04/ex03/materia b/cpp04/ex03/materia new file mode 100755 index 0000000..632448b Binary files /dev/null and b/cpp04/ex03/materia differ