[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Template as a Class

Posted on 1998-10-04
1
Medium Priority
?
224 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

649 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