생성자(Constructor)

2006. 4. 4. 15:49Scrapbook/개발 및 프로그래밍

반응형
#include "stdafx.h"
using std::cout;
using std::endl;
using std::cin;

const int SIZE=20;

class Person
{
char name[SIZE];
char phone[SIZE];
int age;
public:
Person(char* _name, char* _phone, int _age);
void ShowData();
};

Person::Person(char* _name, char* _phone, int _age)
{
strcpy(name, _name);
strcpy(phone, _phone);
age=_age;
}

void Person::ShowData()
{
cout<<"name: "<<name<<endl;
cout<<"phone: "<<phone<<endl;
cout<<"age: "<<age<<endl;
}

int main()
{
Person p("KIM", "013-333-5555", 22);
p.ShowData();

return 0;
}

정보은닉에 위배되지 않고 생성과 동시에 초기화.
객체의 생성과정은
첫째, 메모리할당
둘째, 생성자 초기화.
반응형