title: Create a variable to hold objects of different types C++
body: <p>I have 3 different objects <code>A</code>, <code>B</code> and <code>C</code>. Depending on the the parameter given, I would like to choose among these different objects. In programming, </p>
  
  <pre><code>class A { 
    public: 
      void printHello() { cout &lt;&lt; "HELLO A" &lt;&lt; endl; }
  }; 
  
  class B { 
    public: 
      void printHello() { cout &lt;&lt; "HELLO B" &lt;&lt; endl; }  
  };
  
  class C { 
     public: 
       void printHello() { cout &lt;&lt; "HELLO C" &lt;&lt; endl; }  
  }; 
  
  int main () { 
      string key = "c"; 
      A a; 
      B b; 
      C c; 
      Object obj; // I don't know how to declare Object. 
  
      if (key == "a") obj = a; 
      else if (key == "b") obj = b; 
      else obj = c;
      obj.printHello(); // print Hello C.
      return 0; 
  } 
  </code></pre>
  
  <p>I have thought about templates and struct objects. But none of them work so far. </p>
  
  <pre><code>template&lt;typename T1, T2, T3&gt; 
  T1 getType(string key, T1 t1, T2 t2, T3 t3) { // this is problem coz return types are different.
      if (key == "a") return t1; 
      else if (key == "b") return t2; 
      else return t3; 
  } 
  </code></pre>
  
  <p>It is easy to create <code>Object o;</code> in <code>JAVA</code> because every object in Java is subclass of <code>Object</code> class. But how do I achieve this in C++?</p>
  
  <p>Edit.
  I can't change class <code>A</code>, <code>B</code> and <code>C</code>. I was given these classes to work with and my goal is to implement <code>main</code> method. So, <code>inheritance</code> was out of the question for me. Sorry for any confusion. </p>
  
  <p>Any help is appreciated. </p>