/*
* 두 클래스에 적절한 생성자와 소멸자를 정의해보자
* 그리고 확인을 위한 main함수를 정의해보자
*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyFriendInfo {
private:
char* m_pName;
int m_Age;
public:
MyFriendInfo(const char* name, int age);
~MyFriendInfo();
void ShowMyFriendInfo() {
cout << "이름 : " << m_pName << endl;
cout << "나이 : " << m_Age << endl;
}
};
MyFriendInfo::MyFriendInfo(const char* name, int age) {
m_pName = new char[strlen(name) + 1];
strcpy(m_pName, name);
m_Age = age;
cout << "constructor" << endl;
}
MyFriendInfo::~MyFriendInfo() {
delete[]m_pName;
cout << "destructor" << endl;
}
class MyFriendDetailInfo : public MyFriendInfo {
private:
char* m_pAddr;
char* m_pPhone;
public:
MyFriendDetailInfo(const char* name, int age, const char* addr, const char* phone)
: MyFriendInfo(name, age) {
m_pAddr = new char[strlen(addr) + 1];
m_pPhone = new char[strlen(phone) + 1];
strcpy(m_pAddr, addr);
strcpy(m_pPhone, phone);
cout << "derived constructor" << endl;
};
~MyFriendDetailInfo() {
delete[]m_pAddr;
delete[]m_pPhone;
cout << "derived destructor" << endl;
};
void ShowMyFriendDetailInfo() {
ShowMyFriendInfo();
cout << "주소 : " << m_pAddr << endl;
cout << "번호 : " << m_pPhone << endl;
}
};
int main(void) {
MyFriendInfo choi("choi", 21);
choi.ShowMyFriendInfo();
MyFriendDetailInfo sohn("sohn", 22, "경기", "1243");
sohn.ShowMyFriendDetailInfo();
MyFriendDetailInfo lee = sohn;
lee.ShowMyFriendDetailInfo();
return 0;
}