Solved

Template as a Class

Posted on 1998-10-04
1
221 Views
Last Modified: 2010-04-01
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
Comment
Question by:sly_original
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1174344
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question