C++ customizes the class template and overrides all members defined in the class template or specified members

Directory

1. Customize a class template to cover all members defined in the class template.

1. Briefly describe the general process of this work

(1), define common classes

(2), define the template class

(3), custom template class

(4), main function

2. How to customize the template

3. An uncommon C++ grammatical structure: a half-width colon is used for the initialization list start identifier in the constructor

4. Examples of the whole process

5. Debug the program step by step, pay attention to and observe the matching of actual participation formal parameters

2. Customize a class template to override the specified members defined in the class template.

1. The method of customizing the template to cover the specified part of the members

2. Examples of the whole process

3. Debug the program step by step, pay attention to and observe the matching of actual participation formal parameters;

3. Summary


After customizing the template class, if you need to extend the tired functions of the template, you must cover the class template so that the template class can complete special functions.

The overriding operation can target the entire class template, part of the class template, and member functions of the class template. This overriding operation is called customization.

This process is illustrated with a case.

1. Customize a class template to override all members defined in the class template.

1. Briefly describe the general process of this work

(1), define common class

  • Define a common class class Date, this class has 3 data parameters int iMonth, iDay, iYear;
  • Define the constructor, friend overload operator << function, and display output function in this common class;

(2), define template class

  • Define the template class template class Set where the parameter t is T type, that is, the standard data type;
  • Define the constructor and initialize the constructor. t is a formal parameter, the data type is T, instantiated with the actual parameter st, of course, it can also be instantiated with intset, dt, etc.;
  • show output function;

(3), custom template class

  • Custom template class, extension, specialized template class template<> class Set, replace T type with Date type;
  • Cover all class bodies of class Set;

(4), main function

  • Set intset(123);
  • Instantiation description: define template class object, data type int (note that int is used instead of T), define class Set object variable intset (use intset instead of formal parameter t), assign 123 to actual parameter intset and replace formal parameter st;
  • Set intset1(123);

  • Instructions for instantiation: customize template class object, data type Date, define class Set object variable intset1 (use intset1 instead of formal parameter t), assign value 123 to actual parameter intset1 and replace formal parameter st;

2. How to customize the template

Template<> should be added to the specialization of the member function of the template class; the specialization of the member function of the template class needs to match the return value type with the template;

template <class T> class Set { T display(); }; //declare
template<> class Set<Date> { Date display(); }; //specialization

3. An unusual C++ grammatical structure: half-width colons are used for constructors. Initializer list start identifier

This grammatical structure is not explained in general textbooks, and it is used but not explained in individual books. This grammatical structure is: The half-width colon is used for the initialization list start identifier in the constructor.

Set(Date st):t(st){}; //The definition is inside the class at this time, if it is defined outside, there will be a set::

// Equivalent to
set(data st)
{
    t = st;
}

Both usages are equivalent, the second usage is easier to understand.

The difference is that the second form t will perform one more step to construct t with the default constructor, and then call the assignment operator in the statement block of the constructor body to assign a value to t again.
In addition, for some special cases, it must be written in the initialization list, such as the initialization of reference properties. Because that is the initialization, the statement block is the reassignment after initialization.

4. Examples of the entire process

/*Customize a class template to override all members defined in the class template*/
#include <iostream>
using namespace std;
//Define common class
class Date
{
int iMonth, iDay, iYear;
/*char Format[128];*/
public:
Date(int m=0,int d=0,int y=0) //Define the constructor
{
iMonth=m;
iDay=d;
iYear=y;
}
friend ostream & amp; operator<<(ostream & amp; os, const Date t)//Define a friend overloaded operator '<<' for output Date type
{
cout << "Month: " << t.iMonth << ' ' ;
cout << "Day: " << t.iDay<< ' ';
cout << "Year: " << t.iYear<< ' ' ;
return os;
\t\t
}
void Display() //Define member functions
{
cout << "Month: " << iMonth;
cout << "Day: " << iDay;
cout << "Year: " << iYear;
cout << endl;
}
};
//Define a class template and share a class template
template <class T>
//Define the template class Set, where the parameter t is an int type, the Date type cannot be recognized, and the Date type is input, warning E0289
class set
{
T t; //Define class variables
public:
Set(T st) : t(st) {} //Define the constructor, the colon is the mark of the beginning of the initialization list in the constructor Set(T st) {t = st;}
//t is a formal parameter, the data type is T, instantiated with the actual parameter st, of course, it can also be instantiated with at, mt
void Display() //Define member functions
{
cout << t << endl;
}
};
// Customize the new template class Set<Date>, overwrite the original class parameters, and share a class template <class T>
//The parameter t is Date type
//Must add template<> otherwise warning C2906: "Set<Date>": explicit specialization requires "template<>"
template<> class Set<Date>
{
Date t; //Define class variables
public:
Set(Date st): t(st){} //Define the constructor, the colon is the mark of the beginning of the initialization list in the constructor Set(Data st) {t = st;}
//t is a formal parameter, the data type is Data, instantiated with the actual parameter st, of course, it can also be instantiated with at, mt
void Display() //Define member functions
{
cout << "Date :" << t << endl;
}
};

