嘿伙计们我正在尝试继承这个功能" addItem"从基本类Hash到可派生类Word。我怎样才能完成这个方法。
List l;
List ls;
class Word: public Hash { //Hash is the basic class and Word is the derivable
public: char c;
string word;
public: Word() {
word = "";
}
void read(const char * filename);
virtual void Word::addItem(string name); //how can I inherint
};
void Word::read(const char * file) {
fstream myfile;
myfile.open(file);
if (!myfile) {
cout << "No file..............";
exit(1);
}
//string word = "";
while (myfile.get(c)) {
if ((int) c > 64 && (int) c < 91 || (int) c > 96 && (int) c < 123) {
word += c;
} else {
if (word.length() > 1) {
l.insertLast(word);
//myHash.addItem(word);
}
word = "";
}
}
for (int i = 0; i < l.Size(); i++) {
string str = l.elementAtRank(i);
for (int j = 0; j < str.length(); j++) {
str[j] = tolower(str[j]);
}
ls.insertLast(str);
}
myfile.close();
};
class Hash {
public: static const int startTableSize = 2;
int tableSize;
int r;
//List *hashTable[tableSize];
List * * hashTable;
int maxSize;
float threshold;
int tableCount;
public: Hash() { //Here is the base class
tableCount = 0;
threshold = .5;
r = 1;
tableSize = pow(2.0, (double) r);
maxSize = startTableSize * threshold;
hashTable = new List * [tableSize];
for (int i = 0; i < tableSize; i++) {
hashTable[i] = new List;
}
};
int getSize();
int hash(string key);
void addItem(string name);
void changSize();
void display();
~Hash() {
for (int i = 0; i < tableSize; i++) {
delete hashTable[i];
}
delete[] hashTable;
}
};
void Hash::display() {
for (int i = 0; i < tableSize; i++) {
//cout << l[i];
}
}
void Hash::addItem(string name) {
int index = hash(name);
//hashTable[index] = new List();
if (hashTable[index] - > size == 0) {
tableCount++;
}
hashTable[index] - > insertLast(name);
if (tableCount >= maxSize) {
changSize();
}
}
int Hash::getSize() {
return tableSize;
}
int Hash::hash(string key) {
//int hash = 0; int index;
//for (int i = 0; i < key.length(); i++){
// hash += (int)key[i];
// index = hash%tableSize;
//}
//return index;
int hash = 0;
int index;
int scalar = 23345;
for (int i = 0; i < key.length(); i++) {
//hash += (int)key[i]*(1000^i);
hash += (int) key[i];
}
index = (scalar * hash) % 32768 >> (15 - r);
return index;
}
void Hash::changSize() {
int lastSize = tableSize;
tableSize = tableSize * 2;
r++;
maxSize = tableSize * threshold;
List * * lastTable = hashTable;
tableCount = 0;
hashTable = new List * [tableSize];
for (int i = 0; i < tableSize; i++) {
hashTable[i] = new List;
}
for (int j = 0; j < lastSize; j++) {
List * hash = lastTable[j];
//while (hash->Size() != 0){
// //addItem(lastTable[j]->removeLast->data);
//}
for (int i = 0; i < hash - > size; i++) {
addItem(hash - > elementAtRank(i));
}
}
};
int main(int argc, char * argv[]) {
//Hash myHash;
int x;
Word word;
word.read("input.txt");
for (int i = 0; i < ls.Size(); i++) {
word.addItem(ls.elementAtRank(i));
cout << ls.elementAtRank(i) << '\n';
}
cin >> x;
return 0;
};
答案 0 :(得分:1)
将virtual
关键字添加到void addItem(string name);
类中的方法Hash
,并在Word
类中为该方法实现一个主体。
示例强>
virtual void addItem(string name);
和
void Word::addItem(string name)
{
// Body
}
答案 1 :(得分:1)
您需要在基类中将addItem
标记为virtual
。
事实上,在派生类中标记为virtual
是多余的。