Note: I'm not near a C++ compiler, but hopefully the below logic will be helpful to you.
Create an interface that has two options for every subclass of its type--
class DoSomething{
public:
DoSomething(){}
virtual void option1() const = 0;
virtual void option2() const = 0;
};
-- then create some subclasses--
class DoNothing : public DoSomething{
public:
DoNothing(){}
virtual void option1() const {}
virtual void option2() const {}
}; Then change your function to (by default) do nothing if no parameter is given,
or when an appropriate DoSomething object is given it will perform the action--
void YesorNofunc(const DoSomething& action = DoNothing() )
{
cout << "Are you sure?\n\n"
<< "[1] Yes\n"
<< "[2] No\n\n";
char YesorNo;
cin >> YesorNo;
if(YesorNo == 1)
{
action.option1();
}else if(YesorNo == 2)
{
action.option2();
}else {
cout << "Invalid; Try Again.";
}
}
-- hopefully this will give you an idea of what to do to make your method reusable. Have fun =)
Edit: Just got to a compiler, here's a running example--
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::flush;
class DoSomething{
public:
DoSomething(){}
virtual void option1() const = 0;
virtual void option2() const = 0;
};
class DoNothing : public DoSomething{
public:
DoNothing(){}
virtual void option1() const {}
virtual void option2() const {}
};
class Speak : public DoSomething{
public:
Speak(){};
virtual void option1() const {
cout << "Whoo! O_O" << endl;
}
virtual void option2() const {
cout << "Oh no >_<" << endl;
}
};
void YesorNofunc(const DoSomething& action = DoNothing() )
{
cout << "Are you sure?\n\n"
<< "[1] Yes\n"
<< "[2] No\n\n";
int YesorNo;
cin >> YesorNo;
if(YesorNo == 1)
{
action.option1();
}else if(YesorNo == 2)
{
action.option2();
}else {
cout << "Invalid; Try Again.";
}
}
int main(){
YesorNofunc(Speak());
cin.ignore();
cin.get();
return 0;
}