Solved

Template as a Class

Posted on 1998-10-04
1
219 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

809 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