-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfuncoverload.cpp
More file actions
50 lines (38 loc) · 768 Bytes
/
funcoverload.cpp
File metadata and controls
50 lines (38 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* overload.cpp */
#include <iostream>
using namespace std;
class vector {
public:
int[] elements;
vector(int[] array);
int dim(){return sizeof(elements)/sizeof(*elements)};
};
// constructor
vector::vector(int[] array){
elements = array;
}
// operator overload
vector vector::operator+(x, y){
int vdim = x.dim();
vector z(int[] array = new int[vdim]);
for(int i = 0; i < vdim; i++){
z(i) = x(i) + y(i);
}
return z;
}
vector vector::operator[](x, i){
return x.elements[];
}
int main()
{
// define a, b and compute c
vector a([2,3,5]), b([7,5,9]);
vector c = a + b;
// print out c
cout << "c = [";
for(int i = 0; i < c.dim(); i++){
cout << c(i);
if (i < c.dim() - 1){cout << ", ";}
}
cout << "]\n";
}