Question: I am trying to calculate the average number participants scored correct on a memory task. I have a column called `RecallType` which tells me if participants were assessed through forward memory recall (called `forwards`) or through backwards memory recall (called `backwards`). I also have a column called `ProbeState` which identifies the type of memory task, of which there are two. In this column I have `positions` and `digits`. These are all my variables of interest. 

The memory task itself is split by two columns. `Recall.CRESP` is a column specifying the correct answers on a memory test selected through grid coordinates. `Recall.RESP` shows participants response.

These columns look something like this:

```
|Recall.CRESP                     | Recall.RESP                     |
|---------------------------------|---------------------------------|                 
|grid35grid51grid12grid43grid54   | grid35grid51grid12grid43grid54  |                
|grid11gird42gird22grid51grid32   | grid11gird15gird55grid42grid32  |

```

So for example in row 1 of this table, the participant got 5/5 correct as the grid coordinates of `Recall.CRESP` matches with `Recall.RESP`. However in row 2, the participant only got 2/5 correct as only the first and the last grid coordinate are identical. The order of the coordinates must match to be correct.

Ideally I would love to learn from any response. If you do reply please kindly put some comments.

Thanks.

Answer: 
THe problem is that you have a non virtual `foo()` so that `A::bar()` will call the only `foo()` it knows, being `A::foo()`, even if it's B that invokes it. 

Try: 

```
class A {
public:
    A() { foo(); }
    virtual ~A() { foo(); }             // <<---------- recommendation
    virtual void foo() { cout << 3; }   // <<<--------- solves it
    void bar() { foo(); }
};
class B : public A {
    void foo() override { cout << 2; }  // <<---------- recommendation
};

```

**Additional infos:** 

Making `foo()` [virtual](https://www.geeksforgeeks.org/virtual-function-cpp/) in the base class allows each class to override this function, and be sure that the `foo()` that is invoked is will be the `foo()` corresponding to the object's real class. 

It's a [good practice](https://stackoverflow.com/q/497630/3723423) then to use the keyword `override` in the derived classes: it's not mandatory, but in case you make a typo in the functions signature, you'll immediately notice with a compile-time error message. 

Another good practice is to make your base class destructor virtual if you have at least one virtual function in the class. 

A final remark: in B, `foo()`'s private. This is legal, but it's weird because the inheritance says that B is a kind of A, but you can't use B objects exactly as an A object.
Member `A::foo` is non-virtual and will therefore be statically bound wherever used. So when compiling `A::bar`, the call to `foo()` will be (statically) bound to the implementation `A::foo()`. This statical binding in `A::foo` will not be changed by the fact that you create an instance of derived class `B` later on. 
If you call `b.foo()` in you main, however, `B::foo` will be bound.

In order to have `B::foo` to be called through `A::bar`, you'll have to declare `A::foo` as `virtual:

```
class A {
public:
    A() { foo(); }
    virtual ~A() { foo(); }
    virtual void foo() { cout << 3; }
    void bar() { foo(); }
};

```

Note that you'd also declare the destructor as virtual; non-virtual destructors do very rarely make sense.