12 Mart 2010 Cuma

C++ Constructor, Deconstructor and Delete Example

//www.ethemsulan.com
#include <cstdlib>
#include <iostream>
using namespace std;
class array{
      int *p;
      int size;
      public:
             array(int sz){
                       p=new int[sz];
                       if(!p) exit(1);
                       size=sz;
                       cout<<"normal yapici\n";
                       }
             ~array(){delete [] p;}
             array(const array &a);
             void put(int i,int j){
                  if(i>=0 && i<size) p[i]=j;
                  }
             int get(int i){
                 return p[i];
                 }
      };
array::array(const array &a){
                   int i;
                   size=a.size;
                   p=new int[a.size];
                   if(!p) exit(1);
                   for(i=0;i<a.size;i++) p[i]=a.p[i];
                   cout<<"kopya yapicisi\n";
                   }
int main(int argc, char *argv[])
{
    array num(10);
    int i;
    for(i=0;i<10;i++) num.put(i,i);
    for(i=9;i>=0;i--) cout<<num.get(i)<<"\t";
                      cout<<endl;
    array x=num;
    for(i=0;i<10;i++) cout<<x.get(i)<<"\t";
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hiç yorum yok:

Yorum Gönder