The Following Program is an illustration of Dynamic Arrays which have the property to expand during runtime this array is implemented using pointer and node with an array which can be gained using runtime. Enjoy this code.
//********************Header File
template <class TT>
class dynm
{ public: TT *data; int size; dynm(); dynm(int=0); dynm(const dynm &rhs); dynm &operator =(const dynm &rhs); const TT& operator [](const int) const; TT& operator [](int); friend ostream& operator << (ostream& output,const dynm<TT>& rhs) { for (int i = 0 ; i < 5 ; i++) output << rhs[i]<<endl; return output; } ~dynm(); };
template <class TT> dynm<TT> :: dynm() : data(0),size(0) {}
template <class TT> dynm<TT> :: dynm(int s) : size(s) { data = new TT[size];
} template <class TT> dynm<TT> :: dynm(const dynm<TT> &rhs) { size=rhs.size; if(data) { delete data; data = 0;} data = new TT[size]; memcpy(data,rhs.data,sizeof(TT) * size);
} template <class TT> const TT& dynm<TT> :: operator [] (const int num)const //lhs { return *(data+num); }
template <class TT> TT& dynm<TT> :: operator [] ( int num) //lhs { return *(data+num); } template <class TT> dynm<TT>& dynm<TT> :: operator = (const dynm<TT> &rhs)
{ if (this == &rhs) return *this; size=rhs.size; if(data) { delete []data; data = 0;} data = new TT[size]; memcpy(data,rhs.data,sizeof(TT)* size); return *this; }
template <class TT> dynm<TT> :: ~dynm() { if (data) { delete []data; data = 0; } }
//*************Main*********
#include <iostream> using namespace std;
#include <stdlib.h> #include "dynm.h"
int main() { dynm<int> d(5); for(int i= 0 ;i< 5; i++) d[i] = i+1; cout << d; system("pause"); }
AttachmentsDynamic Safe array (21115-16139-dynsafearr.rar)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|