c++ - 如何从基本类继承函数

标签 c++

大家好,我正在尝试将函数“addItem”从基本类 Hash 继承到派生类 Word。我怎样才能完成这个方法。

Word 类使用继承来使用函数 addItem。

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;
};    

最佳答案

需要在基类中将addItem标记为virtual

事实上,在派生类中标记为 virtual 是多余的。

关于c++ - 如何从基本类继承函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33171318/

相关文章:

c++ - 整数输入仅限于四位数字

C++ 循环不正确循环

c++ - 双向链表。逻辑错误

c# - 尝试从 native dll 获取字符串值

c++ - UDP Boost ASIO 异步客户端挂起

c++ - 在动态分配内存时使用指定大小

c++ - ACE/TAO 性能问题

c++ - gcc 列表节点交换实现过于复杂?

c++ - Boost undefined references交叉编译linux到windows

C++ - 进程以管理员身份运行时的 GetUserName()