👨‍💻
Guide
Laura's GithubSimon's Github
  • What is this gitbook for?
  • 🛠️Useful tools
    • 🏁Header files
    • 🧱C Structures
    • 🔗Linked Lists (todo)
    • 📄Makefiles
    • 🔄Switch statement
    • 🗃️File descriptors (FD)
  • 🖌️MiniLibX
    • MiniLibX Helper Function
    • MiniLibX Hook Examples
  • 0️⃣ Rank 00
    • Libft
      • 📑LIBC functions
        • ft_isalpha
        • ft_isdigit
        • ft_isalnum
        • ft_isascii
        • ft_isprint
        • ft_strlen
        • ft_memset
        • ft_bzero
        • ft_memcpy
        • ft_memmove
        • ft_strlcpy
        • ft_strlcat
        • ft_toupper
        • ft_tolower
        • ft_strchr
        • ft_strrchr
        • ft_strncmp
        • ft_memchr
        • ft_memcmp
        • ft_strnstr
        • ft_atoi
        • ft_calloc
        • ft_strdup
      • 📑Additional functions
        • ft_substr
        • ft_strjoin
        • ft_strtrim
        • ft_split
        • ft_itoa
        • ft_strmapi
        • ft_striteri
        • ft_putchar_fd
        • ft_putstr_fd
        • ft_putendl_fd
        • ft_putnbr_fd
      • 📑Bonus functions
        • ft_lstnew
        • ft_lstadd_front
        • ft_lstsize
        • ft_lstlast
        • ft_lstadd_back
        • ft_lstdelone
        • ft_lstclear
        • ft_lstiter
        • ft_lstmap
  • 1️⃣ Rank 01
    • Born2beRoot
      • 📠What's a virtual machine ?
      • 📠Install your virtual machine
      • 📠P2P Evaluation - Questions
    • ft_printf
      • ▪️Variadic functions
      • ▪️Building the thing
    • get_next_line
      • ▪️open() & read()
      • ▪️Static variables
      • ▪️Building the thing
      • ▪️Commented solution
  • 2️⃣ Rank 02
    • so_long
      • ▪️Understand so_long
      • ▪️Core concepts
      • ▪️Building the thing
    • pipex
      • ▪️Understand pipex
      • ▪️Functions used
      • ▪️Building the thing
    • minitalk
      • ▪️Understand minitalk
      • ▪️Functions used
      • ▪️Building the thing
    • push_swap
      • ▪️Algorithms
      • ◾Building the thing
    • FdF
      • 🗡️Understand FdF
      • 🗡️Graphics programming
      • 🗡️Building the thing
  • 3️⃣ RANK 03
    • Philosophers
      • ▪️Understand Philosophers
      • ▪️Functions used
      • ▪️Building the thing
    • Minishell
      • ▪️Understand Minishell
      • ▪️Functions
      • ◾Building the thing
  • 4️⃣ RANK 04
    • CPP (00 - 04) (doing)
      • CPP00
      • CPP01
      • CPP02
      • CPP03
      • CPP04 (doing)
    • NetPractice
      • Theory
      • Level 1 & 2
    • MiniRT
      • Understand MiniRT
      • Building the thing
  • 5️⃣ RANK 05
    • CPP (05-09) (to-do)
      • CPP05
      • CPP06 (to-do)
      • CPP07
      • CPP08 (to-do)
      • CPP09 (to-do)
    • Inception (doing)
      • 🕓The basics (Docker, Images, etc...)
      • Project Files
    • webserv (to-do)
  • 6️⃣ rank 06
    • ft_transcendence (to-do)
  • 🛂Exams
    • Exam Rank 02
      • Level 1
        • first_word
        • fizz_buzz
        • *ft_putstr
        • *ft_strcpy
        • *ft_strlen
        • ft_swap
        • repeat_alpha
        • rev_print
        • rot_13
        • rotone
        • search_and_replace
        • ulstr
      • Level 2
        • alpha_mirror
        • camel_to_snake
        • do_op
        • *ft_atoi
        • *ft_strcmp
        • ft_strcspn
        • ft_strspn
        • *ft_strdup
        • ft_strpbrk
        • ft_strrev
        • inter
        • last_word
        • ft_is_power_2
        • max
        • print_bits
        • reverse_bits
        • wdmatch
        • swap_bits
        • union
        • snake_to_camel
      • Level 3
        • add_prime_sum
        • epur_str
        • expand_str
        • ft_atoi_base
        • ft_list_size
        • ft_range
        • ft_rrange
        • hidenp
        • lcm
        • paramsum
        • pgcd
        • print_hex
        • rstr_capitalizer
        • str_capitalizer
        • tab_mult
      • Level 4
        • flood_fil
        • fprime
        • ft_split
        • ft_itoa
        • ft_list_foreach
        • ft_list_remove
        • rev_wstr
        • rotstring
        • sort_int_tab
        • sort_list
    • Exam Rank 03
    • Exam Rank 04
    • Exam Rank 05
      • Module 0
  • 👥Team
Powered by GitBook
On this page
  • Main topics
  • Orthodox Canonical Form
  • Ad-hoc polymorphism
  • Operator overloading

Was this helpful?

  1. 4️⃣ RANK 04
  2. CPP (00 - 04) (doing)

CPP02

PreviousCPP01NextCPP03

Last updated 1 year ago

Was this helpful?

Main topics

