9 Mart 2010 Salı

C++ da malloc() ve strcpy() Foksiyonların Kullanımı

//www.ethemsulan.com
#include <cstdlib>
#include <iostream>
#include <cstdlib>
#define size 255
using namespace std;
class strtype{
      char *p;
      int len;
      public:
             strtype();
             ~strtype();
             void set(char *ptr);
             void show();
      };
strtype::strtype(){
                   p=(char *)malloc(size);
                   if(!p){
cout<<"bellekte yer ayirma hatasi\n";
                          exit(1);
                          }
                   *p='\0';
                   len=0;
                   }
strtype::~strtype(){
cout<<"p, serbest birakiliyor\n";
                    free(p);
                    }
void strtype::set(char *ptr){
     if(strlen(p)>=size){
  cout<<"katar fazla buyuk\n";
                         return;
                         }
     else{
          strcpy(p,ptr);
          len=strlen(p);
          }
     }
void strtype::show(){
     cout<<p<<" uzunlugu="<<len<<endl;
     cout<<endl;
     }
int main(int argc, char *argv[])
{
    strtype ob1,ob2;
    ob1.set("HACKER KEWIN MITNIC");
    ob1.show();
    ob2.set("MEHMET ETHEM SULAN");
    ob2.show();
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hiç yorum yok:

Yorum Gönder