Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply Class template in C++

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to apply class templates in C++". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to apply class templates in C++.

First, the introduction of class templates:

1. Can the idea of generics be applied to classes in C++?

In the previous two articles, we have a clear understanding of the function template, as an analogy learning, of course, the idea of generics can be applied to classes.

2. Class template:

Some classes are mainly used to store and organize data elements

The way the data in the class is organized has nothing to do with the specific type of the data element

Such as: array class, linked list class, Stack class, Queue class and so on.

In C++, the idea of template is applied to classes, so that the implementation of the class does not focus on the specific types of data elements, but only on the functions that the class needs to implement.

3. Templates in C++:

Deal with different types in the same way

Use template for identification before class declaration

Template

< typename T >

/ / T indicates a general reference type

Class Operator

{

Public:

T op (T a, T b)

}

4. Application of class template.

Can only display specified specific types, and cannot be deduced automatically (our function template can automatically deduce specific types)

/ / define an object using a specific type "'

Operator op1

Operator op2

Int I = op1.op (1Jue 2)

String s = op2.op ("txp", "C++")

The declared general reference type T can appear anywhere in the class template

The compiler treats class templates in the same way as function templates

-generate 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

Code practice:

# include

# include

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) / / Global function overload-operator, compiled; first inside the class, then global

{

Return "Minus"

}

Int main ()

{

Operator op1

Cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report