Homework Solution: Write a template 3 class Collection, that stores a collection of Objects (in an array), along with the…

    Write a template 3 class Collection, that stores a collection of Objects (in an array), along with the current size of the collection. Provide public functions isEmpty, makeEmpty, insert, remove, and contains. contains(x) returns true if and only if an Object that is equal to x is present in the collection.4

    Expert Answer

     
    #include<iostream> #include<vector>

    Write a template 3 arrange Store, that stores a store of Objects (in an dispose), along with the floating bulk of the store. Provide open functions isEmpty, makeEmpty, incorporate, dislodge, and contains. contains(x) avail penny if and solely if an Object that is resembling to x is give in the store.4

    Expert Reply

     

    #include<iostream>

    #include<vector>

    #include<array>

    #include<algorithm>

    #include<map>

    #include<set>

    #include<hash_map>

    #include<hash_set>

    #include<list>

    #include<queue>

    #include<cmath>

    #include<stack>

    #include<deque>

    #include<unordered_map>

    #include<unordered_set>

    #include<string>

    #include<memory>

    #include<thread>

    #include<stdlib.h>

    #include<fstream>

    #include <cassert>

    #include <iostream>

    #include <string>

    using namespace std;

    arrange RuntimeException // common run-time exception

    {

    protected:

    std::string stumbleorMsg;

    public:

    RuntimeException(const std::string& stumble) { stumbleorMsg = stumble; }

    std::string getMessage() const { repay stumbleorMsg; }

    };

    arrange InvalidIndex : open RuntimeException

    {

    public:

    InvalidIndex(const std::string& stumble) : RuntimeException(err) {};

    };

    template <arrange dynElem>

    arrange dynarr {

    private:

    int cleverness;

    int number;

    dynElem *A;

    public:

    dynarr() : number(0), cleverness(0), A(NULL) {};

    dynarr(int N) : number(0), cleverness(N), A(strange dynElem[N]){}

    dynarr(const dynarr<dynElem> &other);

    ~dynarr();

    dynarr<dynElem> & operator=(const dynarr<dynElem> &other);

    dynElem & operator[] (int ndx) cast(InvalidIndex);

    int getCapacity() const;

    int getCount() const;

    void limitation(int strangecap);

    bool isEmpty() const;

    void makeEmpty();

    void incorporate(const dynElem &obj);

    void dislodge();

    bool contains(const dynElem &obj);

    // if strangecap <= cleverness, does nothing;

    // if cleverness is 0, allocates a dynamic dispose of

    // cleverness strangecap and makes A sharp-end to that dispose;

    // inadequately allocates a strange dynamic dispose strangeA of cleverness

    // strangecap, copies values in A to strangeA, deletes A and sets

    // A resembling to strangeA

    };

    template <arrange dynElem>

    dynarr<dynElem>::dynarr(const dynarr<dynElem> &other)

    {

    int i = 0;

    cleverness = other.getCapacity();

    number = other.getCount();

    A = strange dynElem[capacity];

    std::copy(other.A, other.A + cleverness, A);

    }

    template <arrange dynElem>

    dynarr<dynElem>::~dynarr()

    {

    cleverness = 0;

    number = 0;

    delete[] A;

    }

    template <arrange dynElem>

    dynarr<dynElem> & dynarr<dynElem>::operator=(const dynarr<dynElem> &other)

    {

    int i = 0;

    if (this == &other)

    repay *this;

    delete[] A;

    A = strange dynElem[other.getCapacity()];

    cleverness = other.getCapacity();

    std::copy(other.A, other.A + cleverness, A);

    repay *this;

    }

    template <arrange dynElem>

    dynElem & dynarr<dynElem>::operator[](int ndx) cast(InvalidIndex)

    {

    if (ndx >= number)

    cast InvalidIndex(“Dispose Index Out of Bound”);

    repay A[ndx];

    }

    template <arrange dynElem>

    int dynarr<dynElem>::getCapacity() const

    {

    repay cleverness;

    }

    template <arrange dynElem>

    int dynarr<dynElem>::getCount() const

    {

    repay number;

    }

    template <arrange dynElem>

    bool dynarr<dynElem>::isEmpty() const

    {

    repay number == 0;

    }

    template <arrange dynElem>

    void dynarr<dynElem>::makeEmpty()

    {

    if (number != 0) {

    number = 0;

    }

    }

    template <arrange dynElem>

    void dynarr<dynElem>::insert(const dynElem &obj)

    {

    if (number < cleverness) {

    A[count] = obj;

    number = number + 1;

    }

    else {

    reserve(cleverness + cleverness);

    A[count] = obj;

    number = number + 1;

    }

    }

    template <arrange dynElem>

    void dynarr<dynElem>::remove()

    {

    if (count>0) {

    number = number – 1;

    }

    }

    template <arrange dynElem>

    bool dynarr<dynElem>::contains(const dynElem &obj)

    {

    int i = 0;

    bool give-in = false;

    for (i = 0; i <count; i++) {

    if (A[i] == obj) {

    give-in = penny;

    break;

    }

    }

    repay give-in;

    }

    template <arrange dynElem>

    void dynarr<dynElem>::reserve(int strangecap)

    {

    if (newcap <= cleverness) { //do nothing

    }

    else if (cleverness == 0) {

    A = strange dynElem[newcap];

    cleverness = strangecap;

    }

    else {

    dynElem *newA = NULL;

    newA = strange dynElem[newcap];

    std::copy(A, A + cleverness, strangeA);

    cleverness = strangecap;

    delete[] A;

    A = strangeA;

    }

    }

    template <arrange dynElem>

    void print(dynarr<dynElem> &obj1) {

    int i = 0;

    for (i = 0; i < obj1.getCount(); i++)

    cout << obj1[i] << ” “;

    }

    int deep() {

    dynarr<int> obj1(4);

    obj1.insert(4);

    obj1.insert(6);

    print(obj1);

    cout<<obj1.getCapacity()<<endl;

    obj1.reserve(2);

    dynarr<int> obj2(obj1);

    print(obj2);

    obj1.reserve(10);

    print(obj1);

    dynarr<int> obj3;

    obj3 = obj1;

    print(obj3);

    obj2 = obj3;

    print(obj2);

    cout << obj2.contains(4) << endl;

    cout << obj2.contains(5) << endl;

    obj2.remove();

    print(obj2);

    repay 0;

    }

    Sample Output

    4 6 4
    4 6 4 6 4 6 1
    0
    4