template class

Hello,

how to make a template class with an array of X items

So far I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T>
class Quad {
    
    private:
    int a[4];
    int n, i, small, list;
    
    public:
    Quad();
    void print() {
        cout << "The smallest of the items is: " << small << endl;
    }
    T getSmallest (T a[4]) {
        for(i=1; i <n; i++) {
            if(small < list[i])
            small = list[i];
        }
    }
};



Also, how would I create a Quad object to store 4 strings: "Hello" "World" "How are" "You?"?

Last edited on
Replace int with T and write the constructor accordingly.
On my array? And what would I be adding to the constructor, exactly?
The only data member your class needs is

 
T elements[ 4 ];


You do not need n, i, small, or list.

I do not know what a "general constructor" is. Bazzy is suggesting (I think) that you write (at a minimum) the following constructor:

 
Quad( T (const &e)[ 4 ] );


getSmallest() should not take any parameters; it should reference the "elements" data member. getSmallest() is
also most simply implemented with std::min_element() [see <algorithm>].

print() should print the four elements, not the smallest element. This can be accomplished with a simple for() loop.
Thanks for the help!
Topic archived. No new replies allowed.