Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class MyClass
  7. {
  8.     int *array1;
  9.     int size1;
  10.     int *array2;
  11.     int size2;
  12. public:
  13.     MyClass();
  14.     MyClass(int*, int, int*, int);
  15.     MyClass(const MyClass&);
  16.     ~MyClass();
  17.     void input();
  18.     void output();
  19.     void operator=(MyClass&);
  20. };
  21.  
  22. MyClass::MyClass(int* t1,int n1, int* t2, int n2)
  23. {
  24.     size1 = n1; size2 = n2;
  25.     array1 = new int[size1 + 1];
  26.     for(int i = 0; i < size1 + 1; i++)
  27.         array1[i] = t1[i];
  28.     array2 = new int[size2 + 1];
  29.     for(int i = 0; i < size2 + 1; i++)
  30.         array2[i] = t2[i];
  31. }
  32.  
  33. MyClass::MyClass()
  34. {
  35.     array1 = new int; array1[0] = 0;
  36.     array2 = new int; array2[0] = 0;
  37. }
  38.  
  39. MyClass::MyClass(const MyClass& ob)
  40. {
  41.     size1 = ob.size1;
  42.     size2 = ob.size2;
  43.     array1 = new int[ob.size1 + 1];
  44.     for(int i = 0; i < size1 + 1; i++)
  45.         array1[i] = ob.array1[i];
  46.     array2 = new int[ob.size2 + 1];
  47.     for(int i = 0; i < size2 + 1; i++)
  48.         array2[i] = ob.array2[i];
  49. }
  50.  
  51. MyClass::~MyClass()
  52. {
  53.     delete[]array1;
  54.     delete[]array2;
  55. }
  56.  
  57. void MyClass::input()
  58. {
  59.     delete[] array1;
  60.     delete[] array2;
  61.  
  62.     cout << "Input size1: ";
  63.     cin >> size1;
  64.     cout << "Array1: ";
  65.     array1 = new int[size1 + 1];
  66.     for(int i = 0; i < size1 + 1; i++)
  67.         cin >> array1[i];
  68.  
  69.     cout << "Input size2: ";
  70.     cin >> size2;
  71.     cout << "Array2: ";
  72.     array2 = new int[size2 + 1];
  73.     for(int i = 0; i < size2 + 1; i++)
  74.         cin >> array2[i];
  75. }
  76.  
  77. void MyClass::output()
  78. {
  79.     cout << "Result: ";
  80.     for(int i = 0; i < size1 + 1; i++)
  81.     cout << array1[i] << " ";
  82.     cout << endl;
  83.     for(int i = 0; i < size2 + 1; i++)
  84.     cout << array2[i] << " ";
  85. }
  86.  
  87. void MyClass::operator=(MyClass& ob)
  88. {
  89.     if(this == &ob) return;
  90.  
  91.     delete[] array1;
  92.     delete[] array2;
  93.  
  94.     array1 = new int[size1 + 1];
  95.     for(int i = 0; i < size1 + 1; i++)
  96.         array1[i] = ob.array1[i];
  97.  
  98.     array2 = new int[size2 + 1];
  99.     for(int i = 0; i < size2 + 1; i++)
  100.         array2[i] = ob.array2[i];
  101. }
  102.  
  103. int main()
  104. {
  105.     MyClass A, B;
  106.     A.input();
  107.     B = A;
  108.     B.output();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement