Learning notes-template functions and templates

Source: Internet
Author: User
# Include <iostream>
Using namespace STD;
Template <typename type>
Type max (Type X, Type Y)
{
If (x> Y)
{
Return X;
}
Else
{
Return y;
}
}

Template <typename parameter1 = int, typename parameter2 = int>
Class
{
Parameter1 X;
Parameter2 y;
Public:
Void setdata (parameter1 X, parameter2 y)
{
This-> X = X;
This-> Y = y;
}
Void display () const
{
Cout <"x =" <x <Endl;
Cout <"Y =" <Y <Endl;
}
};
Int main ()
{
A <int, float> A;/* can be like this a <> A; this indicates that the default parameter type parameter1 = int, parameter2 = int can be called, but not a; in this way, an error is reported during compilation,
The order must be consistent with that defined by the template. Otherwise, an error occurs */
A. setdata (11, 12.363 );
A. Display ();
Int max = max <int> (); // You can also write int max = max (). In this way, typename is defined according to the parameter type in the brackets, however, the two parameter types must be consistent.
Int N;
Cin> N;
Return 0;
}

Author: Lin yufei
Source: http://www.cnblogs.com/zhengyuhong/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. However, you must keep the author's information without the author's consent and provide the original article connection clearly on the article page.

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.