The Concept and Significance of [C++] 58_Class Template

Keywords: C++ Programming

Reflection:
Can generics be applied to classes in C++?

Class template

  • Some classes are mainly used to store and organize data elements
  • The way data is organized in a class has nothing to do with the specific types of data elements
  • For example: array class, linked list class, Stack class, Queue class, etc.

The idea of template is applied to classes in C++, so that the implementation of classes does not focus on the specific types of data elements, but only on the functions that classes need to achieve.

  • Class Template in C++

    • Handling different types in the same way
    • Use template to identify before class declaration
    • < typename T > Used to illustrate the generic type T used in classes

template < typename T >
class Operator
{
public:
    T op(T a, Tb);
};

  • Application of Class Template

    • Can only display specified specific types, can not automatically deduce

Define objects with specific type < Type >

void code()
{
    Operator<int> op1;
    Operator<string> op2;
    int i = op1.op(1, 2);
    string s = op2.op("D.T.","Software");
}

  • The declared generic type T can appear anywhere in the class template
  • The compiler handles class templates the same way as function templates

    • Generating different classes from class templates through specific types
    • Compile the class template code itself where it is declared
    • Compile the code after parameter substitution where it is used

Programming Experiments: A Preliminary Study of Class Templates

#include <iostream>
#include <string>

using namespace std;

template < typename T >
class Operator
{
public:
    T add(T a, T b)
    {
        return a + b;
    }
    
    T minus(T a, T b)
    {
        return a - b;
    }
    
    T multiply(T a, T b)
    {
        return a * b;
    }
    
    T divide(T a, T b)
    {
        return a / b;
    }
};

string operator- (string& l, string& r)
{
    return "Minus";
}

int main()
{
    Operator<int> op1;
    
    cout << op1.add(1, 2) << endl;

    Operator<string> op2;
    
    cout << op2.add("D.T.", "Software") << endl;
    cout << op2.minus("D.T.", "Software") << endl;

    return 0;
}
Output:
3
D.T.Software
Minus

Two Compilations of Class Templates by the Compiler

First time: Compile and check the template code itself

#include <iostream>
#include <string>

using namespace std;

template < typename T >
class Operator
{
public:
    T add(T a, T b)
    {
        return a + b         // Watch out here! Deliberately Made Grammatical Errors
    }
};

int main()
{

    return 0;
}
Output: g++]
test.cpp: In member function 'T Operator<T>::add(T, T)':
test.cpp:13: error: expected ';' before '}' token

Second time: Compile and check the code after parameter replacement

#include <iostream>
#include <string>

using namespace std;

template < typename T >
class Operator
{
public:
    T add(T a, T b)
    {
        return a + b;
    }
    
    T minus(T a, T b)
    {
        return a - b;
    }
    
    T multiply(T a, T b)
    {
        return a * b;
    }
    
    T divide(T a, T b)
    {
        return a / b;
    }
};

int main()
{
    Operator<string> op2;
    
    cout << op2.add("D.T.", "Software") << endl;
    cout << op2.minus("D.T.", "Software") << endl;    // Watch out here!

    return 0;
}
Output: [g++]
test.cpp: In member function 'T Operator<T>::minus(T, T) [with T =     std::basic_string<char, std::char_traits<char>, std::allocator<char> >]':
test.cpp:35:   instantiated from here
test.cpp:17: error: no match for 'operator-' in 'a - b'

Analysis:
At the second compilation, the compiler does not compile all member functions in the whole class after parameter substitution, but compiles each member function step by step separately when it is called.

Operator < string > op2; ===> compiles the constructor, and the compilation passes
 Op2.add ("D.T.", "Software") ===> compiles the add function, and the compilation passes.
Op2.minus ("D.T.", "Software") ==> compiles minus functions, subtracts strings inside functions, and compiles errors when subtraction operations are not defined in string classes.

Engineering Application of Class Template

  • Class templates must be defined in header files
  • Class templates cannot be implemented separately in different files
  • A member function defined outside a class template needs to be called a template <> declaration

Engineering application of formwork

Operator.h

#ifndef _OPERATOR_H_
#define _OPERATOR_H_

template < typename T >
class Operator
{
public:
    T add(T a, T b);
    T minus(T a, T b);
    T multiply(T a, T b);
    T divide(T a, T b);
};

template < typename T >
T Operator<T>::add(T a, T b)
{
    return a + b;
}

template < typename T >
T Operator<T>::minus(T a, T b)
{
    return a - b;
}

template < typename T >
T Operator<T>::multiply(T a, T b)
{
    return a * b;
}

template < typename T >
T Operator<T>::divide(T a, T b)
{
    return a / b;
}

#endif

main.cpp

#include <iostream>
#include "Operator.h"

using namespace std;

int main()
{
    Operator<int> op1;
    
    cout << op1.add(1, 2) << endl;
    cout << op1.minus(2, 1) << endl;
    cout << op1.multiply(1, 2) << endl;
    cout << op1.divide(2, 1) << endl;

    return 0;
}
Output:
3
1
2
2

Summary

  • The idea of generic programming can be applied to classes
  • Class templates handle different types of data in the same way
  • Class templates are well suited for writing data structure-related code
  • Class templates can only display specified types when used

The above content refers to Ditai Software College series courses, please protect the original!

Posted by Naoa on Wed, 08 May 2019 16:57:38 -0700