SHOW:
|
|
- or go back to the newest paste.
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 | - | MyClass(int*, int*); |
12 | + | |
13 | MyClass(); | |
14 | MyClass(int*, int, int*, int); | |
15 | MyClass(const MyClass&); | |
16 | ~MyClass(); | |
17 | void input(); | |
18 | void output(); | |
19 | - | MyClass::MyClass(int* t1, int* t2) |
19 | + | void operator=(MyClass&); |
20 | }; | |
21 | - | array1 = new int[strlen(t1) + 1]; |
21 | + | |
22 | - | srcpy(array1, t1); |
22 | + | MyClass::MyClass(int* t1,int n1, int* t2, int n2) |
23 | - | array2 = new int[strlen(t2) + 1]; |
23 | + | |
24 | - | srcpy(array2, t2); |
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[strlen(ob.array1) + 1]; |
35 | + | |
36 | - | strcpy(array1, ob.array1); |
36 | + | |
37 | - | array2 = new int[strlen(ob.array2) + 1]; |
37 | + | |
38 | - | strcpy(array2, ob.array2); |
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 | - | char buffer[100]; |
49 | + | |
50 | - | cin >> buffer; |
50 | + | |
51 | MyClass::~MyClass() | |
52 | - | array1 = strdub(buffer) |
52 | + | |
53 | delete[]array1; | |
54 | delete[]array2; | |
55 | } | |
56 | ||
57 | - | cout << "Result: " << array1 << endl << array2; |
57 | + | |
58 | { | |
59 | delete[] array1; | |
60 | delete[] array2; | |
61 | ||
62 | cout << "Input size1: "; | |
63 | cin >> size1; | |
64 | - | cout << "Hello world!" << endl; |
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 | } |