「✨」 feat: finished ex02
This commit is contained in:
0
ex02/Array.cpp
Normal file
0
ex02/Array.cpp
Normal file
91
ex02/Array.hpp
Normal file
91
ex02/Array.hpp
Normal file
@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Array.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 15:36:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/14 18:52:43 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
|
||||
static void _log(std::string emoji, std::string what, std::string who,
|
||||
std::string str) {
|
||||
#ifdef VERBOSE
|
||||
if (who.empty())
|
||||
std::cout << "「" << emoji << "」" << what << ": " << str << std::endl;
|
||||
else
|
||||
std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str
|
||||
<< std::endl;
|
||||
#else
|
||||
(void)emoji, (void)what, (void)who, (void)str;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T> class Array {
|
||||
public:
|
||||
Array(void) : _arr(new T[0]), _size(0) {
|
||||
_log("➕", "Array", "", "default constructor called");
|
||||
}
|
||||
Array(unsigned int n) : _arr(new T[n]), _size(n) {
|
||||
_log("➕", "Array", "", "size constructor called");
|
||||
}
|
||||
Array(const Array &cpy) {
|
||||
_log("➕", "Array", "", "copy constructor called");
|
||||
*this = cpy;
|
||||
}
|
||||
~Array(void) {
|
||||
_log("➖", "Array", "", "destructor called");
|
||||
delete[] _arr;
|
||||
}
|
||||
|
||||
Array &operator=(const Array &cpy) {
|
||||
_log("➕", "Array", "", "assignement constructor called");
|
||||
if (this != &cpy) {
|
||||
_size = cpy._size;
|
||||
_arr = new T[_size];
|
||||
if (_arr) {
|
||||
for (std::size_t i = 0; i < _size; i++) {
|
||||
_arr[i] = cpy._arr[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
T &operator[](const size_t &where) {
|
||||
if (where >= _size)
|
||||
throw outOfBoundException();
|
||||
|
||||
return _arr[where];
|
||||
}
|
||||
|
||||
T *getArray(void) const { return _arr; }
|
||||
std::size_t getSize(void) const { return _size; }
|
||||
|
||||
class outOfBoundException : public std::exception {
|
||||
virtual const char *what() const throw() { return ("out of bound"); }
|
||||
};
|
||||
|
||||
private:
|
||||
T *_arr;
|
||||
std::size_t _size;
|
||||
|
||||
T *_clone(void) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &os, const Array<T> &arr) {
|
||||
for (std::size_t i = 0; i < arr.getSize(); i++) {
|
||||
os << arr.getArray()[i];
|
||||
}
|
||||
return os;
|
||||
}
|
60
ex02/Makefile
Normal file
60
ex02/Makefile
Normal file
@ -0,0 +1,60 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
|
||||
# Updated: 2025/04/09 15:36:26 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SHELL = bash
|
||||
|
||||
NAME = array
|
||||
|
||||
CC = c++
|
||||
|
||||
OBJSDIR = obj/
|
||||
|
||||
SRCS = $(shell find . -name '*.cpp')
|
||||
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
|
||||
|
||||
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
|
||||
|
||||
RED = \033[0;31m
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[1;33m
|
||||
PURPLE = \e[0;35m
|
||||
NC = \033[0m
|
||||
DELETE = \x1B[2K\r
|
||||
|
||||
ifeq ($(VERBOSE),true)
|
||||
FLAGS += -D VERBOSE
|
||||
endif
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
|
||||
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
|
||||
|
||||
$(OBJSDIR)%.o: %.cpp
|
||||
@mkdir -p $(@D)
|
||||
@$(CC) $(FLAGS) -I . -c $< -o $@
|
||||
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
|
||||
|
||||
clean:
|
||||
@rm -f $(OBJS)
|
||||
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@rm -Rf $(OBJSDIR)
|
||||
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: clean fclean all re
|
28
ex02/main.cpp
Normal file
28
ex02/main.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/14 18:52:19 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/14 18:52:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Array.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
Array<int> str(7);
|
||||
|
||||
str[0] = 10;
|
||||
str[1] = 9;
|
||||
str[2] = 8;
|
||||
str[3] = 7;
|
||||
str[4] = 6;
|
||||
str[5] = 5;
|
||||
str[6] = 4;
|
||||
|
||||
std::cout << str << std::endl;
|
||||
}
|
Reference in New Issue
Block a user