C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » General »

Dynamic Safe array


Posted Date: 16 Sep 2008    Resource Type: Code Snippets    Category: General
Author: anil mominMember Level: Silver    
Rating: 1 out of 5Points: 10



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");
}



Attachments

  • Dynamic Safe array (21115-16139-dynsafearr.rar)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Safe Dynamic Array  .  Dynamic Safe array  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: How to find the Largest value in the table
    Previous Resource: How to generate SecurityBot Text using C#
    Return to Discussion Resource Index
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use