int main()
{
Set<int> intset(123);
intset. Display();

//Note that if the above <int> is replaced with <Date>, the result will be different, and then modify the actual parameter to Date type
Set<Date> intset1(123);
intset1. Display();

Set<Date> intset2(Date(04, 20, 2023));
intset2. Display();

Set<Date> dt(Date(04,20,2023));
dt. Display();

Set<Date> dt1=Date(04, 21, 2023);
dt1. Display();
}
/*operation result
123
Date : Month: 123 Day: 0 Year: 0
Date : Month: 4 Day: 20 Year: 2023
Date : Month: 4 Day: 20 Year: 2023
Date: Month: 4 Day: 21 Year: 2023 */

5. Debug the program step by step, pay attention to and observe the matching of actual parameters

2. Customize a class template to override the specified members defined in the class template.

1. Method for overriding some members specified by custom template

Replace custom templates with types that override specified member functions with scope::. The general expression is:

void Set<T>::Display() //Customize the new class and override some member functions
{
cout << t << endl;
}
void Set<Date>::Display() // Customize the new class and override some member functions
{
cout << "Date: " << t << endl;
}

Only the methods of the custom class are listed here, and the source class definition is detailed in the source code below. The meaning of the general expression is that the custom class substitutes the actual parameter type T (basic type) and actual parameter type Data (date type) into the source class formal parameters, and then selects the corresponding custom class output and displays according to the instantiation of the actual parameter type result.

2. An example of the entire process

/*Customize a class template to override the specified members defined in the class template*/
#include 
using namespace std;
class Date //Define common classes
{
int iMonth, iDay, iYear;
public:
Date(int m=0,int d=0,int y=0) //Define the constructor
{
iMonth=m;
iDay=d;
iYear=y;
}
friend ostream & amp; operator<<(ostream & amp; os,const Date t)//Definition of friend ostream & amp; operator< //Define a class template and share a class template
class Set //Define the template class Set, where the parameter t is an int type, and the Date type cannot be recognized, and the Date type is input, warning E0289
{
T t;
public:
Set(T st) : t(st) {} //Define the constructor, the colon is the mark of the beginning of the initialization list in the constructor Set(T st) {t = st;}
void Display(); //Define an empty member function
};
template  // cannot be commented out
//Override some members in the class template: class scope::member function
void Set<T>::Display() //Customize the new class and override some member functions
{
cout << t << endl;
}
void Set<Date>::Display() // Customize the new class and override some member functions
{
cout << "Date: " << t << endl;
}
int main()
{
Set intset(123); //call class Set, the actual parameter int is substituted into the formal parameter T, the object variable intset is substituted into the formal parameter st, the object instance 123 is substituted into the formal parameter t, and the instantiation is completed
intset.Display(); //Call the member function of the first override template class void Set::Display()
\t
Set dt =Date(14,20,2023); //call the constructor of class Date->assignment->class Set constructor
dt.Display(); //Call the second override template class member function void Set::Display()->friend overload operator<< ->void Set::Display() -> output t
}
/*operation result:
123
Date: Month: 14 Day: 20 Year: 2023 */

3. Debug the program step by step, pay attention to and observe the matching of actual parameters;

3. Summary

Customize and specialize new classes according to the type of actual parameters, and select the corresponding class output results according to the instantiation of actual parameters, which is the core meaning of template class customization.