#include<iostream> #include<cstring> #pragma warning(disable : 4996) using namespace std; class Person{ private: char* name; int age; public: Person(char *myname, int myage){ int len = strlen(myname) + 1; name = new char[len]; strcpy(name, myname); age = myage; } Person(){ name = NULL; age = 0; cout << "called Person()" << endl; } void ShowPersonInfo()const{ cout << "이름 : " << name << endl; cout << "나이 : " << age << endl; } void SetPersonInfo(char* myname, int myage){ name = myname; age = myage; } ~Person(){ delete[] name; cout << "Called Destructor!" << endl; } }; int main(){ Person parr[3]; char namestr[100]; char* strptr; int age; int len; for (int i = 0; i < 3;i++){ cout << "이름 : "; cin >> namestr; cout << "나이 : "; cin >> age; len = strlen(namestr) + 1; strptr = new char[len]; strcpy(strptr, namestr); parr[i].SetPersonInfo(strptr, age); } parr[0].ShowPersonInfo(); parr[1].ShowPersonInfo(); parr[2].ShowPersonInfo(); return 0; }
#include<iostream> #include<cstring> #pragma warning(disable : 4996) using namespace std; class SoSimple{ private: int num; public: SoSimple(int n) : num(n){ cout << "num=" << num << ", "; cout << "address" << this << endl; } void ShowSimpleData(){ cout << num << endl; } SoSimple * GetThisPointer(){ return this; } }; int main(){ SoSimple sim1(100); SoSimple * ptr1 = sim1.GetThisPointer(); //SoSimple * ptr1 = sim1; cout << ptr1 << ", "; ptr1 -> ShowSimpleData(); SoSimple sim2(200); SoSimple * ptr2 = sim2.GetThisPointer(); cout << ptr2 << ", "; ptr2 -> ShowSimpleData(); return 0; }