U E D R , A S I H C RSS

cpp스터디인데이름을못짓겠어요/7월21일

#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;
	}
	void ShowPersonInfo()const{
		cout << "이름 : " << name << endl;
		cout << "나이 : " << age << endl;
	}
	~Person(){
		delete[] name;
		cout << "Called Destructor!" << endl;
	}
};


int main(){
	Person man1("Nam hyo min", 22);
	Person man2("Oh young eun", 21);
	man1.ShowPersonInfo();
	man2.ShowPersonInfo();
	
	return 0;
}

#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;
}

  • auto는 참 좋은 자료형입니다()
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:38
Processing time 0.0128 sec