**template class**
Author |
Message |
JessT0
|
Posted: Mon Mar 15, 2004 9:21 pm Post subject: **template class** |
|
|
Anyone know how to convert this to template ?please help
given:
code: |
class pair{
{
public:
pair(char x):c1(x),c2(x+1){}
void increment(){c1++,c2++;}
void print() const {cout << c1<< "\t" << c2;}
private:
char c1,c2;
}; |
here what i did
code: |
template<class TYPE>
class pair{
public:
pair(TYPE x):c1(x),c2(x+1){}
void increment(){c1++,c2++;}
void print() const {cout << c1<< "\t" << c2;}
private:
TYPE c1,c2;
};
int main()
{
return 0;
} |
note: the program has to have 3 instantiation of this class with type char,int,double.Create a pair for each of these types, print them,increment them,and then reprint them.Thank for reading |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Mar 15, 2004 10:23 pm Post subject: (No subject) |
|
|
code: | #include <iostream>
template <typename _t>
class pair
{
private:
_t first, second;
public:
pair(_t, _t);
friend std::ostream& operator<<(std::ostream&, const pair<_t>&);
void operator++();
void operator++(int);
};
int main()
{
pair<char> p1('A', 'B');
pair<int> p2(1, 9);
pair<double> p3(1.2, 2.5);
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
std::cout << p3 << std::endl;
p1++;
p2++;
p3++;
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
std::cout << p3 << std::endl;
return 0;
}
template <typename _t>
pair<_t>::pair(_t init_first, _t init_second)
: first(init_first), second(init_second)
{ }
template <typename <_t>
std::ostream& operator<<(std::ostream& out, const pair<_t>& p)
{
return out << p.first << "\t" << p.second;
}
template <typename <_t>
void pair<_t>::operator++()
{
first++;
second++;
}
template <typename <_t>
void pair<_t>::operator++(int)
{
first++;
second++;
} |
|
|
|
|
|
|
JessT0
|
Posted: Wed Mar 17, 2004 11:17 am Post subject: (No subject) |
|
|
Thank wtd
did you compile this program?i compiled it and it doesn't work... ... |
|
|
|
|
|
wtd
|
Posted: Wed Mar 17, 2004 7:12 pm Post subject: (No subject) |
|
|
The following fixes the typos and other errors.
code: | #include <iostream>
template <typename _t>
class pair
{
private:
_t first, second;
public:
pair(_t, _t);
void operator++();
void operator++(int);
template <typename __t>
friend std::ostream& operator<<(std::ostream&, const pair<__t>&);
};
int main()
{
pair<char> p1('A', 'B');
pair<int> p2(1, 9);
pair<double> p3(1.2, 2.5);
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
std::cout << p3 << std::endl;
p1++;
p2++;
p3++;
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
std::cout << p3 << std::endl;
return 0;
}
template <typename _t>
pair<_t>::pair(_t init_first, _t init_second)
: first(init_first), second(init_second)
{ }
template <typename _t>
std::ostream& operator<<(std::ostream& out, const pair<_t>& p)
{
return out << p.first << "\t" << p.second;
}
template <typename _t>
void pair<_t>::operator++()
{
first++;
second++;
}
template <typename _t>
void pair<_t>::operator++(int)
{
first++;
second++;
}
|
|
|
|
|
|
|
JessT0
|
Posted: Thu Mar 18, 2004 11:48 am Post subject: (No subject) |
|
|
thank to Wtd alot. i got what you talking about.One more question:For the print and increment, I believe my instructor wants you to use operator overloading instead of creating a normal function. Do you have any idea for that?i'm really appreceate. |
|
|
|
|
|
wtd
|
Posted: Thu Mar 18, 2004 3:19 pm Post subject: (No subject) |
|
|
JessT0 wrote: thank to Wtd alot. i got what you talking about.One more question:For the print and increment, I believe my instructor wants you to use operator overloading instead of creating a normal function. Do you have any idea for that?i'm really appreceate.
That's what the following functions are doing:
code: | template <typename _t>
std::ostream& operator<<(std::ostream& out, const pair<_t>& p)
{
return out << p.first << "\t" << p.second;
}
// Prefix ++ operator:
// ++p;
template <typename _t>
void pair<_t>::operator++()
{
first++;
second++;
}
// Postfix ++ operator:
// p++;
template <typename _t>
void pair<_t>::operator++(int)
{
first++;
second++;
} |
|
|
|
|
|
|
JessT0
|
Posted: Sat Mar 20, 2004 9:23 am Post subject: (No subject) |
|
|
One last question.Can we use print(),increment() functions which already have in there instead of using operator++(),operator++(int) and "cout" ?. Last time i just wanna say thank you very much for your help,this really helpfull.Have a good weekend |
|
|
|
|
|
wtd
|
Posted: Sat Mar 20, 2004 3:28 pm Post subject: (No subject) |
|
|
You can use them, but with the overloaded operators, you don't really have any reason to.
Which would you rather see?
code: | pair<int> p(3, 4);
p.increment();
p.print(); |
Or:
code: | pair<int> p(3, 4);
p++;
std::cout << p << std::endl; |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
JessT0
|
Posted: Sun Mar 21, 2004 11:46 pm Post subject: (No subject) |
|
|
thanks ,i get it now |
|
|
|
|
|
|
|