Scrapbook/개발 및 프로그래밍
생성자(Constructor)
가을이짱짱
2006. 4. 4. 15:49
반응형
#include "stdafx.h"
using std::cout;
using std::endl;
using std::cin;
정보은닉에 위배되지 않고 생성과 동시에 초기화.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;
}
객체의 생성과정은
첫째, 메모리할당
둘째, 생성자 초기화.
반응형