From b54faafe1ddab71c3a167c113081b6cb725e6a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 13 May 2025 13:38:40 +0200 Subject: [PATCH] feat(ex00): it works --- .gitignore | 4 ++++ ex00/.gitignore | 1 + ex00/Fixed.cpp | 29 +++++++++++++++++++++++++++++ ex00/Fixed.hpp | 19 +++++++++++++++++++ ex00/Makefile | 39 +++++++++++++++++++++++++++++++++++++++ ex00/main.cpp | 13 +++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 ex00/.gitignore create mode 100644 ex00/Fixed.cpp create mode 100644 ex00/Fixed.hpp create mode 100644 ex00/Makefile create mode 100644 ex00/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a4cfdeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.d +.cache +compile_commands.json diff --git a/ex00/.gitignore b/ex00/.gitignore new file mode 100644 index 0000000..5d61c98 --- /dev/null +++ b/ex00/.gitignore @@ -0,0 +1 @@ +ex00 diff --git a/ex00/Fixed.cpp b/ex00/Fixed.cpp new file mode 100644 index 0000000..a17d2e4 --- /dev/null +++ b/ex00/Fixed.cpp @@ -0,0 +1,29 @@ +#include "Fixed.hpp" +#include + +const int Fixed::fracbits(8); + +Fixed::Fixed() { + std::cout << "Default constructor called" << std::endl; + value = 0; +} + +Fixed::Fixed(Fixed &other) { + std::cout << "Copy constructor called" << std::endl; + this->value = other.value; +} + +Fixed &Fixed::operator=(Fixed &other) { + std::cout << "Copy assignment operator called" << std::endl; + this->setRawBits(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 value; +} + +void Fixed::setRawBits(int const raw) { value = raw; } diff --git a/ex00/Fixed.hpp b/ex00/Fixed.hpp new file mode 100644 index 0000000..6935e36 --- /dev/null +++ b/ex00/Fixed.hpp @@ -0,0 +1,19 @@ +#ifndef FIXED_HPP +#define FIXED_HPP + +class Fixed { +public: + Fixed(); + Fixed(Fixed &other); + Fixed &operator=(Fixed &other); + ~Fixed(); + + int getRawBits(void) const; + void setRawBits(int const raw); + +private: + int value; + static const int fracbits; +}; + +#endif diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..4a4ab08 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,39 @@ +NAME = ex00 +ifeq ($(CPPFLAGS),) + CPPFLAGS = -Wall -Wextra -Werror -std=c++98 -g +endif +ifeq ($(CXX),) + CXX = c++ +endif +# g++ is the default on 42 computers +ifeq ($(CXX),g++) + CXX = c++ +endif +srcs = \ + Fixed.cpp \ + +main_objs = main.o $(srcs:.cpp=.o) +all_objs = $(main_objs) +deps = $(all_objs:.o=.d) + +all: $(NAME) + +-include $(deps) + +$(NAME): $(main_objs) + $(CXX) $(CPPFLAGS) -o $@ $^ + +%.o: %.cpp + $(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp + $(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d + +clean: + find . -name '*.o' -print -delete + find . -name '*.d' -print -delete + +fclean: clean + rm -f $(NAME) + +re: + +make fclean + +make all diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..39313e7 --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,13 @@ +#include "Fixed.hpp" +#include + +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; +}