Skip to content

The Fantastic Four! To join the club, each class must feature these four superstar members: Default constructor (The magic opener ) Copy constructor (The clone master ) Copy assignment operator (The switcheroo artist ) Destructor (The clean-up crew )

Notifications You must be signed in to change notification settings

NigeParis/42-cpp-02

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Of course! Here's the revised version without emojis:

New Class Code Playbook

1. Welcome to the Orthodox Canonical Form Club!

From now on, all your classes must join the exclusive ranks of the Orthodox Canonical Form, unless you receive special clearance otherwise.

2. The Fantastic Four!

To join the club, each class must feature these four superstar members:

  • Default constructor (The magic opener)
  • Copy constructor (The clone master)
  • Copy assignment operator (The switcheroo artist)
  • Destructor (The clean-up crew)

3. Divide and Conquer!

  • Header file (.hpp/.h): Contains the class's royal decree (definition).
  • Source file (.cpp): Implements the class's grand plan (implementation).

Step 1: The Basics

Up until now, you've been working with:

  • Integers and their buddies (short, char, long, etc.)
  • Floating-point numbers and their cousins (float, double, etc.)

These have been your go-to number types in coding.

Step 2: Opposite Characteristics

Integers and floating-point numbers couldn't be more different in their characteristics. It’s a classic case of opposites attract.

Step 3: Meet the Game Changer

Introducing fixed-point numbers—your new best friend in coding! Fixed-point numbers strike the perfect balance between:

  • Performance
  • Accuracy
  • Range
  • Precision

Why Should You Care?

Fixed-point numbers are particularly useful in:

  • Computer graphics: For flawless visual calculations.
  • Sound processing: Keeping your audio crisp and clear.

Exercises for Fixed-Point Number Class

Exercise 00: My First Class in Orthodox Canonical Form

The Journey Begins

Your first steps into the world of class design were promising, but our class currently represents a grand total of... 0.0. Time to spice things up!

Exercise 01: Towards a More Useful Fixed-Point Number Class

Enhancing the Foundation

Our previous class was a humble beginning, but let's push its boundaries. Here’s how to level up:

Constructor Upgrades

  • Integer Constructor: Transforms a constant integer into a fixed-point value with 8 fractional bits.
    Fixed(const int value);
  • Floating-Point Constructor: Converts a constant floating-point number into a fixed-point value with 8 fractional bits.
    Fixed(const float value);

Transformation Skills

  • Convert to Float: float toFloat(void) const; Transforms the fixed-point value into a floating-point value.
    float toFloat(void) const;
  • Convert to Int: int toInt(void) const; Transforms the fixed-point value into an integer value.
    int toInt(void) const;

Stream Magic

Add an overload of the insertion (<<) operator to insert a floating-point representation of the fixed-point number into the output stream object passed as a parameter.

friend std::ostream &operator<<(std::ostream &out, const Fixed &fixed);

Exercise 02: Now We’re Talking

Operator Overload Party

Add public member functions to overload these operators:

  • Comparison Operators: >, <, >=, <=, ==, and !=.
    bool operator>(const Fixed &other) const;
    bool operator<(const Fixed &other) const;
    bool operator>=(const Fixed &other) const;
    bool operator<=(const Fixed &other) const;
    bool operator==(const Fixed &other) const;
    bool operator!=(const Fixed &other) const;
  • Arithmetic Operators: +, -, *, and /.
    Fixed operator+(const Fixed &other) const;
    Fixed operator-(const Fixed &other) const;
    Fixed operator*(const Fixed &other) const;
    Fixed operator/(const Fixed &other) const;
  • Increment/Decrement Operators: Pre-increment, post-increment, pre-decrement, and post-decrement, adjusting the fixed-point value by the smallest representable ϵ.
    Fixed &operator++();      // Pre-increment
    Fixed operator++(int);    // Post-increment
    Fixed &operator--();      // Pre-decrement
    Fixed operator--(int);    // Post-decrement

Superpowers: Min and Max

Add these public overloaded member functions to your class:

  • Static Min: Takes two references to fixed-point numbers and returns a reference to the smallest one.
    static Fixed &min(Fixed &a, Fixed &b);
  • Static Min (Const): Takes two references to constant fixed-point numbers and returns a reference to the smallest one.
    static const Fixed &min(const Fixed &a, const Fixed &b);
  • Static Max: Takes two references to fixed-point numbers and returns a reference to the greatest one.
    static Fixed &max(Fixed &a, Fixed &b);
  • Static Max (Const): Takes two references to constant fixed-point numbers and returns a reference to the greatest one.
    static const Fixed &max(const Fixed &a, const Fixed &b);

Exercise 03: BSP - Bonus

Putting It All Together

Now that your Fixed class is fully functional, let’s put it to use! Implement a function that indicates whether a point is inside a triangle or not.

bool    bsp( Point const a, Point const b, Point const c, Point const point) {

   //code
}

Screenshot from 2025-01-09 14-03-46

Screenshot from 2025-01-09 14-04-09

Screenshot from 2025-01-09 14-04-27

  • Scientific programming: Ensuring your data remains spot on.

Fixed-point numbers are here to elevate your coding game. Embrace the change and enjoy the ride!

About

The Fantastic Four! To join the club, each class must feature these four superstar members: Default constructor (The magic opener ) Copy constructor (The clone master ) Copy assignment operator (The switcheroo artist ) Destructor (The clean-up crew )

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published