1,首先考虑一个问题:
型别T会影响class的行为吗?
如果不影响,你可以使用template.
如果影响,你必须使用虚拟函数,并因而使用继承机制.
2,来看一个模版的实例:
#include <iostream>
using namespace std;
template<class T>
class Stack
{
public:
Stack();
void push(const T& t);
T& pop();
T& top();
bool empty();
int size();
~Stack();
private:
struct StackNode
{
StackNode(const T& t, StackNode *nextNode) : data(t),next(nextNode){}
T data;
StackNode* next;
};
StackNode* head;
int cnt;
Stack(const Stack&);
Stack& operator=(const Stack&);
};
template<class T>
Stack<T>::Stack() : head(NULL),cnt(0) {}
template<class T>
void Stack<T>::push(const T& t)
{
head = new StackNode(t, head);
cnt++;
}
template<class T>
T& Stack<T>::pop()
{
if (!empty())
{
StackNode* tmp = head;
head = head -> next;
T tmpData = tmp -> data;
delete tmp;
cnt--;
return tmpData;
}
}
template<class T>
T& Stack<T>::top()
{
if (!empty())
return head -> data;
}
template<class T>
bool Stack<T>::empty()
{
return head == NULL;
}
template<class T>
int Stack<T>::size()
{
return cnt;
}
template<class T>
Stack<T>::~Stack()
{
while (head)
{
StackNode* tmp = head;
head = head -> next;
delete tmp;
}
}
int main()
{
Stack<int> myStack;
myStack.push(20);
myStack.push(30);
myStack.push(40);
cout << myStack.pop() <<endl;
cout << myStack.size() <<endl;
cout << myStack.top() <<endl;
return 0;
}
3,总结:
template用来产生一群class,其中对象型别不会影响class的函数行为.
inheritance用于一群class身上,其中对象型别会影响class的函数行为.
分享到:
相关推荐
条款41:了解隐式接口和编译期多态 understand implicit interfaces and compile-time polymorphism. 条款42:了解typename的双重意义 understand the two meanings of typename. 条款43:学习处理模板化基类内的...
条款34:区分接口继承和实现继承 条款35:考虚virtual函数以外的其他选择 条款36:绝不重新定义继承而来的non-virtual函数 条款37:绝不重新定义继承而来的缺省参数值 条款38:通过复合塑模出has-a或“根据某物...
C程序设计教学课件:CHAPTER8INHERITANCE.pptx
C 程序设计教学课件:CHAPTER 8 INHERITANCE.ppt
C++程序设计教学课件:CHAPTER 8 INHERITANCE.ppt
面向对象程序设计英文教学课件:10-Inheritance.pptx
面向对象程序设计英文教学课件:10_Inheritance.ppt
ActiveRecord Inheritance是为Yii2框架提供的实用程序。 其动机是在两个ActiveRecord类之间伪造继承。 安装 将程序包作为依赖项包含在bower.json文件下。 要安装,请运行 $ ...
条款24:了解 virtual functions、multiple inheritance、virtual base classes、 runtime type identification 所需的成本 113 Understand the costs of virtual functions, multiple inheritance, virtual base ...
软件设计课件:Lecture 08 CPP Inheritance.ppt
Chapter 9: Class Inheritance and Virtual Functions Chapter 10: The Standard Template Library Chapter 11: Debugging Techniques Chapter 12: Windows Programming Concepts Chapter 13: Windows Programming...
Chapter 10: Discovering Inheritance Techniques Chapter 11: Writing Generic Code with Templates Chapter 12: Understanding C++ Quirks and Oddities Part III: Mastering Advanced Features of C++ ...
Type inheritance is that phenomenon according to which we can say, for example, that every square is also a rectangle, and so properties that apply to rectangles in general apply to squares in ...
用中国式思维理解面向对象,pdf格式,内容包含:目彔 Catelog序言 Perface真经第一章:世界 Waltanschauung真经第二章:抽象 Abstraction真经第三章:层次 Arrangement真经第四章:继承 Inheritance真经第亐章:...
用c语言编写的遗传算法程序包,无需解压密码
Chapter 8: Introducing Inheritance Chapter 9: Using Templates Chapter 10: Using Streams and Files Chapter 11: Errors and Exception Chapter 12: Programming with Windows Chapter 13: Using DirectX ...
PPT格式的文档 关于uml的课件,关于java的继承问题
template-inheritance
Chapter 11:Advanced Inheritance Concepts Chapter 12:Exception Handling Chapter 13:File Input and Output Chapter 14:Introduction to Swing Components Chapter 15:Using JavaFX and Scene Builder Appendix A...