Q:

defining functions on-the-fly in C++

In C++, is it possible to implement one Class's function(s) within another (unrelated except by dependency) Class's function(s)?  The behavior I'm looking for can be achieved in Java using abstract methods and/or interfaces which are implemented within anonymous inner-classes like so: 
Interface IFoo{

public void doFoo();

}

Class Bar {
private IFoo impl;
public Bar(IFoo implArg){
    impl = implArg;
}

}
static void main(String[] args) {
Bar myBar = new Bar(new IFoo(){
    @Override
    public void doFoo(){
        System.out.println("hello foo impl!");
    }
});
}

Can this same effect be achieved in C++ and, if so, how?  The use case I have in mind is on-the-fly definition of rules something like the following:
AbstractRule.h
#ifndef _ABSTRACT_RULE_H_
#define _ABSTRACT_RULE_H_

#include <stdio.h>

class AbstractRule{
private:

public:
AbstractRule();
~AbstractRule();
void enforceAbstractRule();
};

#endif

AbstractRule.cpp
#include "AbstractRule.h"
AbstractRule::AbstractRule(){
}
AbstractRule::~AbstractRule(){
}

void AbstractRule::enforceAbstractRule(){
printf("placeholder impl of enforceRule-- should not see this in production");
}

Main.cpp
#include "AbstractRule.h"

int main(int argc, char** argv){
AbstractRule myRule;
//do something here so that myRule.enforceAbstractRule() call prints "enforced 
    //myRule!"
myRule.enforceAbstractRule();
}

The question is, what do I need to do at the 'do something here...' comment to achieve the desired effect?

A:

Java has something called "Nameless classes", which is what your example uses - note that your "concept" is slightly "wrong" in that you are not actually declaring a function "on the fly", you are declaring a new class "on the fly". 
In C++ all classes have to have a name [1]. 
So, for what you want to do, you need to declare a class that extends the baseclass:
class MyRule : public Rule
{
   void enforceRule();
};

void MyRule::enforceRule()
{
   ... stuff goes here ... 
}

You can of course place the declaration inline inside the class declaration as well. But for this to be possible to use, you need to give it a name. 
Now, your main will have:
MyRule myRule;

[Or, more likely, you call a factory function that selects the "correct" rule class, creates a new object, and return a pointer to that object]
[1] Not strictly true always, but for this particular use, you do need a class-name. 

