0

I was completing an assignment for a lab in my C++ class, and I came across this problem where all of the protected variables from my base class haven't been declared for use in my derived class, and I'm not sure why. I've been trying to solve this for a while, and can't seem to figure it out.

Here is the code:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

template <class elemType>
class arrayListType {

    public:
        const arrayListType<elemType>& operator=(const arrayListType<elemType>&);

        bool isEmpty() const;
        bool isFull() const;
        int listSize() const;
        int maxListSize() const;
        void print() const;
        bool isItemAtEqual(int loc, int item) const;
        void removeAt(int loc);
        void retrieveAt(int loc, int& retItem);
        void clearlist();

        virtual int seqSearch(int searchItem) const = 0;
        virtual void insertAt(int loc, int insertItem) = 0;
        virtual void insertEnd(int insertItem) = 0;
        virtual void replaceAt(int loc, int repItem) = 0;
        virtual void remove(int removeItem) = 0;

        arrayListType(int size = 100);
        arrayListType(const arrayListType& otherList);

        virtual ~arrayListType();

    protected:
        int *list;
        elemType length;
        elemType maxSize; 


};

template <class elemType>
bool arrayListType<elemType>::isEmpty() const {
    return (length == 0);
}

template <class elemType>
bool arrayListType<elemType>::isFull() const {
    return (length == maxSize);
}

template <class elemType>
int arrayListType<elemType>::listSize() const {
    return length;
}

template <class elemType>
int arrayListType<elemType>::maxListSize() const {
    return maxSize;
}

template <class elemType>
void arrayListType<elemType>::print() const {
    for (int i = 0; i < length; i++) {
        cout << list[i] << " ";
        cout << endl;
    }
}

template <class elemType>
bool arrayListType<elemType>::isItemAtEqual(int loc, int item) const {
    if (loc < 0 || loc >= length) {
        cout << "The item's location is out of range" << endl;
        return false;
    } else {
        return (list[loc] == item);
    }
}

template <class elemType>
void arrayListType<elemType>::removeAt(int loc) {
    if (loc < 0 || loc >= length) {
        cout << "The item's location is out of range" << endl;
    } else {
        for (int i = loc; i < length - 1; i++) {
            list[i] = list[i + 1];
            length --;
        }
    }
}

template <class elemType>
void arrayListType<elemType>::retrieveAt(int loc, int& retItem) {
    if (loc < 0 || loc >= length) {
        cout <<"The item's location is out of range" << endl;
    } else {
        retItem = list[loc];
    }
}

template <class elemType>
void arrayListType<elemType>::clearlist() {
    length = 0;
}

template <class elemType>
arrayListType<elemType>::arrayListType(int size) {
    if (size <= 0) {
        cout << "You need a positive value for size, the default size is 100" << endl;
        maxSize = 100;
    } else {
        maxSize = size;
        length = 0;
        list = new int[maxSize];
    }
}

template <class elemType>
arrayListType<elemType>::~arrayListType() {
    delete [] list;
}

template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator= (const arrayListType<elemType>& otherList) {
    if (this != otherList) {
        delete [] list;
        maxSize = otherList.maxSize;
        length = otherList.maxSize;
        list = new elemType[maxSize];
        for (int i = 0; i < length; i++) {
            list[i] = otherList.list[i];
        }
    }
    return *this;
}

template <class elemType>
arrayListType<elemType>::arrayListType(const arrayListType& otherList) {
    maxSize = otherList.maxSize;
    length = otherList.length;
    int j;

    list = new int[maxSize];

    for (int i = 0; j < length; j++) {
        list[i] = otherList.list[j];
    }
}

template <class elemType>
class unorderedArrayListType :public arrayListType<elemType> {
    public:
        void insertAt(int loc, int insertItem);
        void insertEnd(int insertItem);
        void replaceAt(int loc, int repItem);
        int seqSearch(int searchItem) const;
        void remove(int removeItem);

        unorderedArrayListType(int size = 100);
};

template <class elemType>
void unorderedArrayListType<elemType>::insertAt(int loc, int insertItem) {
    if (loc < 0 || loc >= maxSize) {
        cout << "The item's position is out of range" << endl;
    } else if (length >= maxSize) {
        cout << "Can't insert items into a full list" << endl;
    } else {
        for (int i = length; i > loc; i--) {
            list[i] = list [i - 1];
            list[loc] = insertItem;
            length++;
        }
    }
}
    
template <class elemType>
void unorderedArrayListType<elemType>::insertEnd(int insertItem) {
    if (length >= maxSize) {
        cout << "Can't insert item into a full list" << endl;
    } else {
        list[length] = insertItem;
        length++;
    }
}

template <class elemType>
int unorderedArrayListType<elemType>::seqSearch(int searchItem) const {
    int col;
    bool found = false;

    for (col = 0; col < length; col++) {
        if (list[col] == searchItem) {
            found = true;
            break;
        }
    }
    if (found) {
        return col;
    } else {
        return -1;
    }
}

template <class elemType>
void unorderedArrayListType<elemType>::remove(int removeItem) {

    int col;

    if (length == 0) {
        cout << "You can't delete from an empty list" << endl;
    } else {
        col = seqSearch(removeItem);

        if (col != -1) {
            removeAt(col);
        } else {
            cout << "The item that needs to be deleted isn't in the list" << endl;
        }
    }
}

template <class elemType>
void unorderedArrayListType<elemType>::replaceAt(int loc, int repItem) {
    if (loc < 0 || loc >= length) {
        cout << "The item's location is out of range" << endl;
    } else {
        list[loc] = repItem;
    }
}

template <class elemType>
unorderedArrayListType<elemType>::unorderedArrayListType(int size): arrayListType(size) {
}

int main() {

    unorderedArrayListType<int> list(25);
    unorderedArrayListType<float> floatList(25);

    int num;
    int removeNum;
    float k;
    float repK;

    cout << "Enter 5 numbers: ";
    for (int i = 0; i < 5; i++) {
        cin >> num;
        list.insertEnd(num);
    }

    cout << endl;

    cout << "The current list is: ";
    list.print();

    cout << "Enter a number to remove: ";
    cin >> removeNum;
    cout << endl;
    list.remove(removeNum);

    cout << "The updated list is: ";
    list.print();

    cout << "Enter 5 numbers: ";
    for (int i = 0; i < 5; i++) {
        cin >> k;
        list.insertEnd(k);
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

0 Answers0