









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
CPP_02 in the 42 (1337) coding school
Typology: Exercises
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Summary: This document contains the exercises of Module 02 from C++ modules.
Version: 8
Compiling
Formatting and naming conventions
Allowed/Forbidden
You are not coding in C anymore. Time to C++! Therefore:
C++ - Module 02
Ad-hoc polymorphism, operator overloading and Orthodox Canonical class form
A few design requirements
Read me
You will have to implement a lot of classes. This can seem tedious, unless you’re able to script your favorite text editor.
You are given a certain amount of freedom to complete the exercises. However, follow the mandatory rules and don’t be lazy. You would miss a lot of useful information! Do not hesitate to read about theoretical concepts.
Exercise : 00
My First Class in Orthodox Canonical Form Turn-in directory : ex 00 / Files to turn in : Makefile, main.cpp, Fixed.{h, hpp}, Fixed.cpp Forbidden functions : None
You think you know integers and floating-point numbers. How cute.
Please read this 3 pages article (1, 2, 3) to discover that you don’t. Go on, read it.
Until today, every number you used in your code was basically either an integer or a floating-point number, or any of their variants (short, char, long, double, and so forth). After reading the article above, it’s safe to assume that integers and floating-point num- bers have opposite caracteristics.
But today, things will change. You are going to discover a new and awesome number type: fixed-point numbers! Forever missing from the scalar types of most languages, fixed-point numbers offer a valuable balance between performance, accuracy, range and precision. That explains why fixed-point numbers are particularly applicable to computer graphics, sound processing or scientific programming, just to name a few.
As C++ lacks fixed-point numbers, you’re going to add them. This article from Berkeley is a good start. If you have no idea what Berkeley University is, read this section of its Wikipedia page.
C++ - Module 02
Ad-hoc polymorphism, operator overloading and Orthodox Canonical class form
Create a class in Orthodox Canonical Form that represents a fixed-point number:
◦ An integer to store the fixed-point number value. ◦ A static constant integer to store the number of fractional bits. Its value will always be the integer literal 8.
◦ A default constructor that initializes the fixed-point number value to 0. ◦ A copy constructor. ◦ A copy assignment operator overload. ◦ A destructor. ◦ A member function int getRawBits( void ) const; that returns the raw value of the fixed-point value. ◦ A member function void setRawBits( int const raw ); that sets the raw value of the fixed-point number.
Running this code:
#include
Should output something similar to: $> ./a.out Default constructor called Copy constructor called Copy assignment operator called // <-- This line may be missing depending on your implementation getRawBits member function called Default constructor called Copy assignment operator called getRawBits member function called getRawBits member function called 0 getRawBits member function called 0 getRawBits member function called 0 Destructor called Destructor called Destructor called $>
C++ - Module 02
Ad-hoc polymorphism, operator overloading and Orthodox Canonical class form
Running this code:
#include
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. b is 10 c is 42. 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 $>
Exercise 02
Now we’re talking Turn-in directory : ex 02 / Files to turn in : Makefile, main.cpp, Fixed.{h, hpp}, Fixed.cpp Allowed functions : roundf (from
Add public member functions to your class to overload the following operators:
Add these four public overloaded member functions to your class:
Exercise 03
BSP Turn-in directory : ex 03 / Files to turn in : Makefile, main.cpp, Fixed.{h, hpp}, Fixed.cpp, Point.{h, hpp}, Point.cpp, bsp.cpp Allowed functions : roundf (from
Now that you have a functional Fixed class, it would be nice to use it.
Implement a function which indicates whether a point is inside of a triangle or not. Very useful, isn’t it?
BSP stands for Binary space partitioning. You are welcome. :)
You can pass this module without doing exercise 03.
C++ - Module 02
Ad-hoc polymorphism, operator overloading and Orthodox Canonical class form
Let’s start by creating the class Point in Orthodox Canonical Form that represents a 2D point:
◦ A Fixed const attribute x. ◦ A Fixed const attribute y. ◦ Anything else useful.
◦ A default constructor that initializes x and y to 0. ◦ A constructor that takes as parameters two constant floating-point numbers. It initializes x and y with those parameters. ◦ A copy constructor. ◦ A copy assignment operator overload. ◦ A destructor. ◦ Anything else useful.
To conclude, implement the following function in the appropriate file:
bool bsp( Point const a, Point const b, Point const c, Point const point);
Implement and turn in your own tests to ensure that your class behaves as expected.