Hoşgeldin Misafir

C++ İle Hafızandan Yer Ayırma

MiRaT

13 Kas 2017
2,077 Mesaj

Aktiflik

Seviye

Deneyim

TIM / GÖREV:
Str.h

Kod:
#ifndef STR_H
#define STR_H
#include <iostream>
class Str
{
    public:
        Str(int sinir);
        virtual ~Str();
        **** harfEkle(char c);
        **** yaz();
    protected:
    private:
        char *p;  //hafızada ayrılan yerin başlangıç adresi
        int len; //uzunluk
        int pos; //imleç yeri
};
 
#endif // STR_H


str.cpp

Kod:
#include "str.h"
#include <cstdlib>
Str::Str(int sinir)
{
    //ctor
    len = sinir;
    std::cout<<"ayrilan yer:"<<len<<"\n";
    p = (char *)malloc(len);
    if(!p)
    {
        std::cout<<"vermediler";
        exit(1);
    }
    else
    std::cout<<"verdiler\n";
    *p = \n ; //sonlandır
    pos = 0;
}
 
Str::~Str()
{
    //dtor
    std::cout<<"iade ediyorum\n";
    free(p);
}
 
**** Str::harfEkle(char c)
{
    p[pos] = c;
    pos++;
    p[pos] = \n ;
}
**** Str::yaz()
{
    std::cout<<p<<"\n";
}


main.cpp

Kod:
#include <iostream>
#include "str.h"
using namespace std;
 
int main()
{
    Str btr(32);
    btr.harfEkle( Y );
    btr.harfEkle( e );
    btr.harfEkle( t );
    btr.harfEkle( e );
    btr.harfEkle( r );
    btr.harfEkle( ! );
    btr.yaz();
    int z;
    cin>>z;
    return 0;