C + + class templates

Source: Internet
Author: User

Like a function template, a class template is used to enable a user to define a pattern for a class so that some data members in the class, parameters of some member functions, and return values of some member functions can take any type. A class template is an abstraction of a class of classes that differ only in the number of member data types, and programmers simply create a class template for the entire class family of this batch of classes, giving a set of program code that can be used to generate a variety of concrete classes (which can be seen as instances of class templates), thus greatly improving the efficiency of programming.

The general form of defining a class template is:
Template < type name Parameter name 1, type name parameter name 2,...>
Class name
{
class declaration Body
};
For example,template <class t>
Class Smemory
{...
Public
void Mput (T x);
...
}

Represents the definition of a class template named Smemory with the type parameter T.

The general form of class member functions that are defined outside the class template is:
 Template < type name Parameter name 1, type name parameter name 2,...>
function return value type class name < parameter name 1 parameter name 2,...>:: member function name (formal parameter list)
{
function body
}
Example:template <class t>
void Smemory<t>::mput (T x)
{...}

Represents a member function that defines a class template smemory, the function name is Mput, the type of parameter x is T, and the function has no return value.
A class template is an abstraction of a class family, which is simply a description of the class, and the compiler does not create program code for the class template (including member function definitions), but by instantiating the class template you can generate a concrete class and an object of that specific class.

Unlike a function template, the instantiation of a function template is done automatically by the compiler when the function call is processed, and the instantiation of the class template must be explicitly specified by the programmer in the program.
In fact, the general form of the example is:
Class name < data type 1 (or data), data type 2 (or data) ...> object name
For example,smemory<int> mol;
Represents the type parameter T of the class template smemory all replaced by the int type, thus creating a concrete class and generating an object mol for that concrete class.

The following is an example of how class templates are defined and used.
  example 9.3.1: definition and use of class templates.
#include <iostream.h>
#include <conio.h>
const int size=8;
  Template <class t>
Class Smemory {//define the template Smemory
T Data[size]; Type T, array of size data[] as data member
int count;
Public
Smemory () {count=0;}
void Mput (T x); The type of the parameter x of the Mput () function is T
T mget (); Declares a member function that returns a value of type T mget ()
};
  Template <class t>
void Smemory<t>::mput (T x)//define member function Mput (), the function's parameter type is T, which is used to assign values to the individual elements of the data array
{
if (count==8) {cout<< "Memory is full"; return;}
Data[count]=x;
count++;
}

  Template <class t>
T Smemory<t>::mget ()//define member function Mget (), the return type of the function is T, which is used to take out individual elements of the data array
{
if (count==0) {cout<< "Memory is empty"; return 0;}
count--;
return Data[count];

void Main ()
{
Smemory<int> Mo1;
int i; Char ch= ' A ';//Instantiate smemory and create object Mo1
Smemory<char> Mo2; Instantiate the Smemory and create the object Mo2
for (i=0; i<8;i++)
{
Mo1.mput (i); Call member function mput ()
Mo2.mput (CH); ch++; Call member function mput ()
}
cout<< "Get mo1 =";
for (i=0;i<8;i++)
Cout<<mo1.mget (); Call member function Mget ()
cout<< "\nget Mo2 =";
for (i=0;i<8;i++)
Cout<<mo2.mget (); Call member function Mget ()
Getch ();
}
The running result of the program is:
Get mo1=> 76543210
Get mo2=> HGFEDCBA


Description: Class template smemory with a type parameter t,t is a parameter that represents the data type, the keyword class must precede the type parameter, and class represents the type of the type parameter. When instantiated, the corresponding type parameter T must be a specific data type name, where the first object is created with the data type name int, indicating that all type parameter T in the class template smemory is replaced by an int, creating a concrete class and its object mo1 When the second object is created, the data type name is char, which means that all type parameter T in template smemory is replaced by char, creating a concrete class and its object Mo2.

C + + class templates

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.