Native/C++

destructor

aucd29 2013. 10. 2. 18:57
[code]
#include <turboc.h>

class Person
{
private:
    char *Name;
    int Age;

public:
    Person(const char *aName, int aAge)
    {
        // C++이 다른점은 이런식으로 new를 이용해서 동적으로 메모리
        // 할당이 다른 점이라고 생각한다. 멋지다 -_-; ㅋ
        Name = new char[strlen(aName)+1];
        strcpy(Name, aName);
        Age = aAge;
    }
    ~Person()
    {
        delete [] Name; // 할당 했던 메모리를 제거 한다...
    }

    void OutPerson()
    {
        printf("Name : %s Age : %d\n", Name, Age);
    }
};

void main()
{
    Person Man("Ulgimunduk", 25);
    Man.OutPerson();

    // 동적으로 할당할 수 도 있다.
    Person *pLady;
    pLady = new Person("SinSaImDang", 19);
    pLady->OutPerson();
    delete pLady;
}[/code]