Ad-hoc polymorphism, operator overloading and Orthodox Canonical class form

Before diving into ad-hoc polymorphism and operator overloading, let's take a look at the new rules given in the subject of this module.

Orthodox Canonical Form, oh man that sounds complicated and... it's actually not that difficult.

It simply adds mandatory member functions to your classes, we'll see that in details in a few lines.

The second new rule, maybe you already did that, if not, look at the examples below and I think you'll get the idea pretty quickly.

Okay, so let's get to Orthodox Canonical Form...

Orthodox Canonical Form

As the subject says, to respect the OCF (Orthodox Canonical Form), you have 4 mandatory members functions, let's take an example of what it should look like:

example.hpp
#ifndef EXAMPLE_HPP
# define EXAMPLE_HPP

class Example
{
    public:
        Example(void);
        Example(const Example& ex);
        Example &operator=(const Example& e);
        ~Example(void);
}

#endif

The class declaration itself is not that hard except maybe for this line:

Example &operator=(const Example& e);

When I first saw that I was like Excuse me wtf, but it starts to make sense pretty quickly. It is "only" a way to tell your program how to act when you write the following code:

main.cpp
#include "Example.hpp"

int main(void)
{
    Example a;
    Example b;
    
    a.num = 10;
    a.name = "Simon";
    
    b = a; 
    // What values should be equal to which ?
    // this is what the operator overload let's us define
}

In the 4 mandatory member functions, you already know at least 2 of them, the default constructor and the destructor. Let's take a quick look at what the two other do.

First, the copy constructor. The copy constructor takes a reference to an object and constructs a new one based on the values of the referenced one.

Example.cpp
#include "Example.hpp"

Example::Example(const Example& ex) : _val1(ex._val1) /* ... */
{
    return ;
}

I think you get the gist, the name copy constructor is pretty clear.

Secondly, let's look at the assignment operator overload.

This is also connected to the Operator overload main topic.

Example.cpp
#include "Example.hpp"

Example &Example::operator=(const Example& ex)
{
    // in this case, we can't use the initialization list since
    // it's not a constructor function
    this->_val1 = ex._val1;
    return (*this);
}

Ad-hoc polymorphism

I'm not insulting you, I promise. Let me explain.

What the f*** is ad-hoc polymorphism ?

Remember that, like in C, C++ is a typed language, so if you write a function taking an int as parameter, you'll not be able to pass it a unsigned int. But, there's something you can do in C++ that you can't in C, called function overloading, and that's how ad-hoc polymorphism is built.

First, try to compile the following C code.

main.c
#include <stdio.h>

void print(int i);
void print(unsigned int j);

int main(void)
{
    int i = 10;
    unsigned int j = 100;
    
    print(i);
    print(j);
    
    return (0);
}

void print(int i)
{
    printf("Value is: %d\n", i);
}

void print(unsigned int j)
{
    printf("Value is: %u\n", j);
}

If everything's good, you should get an error like

main.c:4:6: error: conflicting types for 'print'

And that's totally normal, you have to functions with the same name but taking different argument types, and that's not possible in C.

Now, let's try the following C++ code.

main.cpp
#include <iostream>

void print(int i);
void print(unsigned int j);

int main(void)
{
    int i = 10;
    unsigned int j = 100;
    
    print(i);
    print(j);
    print(i, j);
    
    return (0);
}

void print(int i)
{
    std::cout << "Value is: " << i << std::endl;
}

void print(unsigned int j)
{
    std::cout << "Value is: " << j << std::endl;
}

void print(int i, unsigned int j)
{
    std::cout << "Values are: " << i << " and " << j << std::endl;
}

And now... surprise, no errors at compile time, not in C++ and why is that ?

That's because function overloading is authorised in C++. Function overloading allows us to define two or more function with the same name in the same scope.

Ad-hoc polymorphisme works with function overloading, this will lead to code duplication. For each type you want your function to work with, you have to write it completely from 0 with another type.

There's also something called Parametric polymorphism, but that's another topic for a later time (-> CPP07)

Operator overloading

And now, let's get to the last topic of this module, operator overloading.

As you saw in the last part, you can overload function in C++ by defining two or more function with the same name. Now, I'll show you how you can overload operators like =, >=, <=, ==, and a lot of others.

I'll only show example with some operator overloads, the other types works mostly the same with a little twist in the declaration but I'll let you search for the specific twists needed when you reach exercise 02 of this module.

Example.cpp
#include "Example.hpp"

// Assignment operator
Example &Example::operator=(const Example& ex) {...} // you already saw that one

// Comparison operator (only one of the many available)
bool Example::operator>(const Example& ex)
{
    return (this->_val > ex._val);
}

// Arithmetic operator
Example Example::operator+(const Exammple& ex) const
{
    return (Example(this->_val + ex._val));
}

// Output stream operator
std::ostream &operator<<(std::ostream &o, const Example &ex)
{
    o << "Value is: " << ex._val << std::endl;
    return (o);
}

As said before, I won't show you all the operator overload available (see below), but only the main ones and this gives you an idea of most of the declarations. A little Google search will get you to what you want easily now that you have the correct terms to look for.

Helper Script

If you read through everything and got there, you deserve some kind of "treat", click to find a bash script that will generate the files and template the required functions for Orthodox Canonical Form automatically for you.

here
CPP02 - New rules
All the available operator that you can overload... -
cppreference.com