-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fixed.cpp
53 lines (41 loc) · 1.35 KB
/
Fixed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Created by tde-sous on 11/29/23.
#include "Fixed.hpp"
#include <iostream>
Fixed::Fixed(const float nb_float)
: fpn(nb_float * float(1 << fractional) + (nb_float >= 0 ? 0.5 : -0.5)) {
std::cout << "Float constructor called\n";
}
Fixed::Fixed(const int nb_integer)
: fpn(nb_integer * int(1 << fractional) + (nb_integer >= 0 ? 0.5 : -0.5)) {
std::cout << "Int constructor called\n";
}
Fixed::Fixed() : fpn(0) { std::cout << "Default constructor called\n"; }
Fixed::Fixed(const Fixed &other) : fpn(other.fpn) {
// Copy constructor implementation
std::cout << "Copy constructor called\n";
}
Fixed &Fixed::operator=(const Fixed &other) {
// Copy assignment operator implementation
std::cout << "Copy assignment operator called\n";
if (this == &other)
return *this;
this->setRawBits(other.getRawBits());
return *this;
}
Fixed::~Fixed() {
std::cout << "Destructor called\n";
// Destructor implementation
}
int Fixed::getRawBits(void) const {
std::cout << "getRawBits member function called\n";
return (fpn);
}
void Fixed::setRawBits(const int raw) { fpn = raw; }
float Fixed::toFloat(void) const {
return (float(fpn) / float(1 << fractional));
}
int Fixed::toInt(void) const { return (int(fpn) / int(1 << fractional)); }
std::ostream &operator<<(std::ostream &out, const Fixed &right) {
out << right.toFloat();
return (out);
}