Definition and use of C + + class templates __c++

Source: Internet
Author: User
Tags function definition int size
Like a function template, using a class template enables a user to define a pattern for a class, allowing some data members in the class, arguments to some member functions, and return values of some member functions to take any type. A class template is an abstraction of a class that is different from a group of member data types only. Programmers can greatly improve the efficiency of programming by creating a class template for the entire family of classes, giving a set of program code to generate a variety of specific classes, which can be seen as instances of class templates.

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 ,

1
2
3
4
5
6
7
8
9
10
Template < class t>
Class Smemory

{
...
Public
void Mput (T x);
...

}

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

The general form of defining class member functions outside of a 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
}
For example:

1
2
3
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 the 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 the member function definition), but it can generate a specific class and an object of that particular class by instantiating the class template.

Unlike function templates, the instantiation of a function template is done automatically by the compiler when it processes a function call, 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 replacing the type parameter T of the class template smemory with an int, creating a concrete class and producing a mol of the object of that particular class.

The following examples illustrate the definition and usage of class templates.
Example: definition and use of class templates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream.h>
#include <conio.h>
const int SIZE = 8;
Template < class t>
Class Smemory///Definition Classes template Smemory
{
T Data[size]; The type is T, an array of length size data[] is a 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 type of 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 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 the 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 the Smemory and create the 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 ();
}

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

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.