Operator Overloading - Exercise

#include <iostream>
#include <vector>
#include <algorithm>    //std::any_of
#include <numeric> //std::iota
using namespace std;

struct Predicate{
    int N;

    Predicate(int v):N(v){} //ctor

    bool operator()( const int val) const{
        return val >= N;
    }
};

int main(){
    vector<int> v1 {1,2,3,4,-1};

    std::vector<int> v2(10);
    std::iota(v2.begin(), v2.end(), 0); // creates range from 0 to 9


    int N=2;
    Predicate predicate{N};

    cout << "How many num of numbers greater or equal to "  << N << "?\n" <<
            std::count_if(v1.begin(), v1.end(), predicate) <<endl; //calls predicate() on each value of v

    cout << "How many num of numbers greater or equal to "  << N << "?\n" <<
         std::count_if(v2.begin(), v2.end(), predicate) <<endl;

    //cout << std::count_if(v.begin(), v.end(),[N](int a){ return (a >= N);}) <<endl; //lambda expression

    return 0;
}

std::iota creates range from the number of the 3rd param std::count_if count how many times the predicate returns true

Note: The better way would to be to replace our functor with a lambda expression

Revisiting the polynomial functor

ax2+bx+cax^2+bx+c

Same code but with color and different poly(x)

ADD IMAGE

Exercise

Find in the above code how many poly(3) are greater than 50 ax2+bx+c>50ax^2+bx+c>50

Erase the ostream and couts

Exercise

Lets add Polynomial addition

  • +

(1x2+2x+3)+(10x2+10x+10)(1x^2+2x+3) + (10x^2+10x+10)

=11x2+12x+13=11x^2+12x+1 3

  • +=

  • ++ - pre/postfix: this will add 1 to the c of the polynomial

ax2+bx+(c+1)ax^2+bx+(c+1)

SFML version

Before we begin lets install SFML

To compile

And to run

Lets now code it 😃

We should also normalize but we are going to keep it simple 1/maxfxHeight1/max*fx*Height

Polynomial 3

ax3+bx2+cx+dax^3+bx^2+cx+d

Add:

  • Integrals: prefix,postfix

  • Derivatives: prefix,postfix

Our definition of integration (bx2+cx+d)dx=bx33+cx22+dx+0\int (bx^2 + cx +d ) dx = \frac{bx^3}{3} + \frac{cx^2}{2} +dx +0

(0x3+2x2+3x+4)dx=2x33+3x22+4x+0\int (0x^3+2x^2+3x+4)dx = \frac{2x^3}{3} + \frac{3x^2}{2} +4x +0

f(2x33+3x22+4x+0)=0x3+2x2+3x+4f' (\frac{2x^3}{3} + \frac{3x^2}{2} +4x +0) = 0x^3+2x^2+3x+4

SFML version

Adding the [] operator

Thinking exercise

Lets say we would like to add < operator what would you do?

I suggest we we start we can compare the a of ax^3 and

return the lowest . If the they have the same the compare the b of ax^2.

Now we are able to sort our polynimials

If extra time

  • Convert polynomial to array type instead of _a,_b

  • Deep copy

Inline

Without inline

When the program executes the function call instruction

  • the CPU stores the memory address of the instruction following the function call

  • copies the arguments of the function on the stack

  • and finally transfers control to the specified function.

The CPU then executes the function code

  • stores the function return value in a predefined memory location/register

  • and returns control to the calling function.

With inline

When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time

Remember: inlining is only a request to the compiler, not a command Note: all the functions defined inside the class are implicitly inline.

Syntax

use the inline keyword before the function declaration

Example

Last updated