Skip to content

CPP02-ex01 - Towards a more useful fixed-point number class #38

Description

@TheLeBerton

Files to submit

  • in ex01/ dir
  • Makefile
  • main.cpp
  • Fixed.{h, hpp}
  • Fixed.cpp

Authorized

  • roundf from

Definition of done

Add the following public constructors and public member functions to the class

  • a constructor that takes a constant integer as a parameter
  • a constructor that takes a constant floating-point number as a parameter
  • a member function float toFloat( void ) const; that converts the fixed-point value to a floating-point value
  • a method int toInt( void ) const; that converts the fixed-point value to an integer value
  • an overload of the insertion << operator that inserts a floating-point representation of the fixed-point number into the output stream object passed as a parameter

Testing

Running this code:

#include <iostream>

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;
}

Should output something similar to:

$> ./a.out
Default constructor called
Int constructor called
Float constructor called
Copy constructor called
Copy assignment operator called
Float constructor called
Copy assignment operator called
Destructor called
a is 1234.43
b is 10
c is 42.4219
d is 10
a is 1234 as integer
b is 10 as integer
c is 42 as integer
d is 10 as integer
Destructor called
Destructor called
Destructor called
Destructor called
$>

Metadata

Metadata

Assignees

Labels

Type

Fields

Priority

High

Projects

Status
Todo

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions