12 Mart 2010 Cuma

C++ da Array Tanımlama

//www.ethemsulan.com
#include <cstdlib>
#include <iostream>
using namespace std;
class array{
      int size;
      char *p;
      public:
             array(int num);
             ~array(){delete [] p;}
             char &put(int i);
             char get(int i);
      };
array::array(int num){
                 p=new char[num];
                 if(!p){
                        cout<<"memory is full\n";
                        exit(1);
                        }
                 size=num;
                 }
char &array::put(int i){
     if(i<0 || i>=size){
            cout<<"sinir hatasi\n";
            exit(1);
            }
     return p[i];
     }
char array::get(int i){
     if(i<0 || i>=size){
            cout<<"sinir hatasi";
            exit(1);
            }
     return p[i];
     }
int main(int argc, char *argv[])
{
    int i;
    array dizi(10);
    dizi.put(0)='K';
    dizi.put(1)='E';
    dizi.put(2)='W';
    dizi.put(3)='I';
    dizi.put(4)='N';
    for(i=0;i<5;i++) cout<<dizi.get(i)<<"\t";
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hiç yorum yok:

Yorum Gönder