Home>

Sometimes, there are two or more classes,Its function is the same,Just the data types are different,As the following statement declares a class:

class compare_int
{
public:
  compare (int a, int b)
  {
   x=a;
   y=b;
  }
  int max ()
  {
   return (x>y)?x:y;
  }
  int min ()
  {
   return (x<y)?x:y;
  }
private:
  int x, y;
};

Its role is to compare two integers,You can get the larger and smaller of the two integers by calling the member functions max and min.

If i want to compare two floating point numbers (float type), you need to declare another class:

class compare_float
{
public:
  compare (float a, float b)
  {
   x=a;y=b;
  }
  float max ()
  {
   return (x>y)?x:y;
  }
  float min ()
  {
   return (x<y)?x:y;
  }
private:
  float x, y;
}

Obviously this is basically repetitive work,There should be ways to reduce duplication of effort.

C++ added the function of template in the later stage of development, providing a way to solve this kind of problem.Can declare a generic class template,It can have one or more virtual type parameters,For example, you can write the following class template for the above two classes:

template<class numtype>//declare a template,Virtual type named numtype
class compare //The class template is named compare
{
public:
  compare (numtype a, numtype b)
  {
   x=a;y=b;
  }
  numtype max ()
  {
   return (x>y)?x:y;
  }
  numtype min ()
  {
   return (x<y)?x:y;
  }
private:
  numtype x, y;
};

Compare this template with the first compare_int class,You can see two differences.

1) Add a line when declaring a class template

 template<class type parameter name>

Template means "template", which is a keyword that must be written when declaring a class template.The content in the angle brackets after the template is the parameter list of the template.The keyword class indicates that it is followed by a type parameter.In this example, numtype is a type parameter name.This name is free to take,As long as it is a legal identifier.Taking numtype here just means "data type".At this point, mimtype is not an existing actual type name,It's just a dummy type parameter name.It will be replaced by an actual type name in the future.

2) Replace the original type name int with the virtual type parameter name numtype.

When creating class objects,If the actual type is specified as int, the compilation system replaces all numtypes with int, and if it is specified as float, replaces all numtypes with float. In this way, "one kind of multi-use" can be realized.

Since the class template contains type parameters,It is also called a parameterized class.If a class is an abstraction of an object,An object is an instance of a class,The class template is an abstraction of the class,A class is an instance of a class template.Use class templates to create classes with various data types.

Then, after declaring a class template,How to use it?How to make it into an actual class?

Let's first review the methods used to define objects using classes:

 compare_int cmp1 (4,7);//compare_int is a declared class

Its role is to create an object of compare_int class,The actual parameters 4 and 7 are assigned to the formal parameters a and b, respectively, as two integers for comparison.

The method of defining objects with class templates is similar,But cannot be written directly

 compare cmp (4,7);//compare is the class template name

compare is the class template name,Instead of a concrete class,The type numtype in the class template body is not an actual type,Just a virtual type,It cannot be used to define objects.You must replace the virtual type with the actual type name,The specific approach is:

 compare<int>cmp (4,7);

That is, specify the actual type name in angle brackets after the class template name,When compiling,The compilation system replaces the type parameter numtype in the class template with int, so that the class template is concrete,In other words, instantiated.At this time compareis equivalent to the compare_int class introduced earlier.

[Example] Declare a class template,Use it to compare two integers, floats, and characters, respectively.Find the large and small numbers.

#include<iostream>
using namespace std;
template<class numtype>
//define the class template
class compare
{
  public:
  compare (numtype a, numtype b)
  {x=a;y=b;}
  numtype max ()
  {return (x>y)?x:y;}
  numtype min ()
  {return (x<y)?x:y;}
  private:
  numtype x, y;
};
int main ()
{
  compare<int>cmp1 (3,7);//Define object cmp1 for comparison of two integers
  cout<cmp1.max ()<<<"is the maximum of two integer numbers."<<endl;
  cout<cmp1.min ()<<"is the minimum of two integer numbers."<endl<endl;
  compare<float>cmp2 (45.78,93.6);//Define the object cmp2 for comparison of two floating point numbers
  cout<cmp2.max ()<<<<the maximum of two float numbers.><<endl;
  cout<cmp2.min ()<<"is the minimum of two float numbers."<endl<endl;
  compare<char>cmp3 (′ a ′, ′ a ′);//Define object cmp3 for comparison of two characters
  cout<cmp3.max ()<<<<the maximum of two characters.>><endl;
  cout<cmp3.min ()<<<"is the minimum of two characters."<<endl;
  return 0;
}

The results are as follows:

7 is the maximum of two integers.
3 is the minimum of two integers.
93.6 is the maximum of two float numbers.
45.78 is the minimum of two float numbers.
a is the maximum of two characters.
a is the minimum of two characters.

There is another problem to explain:The member functions in the class templates listed above are defined within the class templates.If instead defined outside the class template,Cannot be used in the form of a general definition class member function:

numtype compare ::max () {…} //Cannot define member functions in a class template like this

Instead, it should be written in the form of a class template:

 template<class numtype>
  numtype compare<numtype>::max ()
  {
    return (x>y)?x:y;
  }

The first line above is the class template,The numtype at the left end of the second line is the virtual type name,The following compare<numtype>is a whole,Is a class with parameters.Indicates that the defined max function is in the scope of the class compare<numtype> ;.When defining objects,Of course, the user must specify the actual type (such as int). When compiling, the virtual type name numtype in the class template is replaced with the actual type.This way compareis equivalent to an actual class.You can rewrite the example to define member functions outside the class template.

Summarizing the above introduction,You can declare and use class templates like this:

1) Write an actual class first.Because of its clear semantics,The meaning is clear,It usually doesn't go wrong.

2) Change the type name in this class (such as int to float or char) to a virtual type name (such as numtype).

3) Add a line before the class declaration,The format is:

template<class virtual type parameter>

Such as:

 template<class numtype>//Note that there is no semicolon at the end of this line
  class compare
  {…};//Class body

4) When defining an object with a class template, use the following form:

 Class template name<actual type name>object name;
  Class template name<actual type name>object name (argument list);

Such as:

 compare<int>cmp;
  compare<int>cmp (3,7);

5) If you define a member function outside the class template,Should be written as a class template:

 template<class virtual type parameter>
 Function type Class template name<virtual type parameter>::member function name (function parameter list) {…}

A few notes about class templates:

1) The type parameter of a class template can have one or more,Each type must be preceded by a class, such as:

 template<class t1, class t2>
  class someclass
  {…};

Substitute the actual type name when defining the object,Such as:

 someclass<int, double>obj;

2) As with using classes,Pay attention to the scope when using class templates.You can use it to define objects only within its effective scope.

3) Templates can have levels,A class template can be used as a base class,Derive a derived template class.There is less practical application of this knowledge,This tutorial will not be introduced for the time being.Interested students can learn by themselves.

c
  • Previous Explain in detail the sandbox mechanism and files during the iOS development process
  • Next mysql ERROR 1044 (42000): Access denied for user `` @ 'localhost' to database