Q: Understanding design with polymorphism (C++) Coming from my background in dynamic languages, I find I have a problem expressing my intentions in a statically-typed language such as C++.
I'm designing a preference system for my application. As every preference will have a few associated values (the default value, limits, an observer function...) I decided to encapsulate each preference in an object of its own. Here's my first draft:
class Preference    // purely abstract class
{
    parseFromString(String s) = 0;
    get() = 0;
    void set(newVal) = 0;
private:
    // internal data
};

Now I need to create a few derived classes, like IntPreference, FloatPreference and StringPreference. Here's how their declaration would look like:
class IntPreference : Preference          class StringPreference : Preference
{                                         {
    int parseFromString(String s);            String parseFromString(String s);
    void set(int newVal);                     void set(String newVal);
    // etc.                                   // etc.
}                                         }

Now that the set() method takes an int parameter in the class IntPreference and a String parameter in StringPreference, there's no way to declare this function in the base class. The same goes with the return value of parseFromString(). I understand this is impossible to do in C++, because functions with the same name and different parameter types in a derived class just overshadow, not override their ancestors. Again, this is how I would express myself in a dynamic language, what's the correct pattern in C++?
EDIT: Sorry, I forgot to mention I need a base class to store them all in a hash table:
Hash(const char *name, Preference pref);


A: What you have now, is a poor boost::any class and you maybe should
simply just use that.
Your parseFromString() member function is dubious. You use the
dynamic type to decide what to parse out of the string, something that
always has to be known statically.
class my_any {
public:
  template<typename T>
  explicit // don't rely on conversions too much
  my_any(const T& t) : x_(t) {}

  // might throw if the cast fails
  template<typename T>
  T& get() { return boost::any_cast<T&>(x_); }

  // also maybe move semantics
  template<typename T>
  set(const T& t) { x_ = t; }
private:
  boost::any x_;
};

// usage:
my_any m;
m.set(23);
try {
  int& x = m.get<int>();
catch(boost::bad_any_cast& ex) {
  // ...
}

// for setting things from string just do 
// the right thing at the call site of set

If you dislike templates you can simply provide a few defaults:
my_any::getInt(); my_any::getString();

EDIT: If boost::any is too generic for you and you want to limit
your construct to a certain set of values use
boost::variant. Although a variant has a larger impact on compile
time and can be quite hard to use for a beginner.
EDIT2: The hash table problem:
typedef boost::unordered_map<std::string, my_any> preference_table;
preference_table t;
// i added a template constructor to my_any
t.insert(std::make_pair("Foobar", my_any(23)));


A: Woah - slow down there!  You have a strongly typed language.  That's not a design flaw, it's deliberate: it's meant to be a little restrictive so it can do compilation-time checks on your program's correctness and produce much faster, cleaner code.  Please do not go out of your way to throw away type safety by creating some type-ambiguous interface!  There's nothing in your question to suggest you have any need to do so.
Consider doing something like:
struct Config
{
    int max_for_whatever_;
    string name_for_whatever_;
    double scaling_factor_for_whatever_else_;
    bool verbose_;
};

As you parse the inputs, you can populate the specific related member variable.
Now, there's lots of good libraries to do that.  The leading general-purpose C++ 3rd-party library is "boost", which has argument parsing facilities.  And while some C++ compilers ship with enhanced versions of this (specifically, the GNU C++ compiler has an extended getopt supporting "long" option format for command line parameters like "--flag" rather than just "-f"), the tried and trusted UNIX/Linux facility getopt() can be used as in:
int c;
Config config = { 20, "plugins", 29.3, true };
while ((c = getopt(argc, argv, "m:n:s:v")) != EOF)
{
    switch (c)
    {
      case 'm': config.max_for_whatever_ = lexical_cast<int>(optarg); break;
      case 'n': config.name_for_whatever_ = optarg; break;
      case 'd': config.scaling_factor_for_whatever_ = lexical_cast<double>(optarg); break;
      case 'v': config.verbose_ ^= true; break;
      default:
        std::cerr << argv[0] << ": unsupported option '" << c << "' - exiting\n";
        exit(EXIT_FAILURE);
    }
}

// then, use the configuration parameters directly by name...

The concepts are the same whether you're reading from a configuration file, command line arguments, some manner of registry: as you encounter specific configuration values try to write them into properly-typed and -named variables specific to their import in the code.

A: Personally I wouldn't create separate classed for each of those things. They are not interchangable, you can't give an IntPreference sometimes that wants a StringPreference...  If you pass an abstract "Preference" to a function it's going to be expecting that to be a specific type in order to make use of the data.
I wouldn't create subsclasses at all here, I would have a Preference class that has separate functions getIntValue(), getStringValue() etc.

A: I don't think you can have a single interface if your preferences have different types of values. This is what comes to mind:
class IPreference
{
public:
  virtual ~IPreference() {};
  virtual void Parse( std::istream& s ) = 0;
  virtual void Serialize( std::ostream& s ) = 0;
};

template <typename T>
class Preference : public IPreference
{
public:
  const T& Get() const { return m_value; }
  void Set(const T& value) const { m_value = value; }
private:
  T m_value;
};

I would try to put as much logic in the base class as possible. But if you want to avoid type-polling I think you might need one class per property type here.
On a second thought, you might assume that all your property types support reading from/writing to std::streams. Then you could just go with templates and work with stringstream (if you read from strings).

A: You want to use templates:
template <typename T>
class ChildPreference
{

    T parseFromString(std::string s) {
        //todo
    }    
    void set(T newVal) {
        //todo
    }                 
    // etc.                                   
}

ChildPreference<int> intObj;
ChildPreference<float> fltObj;
ChildPreference<std::string> strObj;

Note with templates you must define functions in place.

A: if Base declares a member function set(int x), and Derived declares a member function set(string c) (same name but different parameter types and/or constness), then the Base set(int x) is "hidden" rather than "overloaded" or "overridden" (even if the Base set(int x) is virtual)
Your Answer is in this link

A: You could use templates, as suggested in a comment by Jacob S. It would look something like this:
template<class T>
class Preference
{
public:
    parseFromString(std::string s) = 0;
    T get() { /* some implementation */ }
    void set(T newValue) { /* some implementation */ }

private:
    T value_;
};

You would use it like:
Preference<int> intPrefs;
Preference<std::string> stringPrefs;

