From 3d48ed0312f9c0ae691560d7cd688f46917e3fd4 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 13 Apr 2020 13:07:19 +0200 Subject: cpp02 base --- cpp02/ex00/Fixed.cpp | 48 +++++++++++++++++++++++++++++ cpp02/ex00/Fixed.hpp | 33 ++++++++++++++++++++ cpp02/ex00/main.cpp | 27 +++++++++++++++++ cpp02/ex01/Fixed.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cpp02/ex01/Fixed.hpp | 44 +++++++++++++++++++++++++++ cpp02/ex01/main.cpp | 35 ++++++++++++++++++++++ 6 files changed, 272 insertions(+) create mode 100644 cpp02/ex00/main.cpp create mode 100644 cpp02/ex01/Fixed.cpp create mode 100644 cpp02/ex01/Fixed.hpp create mode 100644 cpp02/ex01/main.cpp diff --git a/cpp02/ex00/Fixed.cpp b/cpp02/ex00/Fixed.cpp index e69de29..eb0501b 100644 --- a/cpp02/ex00/Fixed.cpp +++ b/cpp02/ex00/Fixed.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 11:45:18 by charles #+# #+# */ +/* Updated: 2020/04/13 11:58:37 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +Fixed::Fixed() + : m_value(0) +{ + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(Fixed const& other) +{ + std::cout << "Copy constructor called" << std::endl; + *this = other; +} + +Fixed::~Fixed() +{ + std::cout << "Destructor called" << std::endl; +} + +void Fixed::operator=(Fixed const& other) +{ + std::cout << "Assignation operator called" << std::endl; + m_value = other.getRawBits(); +} + +int Fixed::getRawBits() const +{ + std::cout << "getRawBits member function called" << std::endl; + return m_value; +} + +void Fixed::setRawBits(int const raw) +{ + std::cout << "setRawBits member function called" << std::endl; + m_value = raw; +} diff --git a/cpp02/ex00/Fixed.hpp b/cpp02/ex00/Fixed.hpp index e69de29..1f956d7 100644 --- a/cpp02/ex00/Fixed.hpp +++ b/cpp02/ex00/Fixed.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 11:40:49 by charles #+# #+# */ +/* Updated: 2020/04/13 11:53:30 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_HPP +# define FIXED_HPP + +# include + +class Fixed +{ +public: + Fixed(); + Fixed(Fixed const& other); + ~Fixed(); + void operator=(Fixed const& other); + int getRawBits() const; + void setRawBits(int const raw); + +private: + int m_value; + static int const m_fractional_bits = 8; +}; + +#endif diff --git a/cpp02/ex00/main.cpp b/cpp02/ex00/main.cpp new file mode 100644 index 0000000..494dbb3 --- /dev/null +++ b/cpp02/ex00/main.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 11:51:15 by charles #+# #+# */ +/* Updated: 2020/04/13 11:51:16 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#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; +} diff --git a/cpp02/ex01/Fixed.cpp b/cpp02/ex01/Fixed.cpp new file mode 100644 index 0000000..36c76d5 --- /dev/null +++ b/cpp02/ex01/Fixed.cpp @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 11:45:18 by charles #+# #+# */ +/* Updated: 2020/04/13 13:02:27 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +Fixed::Fixed() + : m_value(0) +{ + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(Fixed const& other) +{ + std::cout << "Copy constructor called" << std::endl; + *this = other; +} + +Fixed::~Fixed() +{ + std::cout << "Destructor called" << std::endl; +} + +void Fixed::operator=(Fixed const& other) +{ + std::cout << "Assignation operator called" << std::endl; + m_value = other.getRawBits(); +} + +Fixed::Fixed(int from) +{ + std::cout << " constructor called" << std::endl; + m_value = from << m_fractionalBits; +} + +Fixed::Fixed(float from) +{ + m_value = 0; +} + +int Fixed::getRawBits() const +{ + std::cout << "getRawBits member function called" << std::endl; + return m_value; +} + +void Fixed::setRawBits(int const raw) +{ + std::cout << "setRawBits member function called" << std::endl; + m_value = raw; +} + +float Fixed::toFloat() const +{ + return 0; +} + +int Fixed::toInt() const +{ + return m_value >> m_fractionalBits; +} + +int Fixed::getFractionalBits() +{ + return m_fractionalBits; +} + +std::ostream& operator<<(std::ostream& out, Fixed const& f) +{ + int dec_part; + int shift_size; + + shift_size = (sizeof(int) * 8) - Fixed::getFractionalBits(); + dec_part = f.getRawBits() << shift_size >> shift_size; + out << f.toInt() << '.' << dec_part; + return out; +} diff --git a/cpp02/ex01/Fixed.hpp b/cpp02/ex01/Fixed.hpp new file mode 100644 index 0000000..2dd2d7d --- /dev/null +++ b/cpp02/ex01/Fixed.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 11:40:49 by charles #+# #+# */ +/* Updated: 2020/04/13 12:58:33 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_HPP +# define FIXED_HPP + +# include + +class Fixed +{ +public: + Fixed(); + Fixed(Fixed const& other); + ~Fixed(); + void operator=(Fixed const& other); + + Fixed(int from); + Fixed(float from); + + int getRawBits() const; + void setRawBits(int const raw); + + float toFloat() const; + int toInt() const; + + static int getFractionalBits(); + +private: + int m_value; + static int const m_fractionalBits = 8; +}; + +std::ostream& operator<<(std::ostream& out, Fixed const& f); + +#endif diff --git a/cpp02/ex01/main.cpp b/cpp02/ex01/main.cpp new file mode 100644 index 0000000..44ffe34 --- /dev/null +++ b/cpp02/ex01/main.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 11:51:15 by charles #+# #+# */ +/* Updated: 2020/04/13 12:08:07 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" + +int main(void) +{ + 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; +} -- cgit