Create an array class

The requirement is to write an array class, include square bracket getter and setter, a getter to return data structure's capacity...

And then we need to use the class we just created. Have the user enter a pair of numbers at the same time, one as index one as value, the program needs to store it and output the value when user looking it up by index...

The operator overloading is just like magic to me... Can't understand it at all... This is what I got so far... And obviously, this just can't compile...

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
#include <iostream>
using namespace std;

class Array{
public:
	Array();
	int values[100];
	int capacity() const;
	int operator[](int i) const;
	int &operator[](int i);
};
 
Array::Array(){
	for(int i = 0; i < 100; i++)
		values[i] = 0;
}

int Array::capacity() const{
	return sizeof(values[100]);
}

int Array::operator[](int i) const{
	return values[i];
}

int &Array::operator[](int i){
	return values[i];
}
int main(){ 
	int idx = 0;
	int arr0[100]; 
	int arr1[100];
	string str;
	Array mainArray(arr0, arr1);
	while(str != 'q' && str != 'Q'){
		cout << "Input an index and a value [Q to quit]:";
		cin.get(str);
		mainArray[str];
		idx++;
	}
	cout << "You stored this many values: " << idx << endl;
	cout << "The index-value pairs are:\n";
	for(int i = 0; i < idx; i++)
		cout << arr0[i] " => " << arr1[i] << endl;
	cout << "Input and index for me to loop up [Q to quit]:";
	//unfinished
}
Topic archived. No new replies allowed.