Help with template

B

B. Williams

I have been given an assignment to create a class template. I have begun to
create the template and I know what I want public and private, but I am lost
with how to format my constructor so that it will take any value (ie int vs
bool vs string). Will someone offer me some assistance?

#ifndef TryOME

#define TryOME

template <class R, class S, class V>

class TryOMe{

public:

TryOMe(int, int, int, int); <---- What do I put in here?

private:

S var1;

V var2;

V var4;

R var3;

};

#endif
 
V

vheinitz

Hi,
like this:
template <class R, class S, class V>
class TryOMe{
public:
TryOMe(S s, V v1, V v2, R r) :var1(s), var2(v1), var3(v2),var4(r){}
private:
S var1;
V var2;
V var4;
R var3;
};

The rule is easy:
write a class

class Print
{
int _i;
public: Print(int i):_i(i){cout << _i;}
} ;

decide, would be a class more usefull, if
a variable would be of any different type, (here ie int _i)
Replace type-name of this variable by T(or any other name),

class Print
{
T _i;
public: Print(T i):_i(i){cout << _i;}
} ;

write template<class T> before the class definition.

template<class T>
class Print
{
T _i;
public: Print(T i):_i(i){cout << _i;}
} ;

Of couse templates can much more. I would sugest the book :
David Vandevoorde, Nicolai M. Josuttis, "C++ Templates: The Complete
Guide"

Best regards,
Valentin Heinitz
http://heinitz-it.de
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with Template 6
Need Help with Repository Program (Beginner) 1
template with STL container 10
Issue with textbox script? 0
Help with code 0
Template Class 4
Need help with this script 4
Please, help me. 1

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top