Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T, class S>
  5. class A
  6. {
  7. T t;
  8. S* s;
  9. int size;
  10.  
  11. public:
  12. A(const T& t = 6, int size = 2) : t(t), size(size)
  13. {
  14. s = new S[size];
  15. }
  16.  
  17. A(const A& other) : t(other.t), size(other.size)
  18. {
  19. s = new S[size];
  20. for (int i = 0; i < size; i++)
  21. s[i] = other.s[i];
  22. }
  23.  
  24. ~A()
  25. {
  26. cout << "~A t=" << t << " size=" << size << endl;
  27. delete[]s;
  28. }
  29.  
  30. int getSize() const { return size; }
  31. S& operator[](int index) { return s[index]; }
  32.  
  33. friend ostream& operator<<(ostream& os, const A& a)
  34. {
  35. os << a.t << " " << a.size << ": ";
  36. for (int i = 0; i < a.size; i++)
  37. os << a.s[i] << " ";
  38. os << endl;
  39.  
  40. return os;
  41. }
  42. };
  43.  
  44. template <class T>
  45. class Object
  46. {
  47. T t;
  48. int x;
  49. public:
  50. Object(const T& t = 5, int x = 5) : t(t), x(x) {}
  51. ~Object() { cout << "~Object t=" << t << " x=" << x << endl; }
  52.  
  53. operator T&() { return t; }
  54.  
  55. friend ostream& operator<<(ostream& os, const Object& o)
  56. {
  57. os << o.t << " " << o.x << endl;
  58. return os;
  59. }
  60. };
  61.  
  62. void foo()
  63. {
  64. A<int, Object<int> > a1(3, 3);
  65. cout << a1;
  66. cout << "1---------------------\n";
  67.  
  68. for (int i = 0; i < a1.getSize(); i++)
  69. (a1[i])++;
  70. cout << a1;
  71. cout << "2---------------------\n";
  72. }
  73.  
  74.  
  75. void main()
  76. {
  77. foo();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement