「🎉」 init: hello world !
This commit is contained in:
47
ex02/Array.hpp
Normal file
47
ex02/Array.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
|
||||
template <typename T> class Array {
|
||||
public:
|
||||
Array(): _arr(new T[0]), _size(0) {}
|
||||
Array(const int n): _arr(new T[n]), _size(n) {}
|
||||
Array(const Array &cpy) {*this = cpy;}
|
||||
~Array(void) {delete[] _arr;}
|
||||
|
||||
Array &operator=(const Array &cpy)
|
||||
{
|
||||
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 &pos)
|
||||
{
|
||||
if (pos < 0 || pos >= _size)
|
||||
{
|
||||
throw outOfBoundException();
|
||||
}
|
||||
return _arr[pos];
|
||||
}
|
||||
|
||||
class outOfBoundException : public std::exception {
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return ("value is out of bound");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
private:
|
||||
T *_arr;
|
||||
size_t _size;
|
||||
};
|
||||
58
ex02/Makefile
Normal file
58
ex02/Makefile
Normal file
@@ -0,0 +1,58 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
|
||||
# Updated: 2025/08/16 19:49:30 by mmoussou ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SHELL = bash
|
||||
|
||||
RED = \033[0;31m
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[1;33m
|
||||
PURPLE = \e[0;35m
|
||||
NC = \033[0m
|
||||
DELETE = \x1B[2K
|
||||
|
||||
CC = c++
|
||||
|
||||
FLAGS = -std=c++98 -Wall -Werror -Wextra
|
||||
|
||||
INCLUDES = .
|
||||
|
||||
NAME = ex02
|
||||
|
||||
SRCS = main.cpp
|
||||
|
||||
OBJSDIR = obj/
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
@$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
|
||||
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
|
||||
|
||||
$(OBJSDIR)%.o: %.cpp
|
||||
@mkdir -p $(@D)
|
||||
@$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@
|
||||
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
|
||||
|
||||
clean:
|
||||
@rm -f $(OBJS)
|
||||
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@rm -Rf $(OBJSDIR)
|
||||
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
|
||||
|
||||
re: fclean
|
||||
@$(MAKE) -s all
|
||||
|
||||
.PHONY: clean fclean all re
|
||||
74
ex02/main.cpp
Normal file
74
ex02/main.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <Array.hpp>
|
||||
|
||||
#define MAX_VAL 750
|
||||
int main(int, char**)
|
||||
{
|
||||
Array<int> numbers(MAX_VAL);
|
||||
int* mirror = new int[MAX_VAL];
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < MAX_VAL; i++)
|
||||
{
|
||||
const int value = rand() % 100;
|
||||
numbers[i] = value;
|
||||
mirror[i] = value;
|
||||
}
|
||||
//SCOPE
|
||||
{
|
||||
Array<int> tmp = numbers;
|
||||
Array<int> test(tmp);
|
||||
}
|
||||
|
||||
std::cout << "checking if values are the same after a copy..." << std::endl;
|
||||
for (int i = 0; i < MAX_VAL; i++)
|
||||
{
|
||||
if (mirror[i] != numbers[i])
|
||||
{
|
||||
std::cerr << "didn't save the same value!!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
std::cout << "the values are the same ! :D" << std::endl << std::endl;
|
||||
|
||||
std::cout << "trying -2:" << std::endl;
|
||||
try
|
||||
{
|
||||
numbers[-2] = 0;
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
|
||||
std::cout << std::endl << "trying a correct value (15):" << std::endl;
|
||||
try
|
||||
{
|
||||
std::cout << numbers[15] << std::endl;
|
||||
numbers[15] = 0;
|
||||
std::cout << numbers[15] << std::endl;
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
|
||||
std::cout << std::endl << "trying MAX_VAL:" << std::endl;
|
||||
try
|
||||
{
|
||||
numbers[MAX_VAL] = 0;
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
for (int i = 0; i < MAX_VAL; i++)
|
||||
{
|
||||
std::cout << numbers[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
delete [] mirror;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user