- 6
- 0
- 约1.15万字
- 约 47页
- 2021-11-30 发布于安徽
- 举报
fig22_02.cpp (Part 2 of 2) Program Output Pushing elements onto doubleStack 1.1 2.2 3.3 4.4 5.5 Stack is full. Cannot push 6.6 ? Popping elements from doubleStack 5.5 4.4 3.3 2.2 1.1 Stack is empty. Cannot pop ? Pushing elements onto intStack 1 2 3 4 5 6 7 8 9 10 Stack is full. Cannot push 11 ? Popping elements from intStack 10 9 8 7 6 5 4 3 2 1 Stack is empty. Cannot pop Can use non-type parameters in class templates 可以是常整数(包括枚举)、指向外部链接对象的指针,而浮点数,指向内部链接对象的指针则不行。 must be constant at compile time Example: Template class T, int elements Stack double, 100 mostRecentSalesFigures; Defines object of type Stack double, 100 Template parameters Non-type parameters are resolved at compile time, not runtime Example: This may appear in the class definition: Template class T, int elements T stackHolder[ elements ]; //array to hold stack The array is created at compile time, rather than dynamic allocation at execution time Template parameters Template parameters Class templates can have default arguments for type or value parameters. template class T = long class A; Function templates CANNOT have default arguments Classes can be overridden For template class Array, define a class named ArraymyCreatedType This new class overrides the class template for myCreatedType The template remains for unoverriden types Class template specialization 应用场景:即想使用模板,同时又需要对一个特殊类型做不同的实现。 // class template: template class T class specTemplate { T m_var; public: specTemplate (T inData) { m_var = inData; } T increase () { return ++m_var; } }; // class template specialization: template class specTemplate char { char m_var; public: specTemplate (char arg) { m_var = arg; } char upperCase () { if ((m_var = ’a’) (m_var = ’z’)) m_var += ’A’-’a’; return m_var; } } ; Class templates specialization A non-template class can be derived from a template class (普通类继承模板类) A template class
您可能关注的文档
- 高等数学课件:11.3 复合函数微分法.ppt
- 高等数学课件:11.4 隐函数微分法.ppt
- 高等数学课件:11.5 多元函数微分学在几何上的应用.ppt
- 高等数学课件:11.7 多元函数的极值与最值.ppt
- 高等数学课件:11.7 多元函数的极值与最值(1-36).ppt
- 高等数学课件:11-1 广义积分.ppt
- 高等数学课件:11-1一阶微分方程,可降阶方程.ppt
- 高等数学课件:11-2含参变量常义积分.ppt
- 高等数学课件:11-3 含参变量广义积分.ppt
- 高等数学课件:11-5多元微分学(1).ppt
- 中国近现代史纲要教学课件.ppt
- 电路与电子技术:Chapter 02 Resistive Circuits.pptx
- 电路与电子技术:Chapter 04 Transients.pptx
- 电路与电子技术:Chapter 05 Steady-State Sinusoidal Analysis.pptx
- 电路与电子技术:Chapter 06 Frequency Response, Bode Plots, and Resonance.pptx
- 电路与电子技术:Chapter 8 Diodes.pptx
- 电路与电子技术:Chapter 10-11 Bipolar Junction Transistors & Field Effect Transistors.pptx
- 电路与电子技术:Chapter 12 Operational Amplifiers.pptx
- 离散数学课件:Closures of Relations.pptx
- 离散数学课件:Complexity of Algorithms.pptx
原创力文档

文档评论(0)