Q:

Propose to the user to rewrite functions with lambda

I'm doing tests to understand lambdas. I am trying to offer to the users the ability to rewrite a function directly in the main function. Let me explain:
#include <iostream>
using namespace std;

class A {
public:
    virtual bool execute() {};
};

class B : public A {
public:
    bool execute() { cout << "Execute in B" << endl; }
};

int main() {

    B newB;
    newB.execute();
    newB.execute() { cout << "Execute in New B" << endl; } ;
    newB.execute();

    return 0;
}

This source code doesn't work because it's illegal to rewrite a function like that. What would be for you, the best way to rewrite a function like that in C ++ 14? With lambda? Without lambda?
I want do like in Javascript, overloading a function like that: newB.somefunction = function(...) { ... };. I want the function to be written in source code by a user of my Library. In a way a callback function.
my question is the following: How to write a callback function or Lambda expressions to rewrite a method outside a class/object?
Solution proposed by Exagon with variable :
#include <iostream>
#include <functional>
class B {

public:
    int global=0;
    std::function<void()> execute{
        [](){
            std::cout << "Hello World" << std::endl;
        }
    };
};

int main() {

    B newB;
    newB.execute();
    newB.execute();

    newB.execute = [newB](){std::cout << newB.global << "  = FOOBAR\n";};

    newB.execute();
    return 0;
}

A:

You can do this using std::function from the functional header.
Then make a std::function member and create a setter for this member.
the execute member function need to call this std::function member.
you can pass a lambda into the setter method.
here is my approach:
#include <iostream>
#include <functional>

class B {
public:
    void execute() {_f();}
    void setFunction(std::function<void()> f){ _f = f;}
private:
    std::function<void()> _f{[](){std::cout << "Hello World" << std::endl;}};
};

int main() {

    B newB;
    newB.execute();
    newB.execute();

    newB.setFunction([](){std::cout << "FOOBAR\n";});

    newB.execute();
    return 0;
}

the output is:
Hello World
Hello World
FOOBAR

Since you are after something "JavaScript-like" you could do it like this:
#include <iostream>
#include <functional>
class B {
public:
    std::function<void()> execute{
        [](){
            std::cout << "Hello World" << std::endl;
        }
    };
};

int main() {

    B newB;
    newB.execute();
    newB.execute();

    newB.execute = [](){std::cout << "FOOBAR\n";};

    newB.execute();
    return 0;
}

Which has the same output.
here is a live demo

