Template> function Template

Source: Internet
Author: User

// The C ++ template is parameterized.ProgramDesign method, which is a completely common method to design functions or classes
// You do not need to specify the types of each object to be used in advance. The types of these template calculation objects are not actual.
// Data type, but a parameterized type (also known as a type parameter ).
// A class with a type parameter is called a class template.
// A function with a type parameter is called a function template.
// The type parameter of the template is determined by the type of the actual parameter called by the compiler.
// Truly executableCodeThis process is called Instantiation.
// A function template is called a template function after the type is instantiated.
// A class template is called a template class after the type is instantiated.

# Include <iostream>
Using namespace STD;

// The overload function usually performs similar operations based on different data types.
// They are all the same, so it is more concise and convenient to use the function template to complete this task.
Template <typename T>
Void Exchange (T & x, t & Y)
{

TTemp;
Temp = X;
X = y;
Y = temp;

}
Void main ()
{

IntI = 7, j = 8;
Exchange (I, j );
Cout <"I =" <I <"\ TJ =" <j <Endl;
Double A = 3.14, B = 6.2;
Exchange (A, B );
Cout <"A =" <A <"\ TB =" <B <Endl;
Char X = 'x', y = 'y ';
Exchange (x, y );
Cout <"x =" <x <"\ ty =" <Y <Endl;

}

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.