12 Mart 2010 Cuma

C++ da strcpy(), strlen() ve delete Örneği

//www.ethemsulan.com
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
class strtype{
      char *p;
      public:
             strtype(char *s);
             ~strtype(){delete [] p;}
             char *get() {return p;}
      };
strtype::strtype(char *s){
                      int l;
                      l=strlen(s)+1;
                      p=new char[l];
                      if(!p){
cout<<"bellekte yer ayirma hatasi\n";
                             exit(1);
                             }
                      strcpy(p,s);
                      }
void show(strtype x){
     char *s;
     s=x.get();
     cout<<s<<endl;
     }
int main(int argc, char *argv[])
{
    strtype a("merhaba"),b("Nasil");
    show(a);
    show(b);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hiç yorum yok:

Yorum Gönder