An easy instance of using the template class in C++
2007-02-01 21:49
796 查看
//this is the source code
#include<iostream>
using namespace std;
template<class datatype>class basis{ //set the template class with parameters
public:
void swap(datatype*,datatype*);
void printv(datatype,datatype);
}; //Never lose the stub after a class's definition
/*******************************
* What's more, in the Object *
* orientied c++ programming, *
* a template can have its 8 *
* argument and parameters *
* its arguments can be also a *
* class or so. Thus we can *
* write the parameter in a <>.*
* In the following steps, if *
* we write its member *
* functions we have to write *
* like this. *
* *****************************/
template<class datatype> //if we want to call the function in a template class we should add in this header line!!!
void basis<datatype>::swap(datatype *firstnum,datatype *secondnum)
{
datatype temp;
temp=*firstnum;
*firstnum=*secondnum;
*secondnum=temp;
}
/**********************************************************
* If we wanna swap the values of two variables, we have *
* numerious of methods. But the basic method is to swap *
* the values by referrence. *
**********************************************************/
template<class datatype>
void basis<datatype>::printv(datatype first,datatype second)
{
cout<<"The value is "<<first<<"and "<<second<<"now."<<endl;
}
//All the code above can be written in a head file
int main()
{
basis<int>intpair; //call the template class and give the variable
/***********************************
* The data type is also a kind of *
* class, see how we call the *
* template class and *
* its parameters! *
***********************************/
int inta=1,intb=2;
intpair.printv(inta,intb);
intpair.swap(&inta,&intb);
intpair.printv(inta,intb);
basis<float>fpair;
float fa=0.1,fb=0.2;
fpair.printv(fa,fb);
fpair.swap(&fa,&fb);
fpair.printv(fa,fb);
return 0;
}
#include<iostream>
using namespace std;
template<class datatype>class basis{ //set the template class with parameters
public:
void swap(datatype*,datatype*);
void printv(datatype,datatype);
}; //Never lose the stub after a class's definition
/*******************************
* What's more, in the Object *
* orientied c++ programming, *
* a template can have its 8 *
* argument and parameters *
* its arguments can be also a *
* class or so. Thus we can *
* write the parameter in a <>.*
* In the following steps, if *
* we write its member *
* functions we have to write *
* like this. *
* *****************************/
template<class datatype> //if we want to call the function in a template class we should add in this header line!!!
void basis<datatype>::swap(datatype *firstnum,datatype *secondnum)
{
datatype temp;
temp=*firstnum;
*firstnum=*secondnum;
*secondnum=temp;
}
/**********************************************************
* If we wanna swap the values of two variables, we have *
* numerious of methods. But the basic method is to swap *
* the values by referrence. *
**********************************************************/
template<class datatype>
void basis<datatype>::printv(datatype first,datatype second)
{
cout<<"The value is "<<first<<"and "<<second<<"now."<<endl;
}
//All the code above can be written in a head file
int main()
{
basis<int>intpair; //call the template class and give the variable
/***********************************
* The data type is also a kind of *
* class, see how we call the *
* template class and *
* its parameters! *
***********************************/
int inta=1,intb=2;
intpair.printv(inta,intb);
intpair.swap(&inta,&intb);
intpair.printv(inta,intb);
basis<float>fpair;
float fa=0.1,fb=0.2;
fpair.printv(fa,fb);
fpair.swap(&fa,&fb);
fpair.printv(fa,fb);
return 0;
}
相关文章推荐
- Why is the size of an empty class not zero in C++?
- Why is the size of an empty class not zero in C++?
- What’s the behavior when calling an overwritten virtual method in the constructor method of a base class?
- How to code the forward declaration of class or struct using template?
- Question 27: Which of the following reasons describe why a destructor cannot throw an exception in C++?
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- Using Class of Secure Transport (COST) to Restrict Instance Registration in Oracle RAC (Doc ID 13408
- How Would You Get the Count of an Array in C++?
- Write a method to print the last K lines of an input file using C++
- How Would You Get the Count of an Array in C++?
- Factory methods are static methods that return an instance of the native class(保护CONSTRUCTOR)
- An implementation of the skip list data structure written in C++
- When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.
- An Example about the use of map Container in C++ STL / c++ map容器的用法
- A class extends ArrayList, but the instance of the calss do not work in a complex case.
- Hack the Stack: Using Snort and Ethereal to Master the 8 Layers of an Insecure Network [ILLUSTRATED]
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- The study of Class and Instance in Python(20170918)
- [C/C++] An instance of using EPoll
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)