类模板使用自定义数据类型

#include 
#include 
using namespace std;

//Array.h
template 
class Array{
public:
    Array();
	bool push(T elem);
	void display();
private:
    T *m_pArr;
	int m_iSize;
	int m_iLength;
};

template 
Array::Array ()
{
    m_iSize = size;
	m_iLength = 0;
	m_pArr = new T[m_iSize];
}

template 
void Array::display()
{
    for (int i = 0; i < m_iLength; i++)
	{
    cout << m_pArr[i] << endl;
	}
}

template
bool Array::push(T elem)
{
    if (m_iLength >= m_iSize)
    {
		return false;
	}

    m_pArr[m_iLength]=elem;
	m_iLength++;
    return true;
}
//Cordiante.h
class Coordinate {
    friend ostream& operator<<(ostream &out, Coordinate &coor);
public:
    Coordinate(int x = 0, int y = 0);
private:
    int m_iX;
    int m_iY;
};

Coordinate::Coordinate(int x , int y )// 不能写(int x = 0, int y = 0)
{
    m_iX = x;
    m_iY = y;
}

ostream &operator<<(ostream &out, Coordinate &coor)
{
	out << coor.m_iX << "," << coor.m_iY << endl;
    return out;
}

main(){
    Array arr3;
	Coordinate coor1(3,5);
	Coordinate coor2(2,8);
	arr3.push(coor1);
	arr3.push(coor2);
	arr3.display();
    system("pause") ;
    return 0;
}

类模板使用自定义数据类型_第1张图片

 

你可能感兴趣的:(c/c++,c++)