생성자 & 소멸자를 이용한 예제
Programming/C++ 2012. 5. 21. 04:11 |#include <iostream>
#include <cstring>
using namespace std;
namespace COMP_POS
{
enum {CLERK, SENIOR, ASSIST, MANAGER};
void Select(int pos)
{
switch(pos)
{
case CLERK:
cout<<"사원"<<endl; break;
case SENIOR:
cout<<"주임"<<endl; break;
case ASSIST:
cout<<"대리"<<endl; break;
case MANAGER:
cout<<"과장"<<endl; break;
}
}
}
class NameCard
{
private:
char * name;
char * companyName;
char * tel;
int position;
public:
NameCard(char* na, char* comp, char* tele, int pos) //생성자
:position(pos)
{
int len1 = strlen(na)+1;
int len2 = strlen(comp)+1;
int len3 = strlen(tele)+1;
name = new char[len1];
strcpy(name, na);
companyName = new char[len2];
strcpy(companyName, comp);
tel = new char[len3];
strcpy(tel, tele);
}
void ShowNameCardInfo(void) const
{
cout<<"이름 :"<<name<<endl;
cout<<"회사 :"<<companyName<<endl;
cout<<"전화번호 :"<<tel<<endl;
cout<<"직급 :"; COMP_POS::Select(position);
cout<<endl;
}
~NameCard() //소멸자
{
delete []name;
delete []companyName;
delete []tel;
}
};
int main(void)
{
NameCard manClerk("Lee", "abc", "010-875-5543", COMP_POS::CLERK);
NameCard manSenior("Jane", "def", "010-232-3439", COMP_POS::SENIOR);
NameCard manAssist("Jeon", "ghi", "010-844-1245", COMP_POS::ASSIST);
manClerk.ShowNameCardInfo();
manSenior.ShowNameCardInfo();
manAssist.ShowNameCardInfo();
return 0;
}
실행결과
'Programming > C++' 카테고리의 다른 글
this 포인터 (0) | 2012.05.21 |
---|---|
객체 배열 & 객체 포인터 배열 (0) | 2012.05.21 |
생성자를 이용한 예제. (0) | 2012.05.21 |
소멸자(Destructor) (0) | 2012.05.20 |
private 생성자 (0) | 2012.05.20 |