본문 바로가기

C++

클랙스 동적 할당과 해제

//Default3.h

#include <iostream>

class Person
{
 int age;
 char* name;
 char* phone_num;
 
public:
 Person(int _age,char *_name, char* _phone_num);
 ~Person();
 void showdata();
// void Delmemory();
};


//Default3.cpp
#include "Default3.h"
#include <iostream>

using std::cout;
using std::endl;

Person::Person(int _age ,char *_name, char* _phone_num)
{
 age=_age;
 name=new char[strlen(_name)+1];
 strcpy(name,_name);

 phone_num=new char[strlen(_phone_num)+1];
 strcpy(phone_num,_phone_num);
}
Person::~Person()
{
 delete[]name;
 delete[]phone_num;
}
void Person::showdata()
{
 cout<<"age"<<' '<<age<<' '<<"name"<<' '<<name<<' '<<"phone_num"<<' '<<phone_num<<endl;
}

//main.cpp
#include "Default3.h"
#include <iostream>

using std::cin;
using std::cout;
using std::endl;


int main()
{
 Person p1(35,"phg","01055947775");
 p1.showdata();
 //p1.Delmemory();
 return 0;
}