C++ Store Object to index from an Array

Hello, can you help me with this question.
A store () method gets an object, calculates an index for the array using getKey () modulo size, and stores the object below that index.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class ObjectTable : public HashKey {

public:
   char arr[50];
   int size;

//Override Methode getKey()
 public: 
    int getKey(int key){  }

//???
void store()  {

ObjectTable ob;

int index;
arr[index];
index = getKey(index) % size;

index = ob;         
} 



This is what i got so far. But I'm having problems with storing the object to the index. Is this way correct and what should I change.
The getKey method, is an abstract Method from another class.

1
2
3
4
5
6
class HashKey {

virtual int getKey(int key) = 0 {
    return key;
} };
getkey should probably compute the key from the data in the object. You do this so you can search for it easily later; the user enters the data, you re-compute the key, check that location, its there or not.

one really simple way to generate a key is to seed the random library with the data and take the first random number from it -- letting your libraries do the dirty work.

Thank you! I'm still confused with the exact storing of the Object. It's not very clear for me, how to store it to the index from the array? Sorry if it's a stupid question. Im very new to programming.
Last edited on
A store () method gets an object
I don't see an object being passed into store.

An object table is a hashKey? That doesn't make sense to me. Shouldn't an object be a hashKey?

At lien 14 you define an ObjectTable object inside the store method. That's almost certainly wrong. The ObjectTable instance that you should be working with is the "this" object that store was called on. Put another way, the code will call store like this:
1
2
3
ObjectTable someTable;
...
someTable.store();   // store() should insert into someTable. 

In C++, your store method can access the members of whatever object it was called on by referring to the member names directly. You can also access the whole object through the "this" pointer.

I think it would be helpful if you post the full assignment. That will let us give more specific advice.
Thank you very much!

Here is the full assigment:

1) Write an abstract class HasKey with the abstract function getKey, which returns an int value as the key of the object.
2) Write a class ObjectTable that can store all possible objects whose classes are derived from HasKey. The class should have the following properties: a) A constructor has as parameter the size of the array for storing the objects and initializes the array appropriately. The size of the array is stored in the attribute size, which can not change afterwards. b) A method store () receives an object, calculates an index for the object using getKey () modulo size and stores the object below this index.

Note the rules of inheritance and polymorphism.
2) Write a class ObjectTable that can store all possible objects whose classes are derived from HasKey.

So, ObjectTable isn't derived from HashKey. ObjectTable stores objects that are derived from HashKey.
Let's do this one thing at a time. Can you do this:
1) Write an abstract class HasKey with the abstract function getKey, which returns an int value as the key of the object.
class HashKey {

public :
virtual int getKey(int key) = 0 {
return key;
}
};

Is this not the same what I send at first or I'm doing something wrong here?
1) Please use code tags when posting code, to make it readable.

2) The way you've written your getKey function, it takes an integar value as an argument, and... returns it straight back to the calling code. What is the purpose of that?

3) What do you understand by the term "abstract function"?
Topic archived. No new replies allowed.