feat: finishing the exercice 00 of the cpp02

This commit is contained in:
Raphael 2024-12-23 14:35:26 +01:00
parent bbc496d868
commit ff72109a4e
4 changed files with 223 additions and 0 deletions

44
cpp02/ex00/Fixed.cpp Normal file
View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/21 19:52:44 by rparodi #+# #+# */
/* Updated: 2024/12/23 14:34:26 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <iostream>
Fixed::Fixed() {
std::cout << "Default constructor called" << std::endl;
_fixed_value = 0;
}
Fixed::Fixed(const Fixed &copy) {
std::cout << "Copy constructor called" << std::endl;
this->_fixed_value = copy._fixed_value;
}
Fixed::~Fixed() {
std::cout << "Destructor called" << std::endl;
}
Fixed & Fixed::operator=(const Fixed &value){
std::cout << "Copy assignment operator called" << std::endl;
this->_fixed_value = value._fixed_value;
return *this;
}
void Fixed::setRawBits(const int value) {
std::cout << "setRawBits member function called" << std::endl;
this->_fixed_value = value;
}
int Fixed::getRawBits() {
std::cout << "getRawBits member function called" << std::endl;
return this->_fixed_value;
}

31
cpp02/ex00/Fixed.hpp Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/21 19:30:23 by rparodi #+# #+# */
/* Updated: 2024/12/23 14:32:05 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FIXED_HPP
#define FIXED_HPP
#include <iostream>
class Fixed {
public:
Fixed();
Fixed(const Fixed &copy);
~Fixed();
int getRawBits();
void setRawBits(const int value);
Fixed & operator = (const Fixed &value);
private:
int _fixed_value;
static const int _fractionnal = 8;
};
#endif

124
cpp02/ex00/Makefile Normal file
View file

@ -0,0 +1,124 @@
# **************************************************************************** #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/12/21 19:50:58 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = Fixed
# Commands
CXX = c++
RM = rm -rf
# Flags
# Mandatory flags for 42 cpp
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
# Flags to debug and have the dependences (can be removed for correction)
CXXFLAGS += -MMD -g3
# Flag to debug (TO REMOVE)
# CXXFLAGS += -D DEBUG=1
# Sources
SRC = Fixed.cpp \
main.cpp
# Objects
OBJDIRNAME = ./build
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
# Colors
GREEN = \033[32m
GREY = \033[0;90m
RED = \033[0;31m
GOLD = \033[38;5;220m
END = \033[0m
# Rules
# All (make all)
all: header $(NAME) footer
# Bonus (make bonus)
bonus: header $(OBJ) footer
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Clean (make clean)
clean:
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
@$(RM) $(OBJDIRNAME)
# Clean (make fclean)
fclean: clean
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
@$(RM) $(NAME)
@echo ""
# Restart (make re)
re: header fclean all
# Dependences for all
$(NAME): $(OBJ)
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Creating the objects
$(OBJDIRNAME)/%.o: %.cpp
@mkdir -p $(dir $@)
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
@$(CXX) $(CXXFLAGS) -o $@ -c $<
# Header
header:
@clear
@printf '\n\n'
@printf '$(GOLD) ******* ****** ******* $(END)\n'
@printf '$(GOLD) ****** *** ******* $(END)\n'
@printf '$(GOLD) ******* * ******* $(END)\n'
@printf '$(GOLD) ****** ******* $(END)\n'
@printf '$(GOLD) ******* ******* $(END)\n'
@printf '$(GOLD) ******************* ******* * $(END)\n'
@printf '$(GOLD) ******************* ******* *** $(END)\n'
@printf '$(GOLD) ****** ******* ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GREY) Made by rparodi$(END)\n\n'
# Footer
footer:
@printf "\n"
@printf "$(GOLD) ,_ _,$(END)\n"
@printf "$(GOLD) | \\___//|$(END)\n"
@printf "$(GOLD) |=6 6=|$(END)\n"
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
@printf "$(GOLD) ) \` ( ,$(END)\n"
@printf "$(GOLD) / \\ (('$(END)\n"
@printf "$(GOLD) | | ))$(END)\n"
@printf "$(GOLD) /| | | |\\_//$(END)\n"
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
@printf "$(GOLD) '\"' '\"'$(END)\n"
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
clangd:
@echo \
"CompileFlags:\n" \
" Add:\n" \
" - \"-std=c++98 -Wall -Wextra -Werror\"\n" \
" - \"-I"$(shell pwd)"/includes\"\n" \
" - \"-xc++\"\n" \
> .clangd
# Phony
.PHONY: all bonus clean fclean re clangd
-include ${OBJ:.o=.d}

24
cpp02/ex00/main.cpp Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/21 19:52:47 by rparodi #+# #+# */
/* Updated: 2024/12/23 14:34:53 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
int main(void) {
Fixed a;
Fixed b( a );
Fixed c;
c = b;
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
return 0;
}