C++中使用了模板来减少方法相同但是类型不一样带来的函数重载以及大量复制代码的问题。这里主要说说类模板
 
   类模板的定义:
 
   template<TYPENAME Type>
 
   class Stacks
 
   {
 
   public:
 
   Stacks(void);
 
   Stacks(int nSize);
 
   Stacks(Type Arr[],int nSize);
 
   ~Stacks(void);
 
   public:
 
   bool isEmpty();
 
   bool isFull();
 
   bool push(const Type &e);
 
   Type pop();
 
   Type getTop();
 
   int getLength();
 
   void print();
 
   private:
 
   int m_top;
 
   int m_nLength;
 
   Type *m_pArr;
 
   };
 
   函数的实现:
 
   函数实现既可以放在类定义体内,又可以放在类定义体外,但是不管放在体内还是体外都必须放在头文件中,这点和inline函数有点类似。
 
   分离编译托福答案 www.yztrans.com
 
   如果声明和定义分别放在h文件和cpp文件呢?
 
   这个时候如果在其他程序中用到了模板类中的函数,那么就会出现
 
 
   然后我们在main中调用print函数,在生成的debug文件中用notepad++打开main.obj,我们可以看到有这样的字符出现:
                www.lefeng123.com
 
   这就说明了问题,main.cpp中实例化了函数的定义。
 
   附带栈结构实现的C++源代码
 
   // Stacks.h
 
   #pragma once
 
   #include <IOSTREAM>
 
   using namespace std;
 
   template<TYPENAME Type>
 
   class Stacks
 
   {
 
   public:
 
   Stacks(void);
 
   Stacks(int nSize);
 
   Stacks(Type Arr[],int nSize);
 
   ~Stacks(void);
 
   public:
 
   bool isEmpty();
 
   bool isFull();
 
   bool push(const Type &e);
 
   Type pop();
 
   Type getTop();
 
   int getLength();
 
   void print();
 
   private:
 
   int m_top;
 
   int m_nLength;
 
   Type *m_pArr;
 
   };
 
   template<TYPENAME Type>
 
   Stacks<TYPE>::Stacks(void)
 
   {
 
   m_top = -1;
 
   m_nLength = 10;
 
   m_pArr = new Type[10];
 
   if (m_pArr == NULL)
 
   cout《"Allocate stack failed"《ENDL; Type } template<typename m_top="m_nLength-1;" else>
 
   Stacks<TYPE>::Stacks(int nSize)
 
   {
 
   m_top = -1;
 
   m_pArr = new Type[nSize];
 
   if(m_pArr == NULL)
 
   cout《"Allocate stack failed"《ENDL; Type } template<typename m_top="nSize-1;" else m_nLength="nSize;" {>
 
   Stacks<TYPE>::Stacks(Type Arr[],int nSize)
 
   {
 
   m_top = -1;
 
   m_pArr = new Type[nSize];
 
   if(m_pArr == NULL)
 
   cout《"Allocate stack failed"《ENDL; Type } template<typename m_top="nSize" else m_nLength="nSize;" { 1; - m_pArr[i]="Arr[i];" i="0;i<nSize;i++)" for(int>
 
   bool Stacks<TYPE>::isEmpty()
 
   {
 
   return m_top == -1;
 
   }
 
   template<TYPENAME Type>
 
   bool Stacks<TYPE>::isFull()
 
   {
 
   return m_top == m_nLength - 1;
 
   }
 
   template<TYPENAME Type>
 
   Type Stacks<TYPE>::getTop()
 
   {
 
   if (m_top != -1)
 
   return m_pArr[m_top];
 
   else
 
   {
 
   cout《"The stack is empty"《ENDL; Type } template<typename exit(1);>
 
   Type Stacks<TYPE>::pop()
 
   {
 
   if(m_top == -1)
 
   {
 
   cout《"The stack is empty"《ENDL; Type } template<typename else { exit(1); m_pArr[m_top+1]; return m_top--;>
 
   bool Stacks<TYPE>::push(const Type &e)
 
   {
 
   if(m_top == m_nLength-1)
 
   {
 
   cout《"The stack is full"《ENDL; Type } template<typename else { return true; m_pArr[m_top]="e;" m_top++; false;>
 
   int Stacks<TYPE>::getLength()
 
   {
 
   return m_nLength;
 
   }
 
   template<TYPENAME Type>
 
   Stacks<TYPE>::~Stacks(void)
 
   {
 
   if(m_pArr != NULL)
 
   {
 
   delete m_pArr;
 
   m_pArr = NULL;
 
   }
 
   }
 
   template<TYPENAME Type>
 
   void Stacks<TYPE>::print()
 
   {
 
   cout《"This is a test ,I love you"
 
   《"I love you,I love you"《ENDL; class="brush":java; }<pre>
 
   //main.cpp
 
   #include<IOSTREAM>
 
   #include<STRING>
 
   #include"Stacks.h"
 
   using namespace std;
 
   void main()
 
   {
 
   int a[7] = {1,2,3,4,5,6,7};
 
   Stacks<INT> Sta(a,7);
 
   cout《STA.ISFULL()《ENDL; pre } { i="0;i<7;i++)" for(int }< system(?pause?); Sta.print(); cout《Sta.getLength()《endl; cout《Sta.isEmpty()《endl; is:?《top《endl; top="Sta.pop();" cout《?The int><BR>
 
   <P></P>
 
   <P><BR>
 
   </P>
 
   <P><BR>
 
   </P>
 
   <P><BR>
 
   </P>
 
   <BR>
 
   <BR>

最新文章

  1. asp.net MVC4 异步文件上传
  2. myeclipse如何设置字体?
  3. 网络拥塞控制(三) TCP拥塞控制算法
  4. ngx_cdecl
  5. 配置Memcache服务器并实现主从复制功能(repcached)(转)
  6. Android基础之响应Menu键弹出菜单Demo
  7. 深入理解最强桌面地图控件GMAP.NET ---离线地图
  8. Simple Validation in WPF
  9. _3_form_标签
  10. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
  11. 使用JS+Three.js+Echart开发商场室内地图客流信息统计功能
  12. OO第二单元作业小结
  13. IIS网站部署后,程序常见错误记录
  14. js实现全屏和缩放
  15. 老毛子 Padavan 路由器固件开启教育网 IPv6 并实现IPv6转发
  16. 纯html页面中js如何获得项目路径
  17. web基础,用html元素制作web页面
  18. Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value
  19. js-ES6学习笔记-Class(3)
  20. rz -be 上传文件解压失败

热门文章

  1. has-a关系——包含对象成员的类
  2. 在Eclipse中新建Maven项目
  3. 【COM学习】之一、QueryInterface
  4. shell编程(一)--常用变量及表达式
  5. 只有20行Javascript代码!手把手教你写一个页面模板引擎
  6. VS2008 ctrl+shift+F热键冲突
  7. SKAction类
  8. 高仿“点触验证码”做的一个静态Html例子
  9. Swift开发语法
  10. [转] IPC之管道、FIFO、socketpair