karen pointer sur fonctions en static, sed resolution eof

This commit is contained in:
Hugo LAMY
2022-02-10 17:21:01 +01:00
parent c287ea7c97
commit 504f6c3524
29 changed files with 369 additions and 8 deletions

66
d02/ex01/Fixed.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include "Fixed.hpp"
void printBits(std::string before, unsigned int num)
{
int i = 0;
std::cout << before;
for (unsigned int mask = 1U << 31; mask; mask = mask >> 1)
{
std::cout << ((num & mask) != 0);
i++;
if (i % 8 == 0)
std::cout << ' ';
}
std::cout << "(" << num << ")" << '\n';
}
// default/parametric constructor
Fixed::Fixed( void ) : _value( 0 ) {
std::cout << "Default constructor called" << '\n';
return;
}
// copy constructor
Fixed::Fixed( Fixed const & src ) {
std::cout << "Copy constructor called" << '\n';
*this = src;
return;
}
// destructor
Fixed::~Fixed( void ) {
std::cout << "Destructor called" << '\n';
return;
}
Fixed::Fixed( int integer ) {
std::cout << integer << '\n';
printBits("integer : ", integer);
this->_value = integer << this->_frac;
printBits("this->_value : ", this->_value);
}
// assignement operator
Fixed & Fixed::operator=( Fixed const & rhs ) {
std::cout << "Copy assignment operator called" << '\n';
if ( this != &rhs )
this->_value = rhs.getRawBits();
return *this;
}
int Fixed::getRawBits( void ) const {
std::cout << "getRawBits member function called" << '\n';
return this->_value;
}
void Fixed::setRawBits( int const raw ) {
this->_value = raw;
}

29
d02/ex01/Fixed.hpp Normal file
View File

@@ -0,0 +1,29 @@
#ifndef FIXED_HPP
# define FIXED_HPP
#include <iostream>
#include <string>
class Fixed {
public:
Fixed( void ); // default/parametric constructor
Fixed( Fixed const & src ); // copy constructor
~Fixed( void ); // destructor
Fixed( int integer);
Fixed( float const floater );
Fixed & operator=( Fixed const & rhs ); // assignement operator
int getRawBits( void ) const;
void setRawBits( int const raw );
private:
int _value;
static int const _frac = 8;
};
#endif

62
d02/ex01/Makefile Normal file
View File

@@ -0,0 +1,62 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value . name is case sensitive #
# VARIABLES . or name = value \ . use VPATH only for .c #
# . value . or .cpp #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = fixed
CC = clang++
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
VPATH = $(D_SRCS)
LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = .
SRCS = main.cpp \
Fixed.cpp
D_HEADERS = .
HEADERS = Fixed.hpp
D_OBJS = builds
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
RM_D_OBJS = rm -rf $(D_OBJS)
ifeq "$(D_OBJS)" "."
RM_D_OBJS =
endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
# RULES . recipe . $< : 1st prerequisite #
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
mkdir $@
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
$(RM_D_OBJS)
re: fclean all
.PHONY : all clean fclean re bonus run valgrind

BIN
d02/ex01/fixed Executable file

Binary file not shown.

36
d02/ex01/main.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "Fixed.hpp"
#include <iostream>
int main( void ) {
Fixed a( 1 );
std::cout << a.getRawBits() << '\n';
Fixed b( 6 );
std::cout << b.getRawBits() << '\n';
Fixed c( -6 );
std::cout << c.getRawBits() << '\n';
Fixed d( -1 );
std::cout << d.getRawBits() << '\n';
// 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;
}