Advertisement
dllbridge

Untitled

Dec 29th, 2023
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #include  <iostream>
  5. #include    <string>
  6. using namespace std;
  7.  
  8. template<class T, class U>
  9. //////////////////////////////////////////////////////
  10. class MyClass
  11. {
  12.    
  13.        
  14.     T        value_;
  15.     U anotherValue_;   
  16.    
  17.   public:
  18.     MyClass(T t, U u) : value_(t), anotherValue_(u) {}
  19.  
  20.     T getValue() //const
  21.     {
  22.         return value_;
  23.     }
  24.    
  25.    
  26.     U getAnotherValue() const { return anotherValue_; }
  27.  
  28.   private:
  29.  
  30. };
  31.  
  32.  
  33.  
  34. ///////////////////////////////////////////////////////
  35. int main()                                           //
  36. {
  37.   MyClass<int, string> myObject(42, "Hello, world!");
  38.  
  39.   cout << myObject.getValue() << endl;
  40.   cout << myObject.getAnotherValue() << endl;  
  41.  
  42.   MyClass<float, int> Dima(3.14, 777);
  43.   cout << Dima.getValue()        << endl;
  44.   cout << Dima.getAnotherValue() << endl;  
  45.  
  46.  
  47.   return 0;
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. /*
  58. //  Help
  59.  
  60. template <typename T>
  61. class Person
  62.  
  63.  
  64. */
  65.  
  66. /*
  67.  
  68. #include  <iostream>
  69. using namespace std;
  70.  
  71.  
  72.  
  73.  
  74.  
  75. template <typename T>
  76. ///////////////////////////////////////////////////////
  77. class Person
  78. {
  79.    
  80.    private:      T    id;
  81.             string  name;  
  82.    
  83.    public:
  84.    
  85.    
  86.    Person()
  87.    {
  88.        
  89.        
  90.    }       
  91.        
  92.    Person(T T_id, string str)  // : id{id}, name{name}
  93.    {
  94.        
  95.         id   = T_id;       
  96.         name = str;    
  97.    }       
  98.    
  99.    void set_name(string str)
  100.    {
  101.        
  102.         name = str;        
  103.    }   
  104.        
  105.    void set_id(T T_id);
  106.  //{
  107.        
  108.  //     id   = T_id;               
  109.  //}
  110.  
  111.  
  112.     void print() const
  113.     {
  114.         cout << "Id: " << id << "\tName: " << name << endl;
  115.     }
  116. };
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. ////////////////////////////////////////////////////////
  127. int main()                                            //
  128. {
  129.    
  130.     Person <int>tom(123456, "Tom");                   // T - число        
  131.     tom.print();                                      // Id: 123456      Name: Tom
  132.    
  133.    
  134.     Person <float>Dima;
  135.    
  136.     Dima.set_name("Dmitry");
  137.     Dima.set_id  (0.47);
  138.    
  139.     Dima.print();
  140.    
  141.     Person <string>bob("tvi4xhcfhr", "Bob");          // T - строка
  142.     bob.print();                                      // Id: tvi4xhcfhr  Name: Bob
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. template <typename T>
  160. ////////////////////////////////////////////////////////
  161. void Person<T>::set_id(T T_id)                        //
  162. {
  163.        
  164.      id   = T_id;              
  165. }
  166.  
  167. */
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement