fork download
  1. //
  2. // main.cpp
  3. //
  4. // Created by Rogiel Sulzbach on 7/22/14.
  5. // Copyright (c) 2014 Rogiel Sulzbach. All rights reserved.
  6. //
  7. // http://w...content-available-to-author-only...l.com/
  8. //
  9.  
  10. #include <iostream>
  11. #include <vector>
  12.  
  13. /**
  14.  * Base class for the template
  15.  */
  16. class MyBaseClass {
  17. public:
  18. /**
  19.   * Prints the value to the console
  20.   */
  21. virtual void print() = 0;
  22. };
  23.  
  24. /**
  25.  * A template that holds a value
  26.  * @tparam T the value type
  27.  */
  28. template<typename T>
  29. class MyTemplatedClass : public MyBaseClass {
  30. private:
  31. /**
  32.   * The value storage
  33.   */
  34. T value;
  35.  
  36. public:
  37. /**
  38.   * Creates a new instance
  39.   *
  40.   * @param value the value
  41.   */
  42. MyTemplatedClass(T value) : value(value) {
  43. };
  44.  
  45. // implementation
  46. virtual void print() {
  47. std::cout << value << std::endl;
  48. }
  49.  
  50. };
  51.  
  52. int main(int argc, const char * argv[]) {
  53. // creates two objects
  54. MyTemplatedClass<std::string> myObj1("Hello World!");
  55. MyTemplatedClass<int> myObj2(1000);
  56.  
  57. // inserts the two objects into a vector
  58. std::vector<MyBaseClass*> myVector;
  59. myVector.insert(myVector.end(), &myObj1);
  60. myVector.insert(myVector.end(), &myObj2);
  61.  
  62. // iterate them and call print()
  63. for(auto obj : myVector) {
  64. obj->print();
  65. }
  66.  
  67. // profit.
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Hello World!
1000