C ++ template class Parsing

Source: Internet
Author: User
Tags integer numbers

The specific role of the template class will not be detailed here. The following mainly describes the usage of the template class and some notes.

# Include <iostream> using namespace std; template <typename numtype> // defines the class template class Compare {public: Compare (numtype a, numtype B) {x =; 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); // defines the object cmp1, used to compare two integers: cout <cmp1.max () <"is the Maximum of two integer numbers. & quot; <endl; cout <cmp1.min () <& quot; is the Minimum of two integer numbers. "<endl; Compare <float> cmp2 (45.78, 93.6); // defines the object cmp2 for comparing two floating point numbers in cout <cmp2.max () <"is the Maximum of two float numbers. & quot; <endl; cout <cmp2.min () <& quot; is the Minimum of two float numbers. & quot; <endl; Compare <char> cmp3 ('A', 'A'); // defines the object cmp3 for comparing two characters in cout. & quot; <cmp3.max () <"is the Maximum of two characters. & quot; <endl; cout <cmp3.min () <& quot; is the Minimum of two characters. "<endl; return 0 ;}

Usage:

1) write out an actual class first. Because its semantics is clear and its meaning is clear, there is generally no error.

2) Replace the type name to be changed in this class (for example, int to float or char) with a self-specified virtual type name (numtype in the example above ).

3) add a line before the class declaration in the format
Template <typename virtual type parameter>, as shown in figure
Template <typename numtype> // note that there is no semicolon at the end of the row
Class Compare
{...}; // Class body

4) when defining an object using a class template, use the following form:
Class Template Name <actual type Name> Object Name;
Class Template Name <actual type Name> Object Name (real parameter table );
For example
Compare <int> cmp;
Compare <int> cmp (3, 7 );

Note: Compare is the class Template Name, not a specific class. The type numtype in the class template body is not an actual type, but a virtual type and cannot be used to define objects.
The actual type name must be used to replace the virtual type. The specific method is as follows:
Compare <int> cmp (4, 7 );
That is, after the class Template Name, specify the actual type name in the angle brackets. during compilation, the compilation system replaces the type parameter numtype in the class template with int, in this way, the class template is embodied or instantiated.

5) If a member function is defined outside the class template, it should be written as a class template:
Template <class virtual type parameter>
Function Type Template Name <virtual type parameter>: member function name (function form parameter table ){...}

Eg:

Template <nametype numtype>
Numtype Compare <numtype>: max ()
{Return (x> y )? X: y ;}

Additional instructions:

1) The type parameters of the class template can be one or more. class must be added before each type, as shown in figure
Template <class T1, class T2>
Class someclass
{...};
Enter the actual type names when defining objects, as shown in figure
Someclass <int, double> obj;

2) like using a class, pay attention to its scope when using a class template. It can only be used within its effective scope to define objects.

 

Reference: http://see.xidian.edu.cn/cpp/biancheng/view/213.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.