C++ template funcitons and classes
2009-02-20 00:02
295 查看
#include
//----------------------
//template function
//----------------------
template //this line is required
T power(T base,int e) //you can view T as int or double or sth here.T is an return type
{
T result = base;
if(e == 0) return(T)1;
if(e < 0) return(T)0;
while(--e) result*=base;
return result;
}
//-----------------------
//template class
//-----------------------
template //this line is required
class CThree
{
public:
CThree(T t1,T t2,T t3);
T Min(); //you can view T as int or double or sth here.T is an return type
T Max(); //you can view T as int or double or sth here.T is an return type
private:
T a,b,c; //you can view T as int or double or sth here.T is an return type
};
template //this line is required
T CThree::Min() //T is required here
{
T minab = a < b ?a:b;
return minab //this line is required
T CThree::Max() //T is required here
{
T maxab=a //this line is required
CThree::CThree(T t1,T t2,T t3):
a(t1),b(t2),c(t3)
{
return;
}
int main()
{
int i=power(5,4);
cout< obj1(2,5,4);
cout< obj2((float)8.52,(float)-6.75,(float)4.54);
cout< obj3(646600L,437847L,364873L);
cout<
//----------------------
//template function
//----------------------
template //this line is required
T power(T base,int e) //you can view T as int or double or sth here.T is an return type
{
T result = base;
if(e == 0) return(T)1;
if(e < 0) return(T)0;
while(--e) result*=base;
return result;
}
//-----------------------
//template class
//-----------------------
template //this line is required
class CThree
{
public:
CThree(T t1,T t2,T t3);
T Min(); //you can view T as int or double or sth here.T is an return type
T Max(); //you can view T as int or double or sth here.T is an return type
private:
T a,b,c; //you can view T as int or double or sth here.T is an return type
};
template //this line is required
T CThree::Min() //T is required here
{
T minab = a < b ?a:b;
return minab //this line is required
T CThree::Max() //T is required here
{
T maxab=a //this line is required
CThree::CThree(T t1,T t2,T t3):
a(t1),b(t2),c(t3)
{
return;
}
int main()
{
int i=power(5,4);
cout< obj1(2,5,4);
cout< obj2((float)8.52,(float)-6.75,(float)4.54);
cout< obj3(646600L,437847L,364873L);
cout<
相关文章推荐
- Reversing Microsoft Visual C++ Part II: Classes, Methods and RTTI
- Reusing Code in C++: inheriting and template
- C++ template tricks and techniques
- C and C++ signal handling and C++ signal classes and examples
- evil C++ -- template typename vc and GNU compiler
- C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
- Pure Virtual Functions and Abstract Classes in C++
- C++ Template Classes
- How to call c++ exported method and classes in c#
- How to separate the implementation and definition for template function in c++
- Reversing Microsoft Visual C++ Part II: Classes, Methods and RTTI
- 重复老话题--C++ template tricks and techniques
- Real-Time C++: Efficient Object-Oriented and Template Microcontroller Programming.pdf
- 【转】How to call c++ exported method and classes in c#
- C++ classes and uniform initialization
- eversing Microsoft Visual C++ Part II: Classes, Methods and RTTI
- Metaclasses and Reflection in C++
- [C++] OOP - Virtual Functions and Abstract Base Classes
- C++ Template and Inheritance
- Reversing Microsoft Visual C++ Part II: Classes, Methods and RTTI