temp.h
#ifndef _TEMP_H
#define _TEMP_H
#include <iostream>
using namespace std;
template <typename T>
class Arry{
public:
Arry(const int len = 10);
~Arry();
public:
void show();
void setval(const int post, T data);
T& getval(const int id);
T& operator[](const int id);
template <typename L>
friend Arry<L> operator+(const Arry <L> &a, const Arry <L> &b);
template <typename L>
friend Arry<L> &operator++(Arry <L>&obj);
template <typename L>
friend Arry<L> operator++(Arry <L>&obj, int);
private:
T *arr;
int tail;
};
template <typename T>
Arry<T>::Arry(const int len):arr(new T[len]),tail(len)
{
for(int i = 0; i < tail; i++)
arr[i] = 'a'+i;
}
template <typename T>
Arry<T>::~Arry(){}
template <typename T>
void Arry<T>::setval(const int post, T data)
{
arr[post] = data;
}
template <typename T>
T& Arry<T>::getval(const int id)
{
return arr[id];
}
template <typename T>
void Arry<T>::show()
{
for(int i = 0; i < tail; i++)
cout << arr[i] << " ";
cout << endl;
}
template <typename T>
T& Arry<T>::operator[](const int id){
return arr[id];
}
template <typename T>
Arry<T> operator+(const Arry<T> &a, const Arry<T> &b)
{
Arry<T> tmp(a.tail+b.tail);
int i;
for(int i = 0; i < a.tail; i++)
{
tmp.arr[i] = a.arr[i];
}
for(int j = 0; j < b.tail; j++)
{
tmp.arr[i+j] = b.arr[j];
}
return tmp;
}
template <typename T>
Arry<T> &operator++(Arry <T>&obj)
{
for(int i = 0; i < obj.tail; i++)
++obj.arr[i];
return obj;
}
template <typename T>
Arry<T> operator++(Arry <T>&obj, int)
{
Arry <T> tmp;
tmp.tail = obj.tail;
for(int i = 0; i < obj.tail; i++)
tmp.arr[i] = obj.arr[i];
for(int i = 0; i < obj.tail; i++)
obj.arr[i]++;
return tmp;
}
#endif
main.cpp
#include "temp.h"
using namespace std;
int main()
{
Arry <char> a(5);
cout << "a.show--->" << " ";
a.show();
cout << "----------------------------------------" << endl;
Arry <char> p = a++;
cout << "p = a++, p.show--->" << " ";
p.show();
cout << "a.show--->" << " ";
a.show();
cout << "----------------------------------------" << endl;
++a;
cout << "++a, a.show--->" << " ";
a.show();
cout << "----------------------------------------" << endl;
return 0;
}