A preliminary study of C + + class template Learning Notes

Source: Internet
Author: User
Tags class operator

Class template

Implementation: When defining a class, give it one or more of the parameters, which represent different data types. --abstract class.

When a class template is called, the specified number of parameters is generated by the data type provided by the compilation system itself, and the corresponding template class---the detailed class.

Definition of a class template


The C + + class template is written such as the following:

Template < Type tables >        //Type tables are written as follows: Class type 1, class Type 2, ... class template name {    member function and member variable};


A member function in a class template, as defined outside the class template,

Template < Type table > return value type class templates name < type parameter name list;:: member function name (parameter) {     ...}


Use a class template to define an object's wording such as the following:
Class template Name < real type Reference > object name (constructor actual tables);
Assuming that the class template has no constructor, it can also simply write:
Class template name < real type tables > object names;

Example:

Template <class T1, class T2>class pair{public:    T1 key;  Keyword    T2 value;  Value    Pair (T1 k,t2 v): Key (k), value (v) {};    BOOL operator < (const pair<t1,t2> & P) const; };template<class t1,class t2>    //The member function that defines a Pair outside the class operator <bool pair<t1,t2>::operator< (const PAIR<T1, T2> & P) Const {return key < P.key;}  


Use of the pair class template:

int main () {    pair<string, int> student ("Tom", +);     Instantiate a class pair<string, int>    cout << student.key << "" << Student.value;     return 0;}


function templates as class template members

#include <iostream>using namespace Std;template <class t>class a{public:    template<class T2>     void Func (T2 t) {cout << t;}//member function template};int main () {    a<int> A;    A.func (' K '); The member function template func is instantiated with     return 0;}


Class templates and non-type parameters

Template <class T, int size>class carray{    t array[size];p ublic:    void Print ()     {for        (int i = 0; I &l T Size ++i)        cout << array[i] << Endl;     }       ;


Carray<double, 40> A2; Carray<int, 50> A3;


Note:
Carray<int,40> and carray<int,50> are totally two classes.
The objects of these two classes cannot be assigned to one another

Class templates and inheritance

Generic classes derive from template classes


Template <class t>class A {T v1;  int n; };class b:public a<int> {double v;  };/ /Class A is a class template,a<int> is a template class int main () {     B obj1;     return 0;}


Class templates derive from template classes

Template <class T1, class T2>class A {T1 v1; T2 v2;    }; Template <class t>class b:public  a<int, double> {T v;}; int main () {     b<char> obj1; return 0;}


Class template A, instantiation template class a<int,double>, derived class template B, instantiation template class b<char>

There are members in B at this time

int v1;

Double v2;

T v;

Class templates derive from ordinary classes

Class A {  int v1;}; Template <class t>class b:public A  {T v;}; int main () {     b<char> obj1;     return 0; }

There's nothing to say. A member of type T was added on the basis of inheriting a.

Class templates derive from class templates

#include <iostream>using namespace Std;template <class T1, class T2>class a{public:    T1 v1; T2 v2;}; Template <class T1, class T2>class b:public a<t1,t2>{public:    T1 v3; T2 v4;}; int main () {    b<int,double> B;    b<int,double> *PB = &b;    B.V1 = 1;    B.v3 = 3;    B.v2 = 2.2;    B.V4 = 4.4;    cout << pb->v1<<endl;    cout << pb->v2<<endl;    cout << pb->v3<<endl;    cout << pb->v4<<endl;    return 0;}

Make sure that class template A is instantiated with the type T as it is instantiated for class template B.

B<int,double> B. B has its own member int v3, double v4 also have members that inherit a<int,double> int v1 and double v2

A preliminary study of C + + class template Learning Notes

Related Article

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.