Okay, this is my idea: the print member function should not be virtual since the output of a.print() in printit() appears to be always from A::print(). You can have a protected value member variable of type int in A, and no member variables declared for B. The copy assignment operator for A should not copy over value.
I found it mind boggling to think of what exactly should be done to make it such that b1.print() gives $2 yet printit(b1) does not, so I came up with a simple hack: add a protected flag member variable of type bool in A. Overload the copy assignment operator for B such that it copies over the value and sets flag to true. Now, in A::print(), print 0 if flag is true, else print value. B::print() unconditionally prints value.
Oh, and I have ignored the simplest hack of all:
Code:
class A
{
public:
void print() const {}
};
class B : public A
{
public:
B(int x = 0) {}
void print() const
{
std::cout << "Just got my paycheck!\n"
"Direct deposit not working :(\n"
"Just got my paycheck!\n"
"Direct deposit not working :(\n"
"Withdrawing funds.\n"
"Just got my paycheck!\n"
"Depositing funds.\n"
"Funds withdrawn: $2\n"
"Funds deposited: $0\n"
"Funds deposited: $0\n"
"Funds deposited: $2\n"
"Funds deposited: $0\n"
"No need to find another ATM.\n"
"Now I have to drive to the bank across town.\n"
"No need to find another ATM.\n"
"Now I have to drive to the bank across town.\n"
"No need to find another ATM.\n";
}
};