[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Template as a Class

Ok, I'm a student with an assign problem, but the book for the class does not explain how to use Templates and classes together.       But a problem in the book has me writing a template class DataStore    Here are the following member functions..........
Ok, teacher (very unhelpful lady), says program should be around 1 page.....pending whether you code it 3 files or 2.. either a main with header, or a main with header and it's own CPP file
Member functions are:

bool insert(const T& elt); // Insert elt into the private array dataElements having 5 elements of type T.  The index of the next available location in dataElements is given by the data member "loc", which is also the number of data values in dataElements.  Return false if there is no more room left in dataElements; otherwise, return true.

int find(const &T elt); // Search for element elt in dataElements and return its index if it is found and -1 if it is not in dataElements.

int numElts(); // Return the number of elements stored in dataElements.

T getData(int n); // Return the element at location "n" in dataElements.  Generate an error message and exit if n < 0 or n >= loc

Please help if you can......     I am really stumped and in a bad position.      At the least, can you direct me to a good tutorial site....    PLEASE HELP ASAP!!!!!!!
.....and the problem states:
0
sly_original
Asked:
sly_original
1 Solution
 
alexoCommented:
sly_original, it would be unethical for us to do your homework for you but we can help with the concepts and answer specific questions.

Now, about templates:  Templates are not very difficult actually.  A template says to the compiler "look, here's how to create a class or a function based on the following parameters...".

Say you want to create an array class holding values of some (yet undefined) type.  Let's call the type "T".  You write:


    template<class T>
    class Array
    {
    public:
        Array(); // constructor
        ~Array(); // destructor
        bool insert(const T& elt);

    private:
        T* pItems; // a pointer to the items of type "T"
    };

That means: This is not really a class but a template that explains how the class should look like.  It is used to create a whole family of classes that look similar.  In order to create a class, the type "T" has to be specified.

Now, say you want to provide an implementation of a member function.  You write:

    template<class T>
    bool Array<T>::insert(const T& elt)
    {
        // Do something...
    }

This is still a template.  The unspecified type(s) is/are: "T".  The member function belongs to the class family "Array" parametrized by "T" (for every T).

Say you want to create an implementation for the type "char" that will be different from the default implementations for all other types.  You write:

    bool Array<char>::insert(const T& elt)
    {
        // Do something completly different...
    }

Now, say you want to use the template to create an array of "float" values.  You write:

    int main()
    {
        float item = 1.0F;

        Array<float> arr;
        arr.insert(item);

        return 0;
    }

That's it for now.  Ask if you have more questions.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now