Solved

Template as a Class

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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++.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now