<span style="font-size:18px;">#include <iostream>
#include <typeinfo>
using namespace std;
//using std::cout;
//using std::cin;
//using std::endl;
template<typename T,int maxSize=100> class stack{
public:
void push(T& a);
T pop();
stack( int maxSize);//int maxSize
void show();
bool isFull();
bool isEmpty();
int getSize();
T at(int i);
stack<T,maxSize>& operator=(stack<T,maxSize>& a)
{
cout<<"a-jNum:"<<a.getSize()<<endl;
cout<<"a-maxNum:"<<a.getMaxSize()<<endl;
cout <<"a-id:"<<typeid(a).name()<<endl;
delete[] m_pobj;m_pobj=NULL;
int capcity=a.getMaxSize();
m_iMaxSize=a.getMaxSize();
m_pobj=new T[capcity];
m_iSize=a.getSize();
cout<<"this-jNum:"<<getSize()<<endl;
cout<<"this-MaxNum:"<<getMaxSize()<<endl;
cout<<"at"<<endl;
for (int i=0;i<m_iSize;i++)
m_pobj[i]=a.at(i);
show();
return *this;
}
int getMaxSize();
~stack();
private:
T * m_pobj;
int m_iSize;
int m_iMaxSize;
};
template <typename T,int maxSize> int stack<T,maxSize>::getMaxSize()
{
return m_iMaxSize;
}
//模板 就是先定义类型 在定义返回值 类名<专门化> 到作用域 :: 函数名(参数):数据成员的默认值
//定义变量的时候 stack<int , 30 > 这样其实是一个数据类型 如同int型。相当一个可变组合数据类型
//stack<int , 30 >中30 都是一个类型了。</span>
template <typename T,int maxSize> int stack<T,maxSize>::getSize()//maxSize 的名字不能改变,不然会报错。
{
return m_iSize;
}
template <typename T,int maxSize> T stack<T,maxSize>::at(int i)//maxSize 的名字不能改变,不然会报错。
{
return m_pobj[i];
}
template<typename T,int maxSize> stack<T, maxSize >::stack(int maxSize )//:m_iMaxSize=maxSize//int maxSize
{
m_iMaxSize=maxSize;
m_iSize=0;
m_pobj=new T[m_iMaxSize];
cout<<"m_iMaxSize:"<<m_iMaxSize<<endl;
cout<<"maxSize:"<<maxSize<<endl;
}
template<typename T,int maxSize> void stack<T, maxSize >::push(T& a)
{
m_pobj[m_iSize++]=a;
}
template<typename T,int maxSize> void stack<T, maxSize >::show()
{
for (int i=0;i<m_iSize;i++)
{
cout<<m_pobj[i]<<",";
}
}
template <typename T,int maxSize> T stack<T,maxSize>::pop()
{
m_iSize--;
return m_pobj[m_iSize];
}
template <typename T,int maxSize> bool stack<T,maxSize>::isFull()
{
return m_iSize>=m_iMaxSize;
}
template<typename T,int maxSize> bool stack<T,maxSize>::isEmpty()
{
return m_iSize==0;
}
template<typename T,int maxSize> stack<T,maxSize> ::~stack()
{
delete[] m_pobj;
}
//函数operate 运算符重载不能卸载类外,要写在类外就用friend 函数。</span>
//template<typename T int maxSize> stack<T,maxSize> stack<T,maxSize>::operator=(stack<T,maxSize> a)
//{
// delete[] m_pobj;
// int capcity=m_iMaxSize;
// m_pobj=new T[capcity];
// return *this;
//}
int main()
{
int a;
int sizeMax=50;
stack<int,80> stackTest1(sizeMax);
stack<int> stackTest2(sizeMax);
cout <<"stackTest1-id:"<<typeid(stackTest1).name()<<endl;
cout <<"stackTest2-id:"<<typeid(stackTest2).name()<<endl;</span>
stack<int,30> stackobj(sizeMax);
//stack<int> stackobj(sizeMax);//,30去掉的画 后面的赋值 newstack=stackobj;就编译不过,因为不是同一个数据类型了。
for (int k=0;k<sizeMax;k++)
{
stackobj.push(k);
}
cout<<endl;
cout<<"show push"<<endl;
stackobj.show();
cout<<endl;
cout<<"show pop"<<endl;
for (int j=0;j<sizeMax;j++)
{
cout<<stackobj.pop()<<", ";
}
for (int k=0;k<sizeMax;k++)
{
stackobj.push(k);
}
cout<<endl;
cout<<"show push2"<<endl;
stackobj.show();
stack<int,30> newstack(70);
cout<<"newstackNum:"<<newstack.getSize()<<endl;
newstack=stackobj;
cout<<"newstackNum2:"<<newstack.getSize()<<endl;
cin>>a;
}</span>
C++模板的理解
最新推荐文章于 2022-06-29 21:04:36 发布