728x90
반응형
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>

using namespace std;

class Person
{
private:
	char* name;
	int age;
public:
	// Constructor 1.
	Person(const char* myname, int myage)
	{
		cout << "Called Person()" << endl;
		int len = strlen(myname) + 1;
		name = new char[len];
		strcpy(name, myname);
		age = myage;
	}
	
	// Constructor 2.
	Person()
	{
		name = NULL;
		age = 0;
		cout << "Called Person()" << endl;
	}

	void SetPersonInfo(char* myname, int myage)
	{
		name = myname;
		age = myage;
	}

	void ShowPersonInfo() const
	{
		cout << "Name : " << name << ", ";
		cout << "Age : " << age << endl;
	}

	~Person()
	{
		delete[]name;
		cout << "Called destructor" << endl;
	}
};

int main(void)
{	
	// Using Constructor 1.
	//Person ps("Jake", 20);
	//ps.ShowPersonInfo();

	// Using Constructor 2.
	/*
	Person parr[3];
	char namestr[100];
	char* strptr;
	int age;
	int len;

	for (int i = 0; i < 3; i++)
	{
		cout << "Name: ";
		cin >> namestr;
		cout << "Age: ";
		cin >> age;
		len = strlen(namestr) + 1;
		strptr = new char[len];
		strcpy(strptr, namestr);
		parr[i].SetPersonInfo(strptr, age);
	}

	for (int i = 0; i < 3; i++)
	{
		parr[i].ShowPersonInfo();
	}
	*/
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

class Student
{
private:
	char* name;
	int age;
public:
	Student(char* name, int myage)
	{
		int len = strlen(this->name) + 1;
		name = new char[len];
		strcpy(name, this->name);
		age = myage;
	}
	Student()
	{
		name = NULL;
		age = 0;
		cout << "Excute Constructor" << endl;
	}

	void SetInfo(char* pname, int page)
	{
		name = pname;
		age = page;
	}

	void ShowInfo() const
	{
		cout << "Name : " << name << endl;
		cout << "Age: " << age << endl;
	}

	~Student()
	{
		delete[]name;
		cout << "Excute destructor" << endl;
	}

};

int main(void)
{
	/* Just Object Array *//*
	Student str[3];
	char namestr[100];
	int age;
	char* strptr;

	for (int i = 0; i < 3; i++)
	{
		cout << "Name : ";
		cin >> namestr;
		cout << "Age: ";
		cin >> age;
		strptr = new char[strlen(namestr) + 1];
		strcpy(strptr, namestr);
		str[i].SetInfo(strptr, age);
	}

	for (int i = 0; i < 3; i++)
	{

	}
	*/

	/* Object Pointer Array */
	Student* str[3];
	char namestr[100];
	int age;

	for (int i = 0; i < 3; i++)
	{
		cout << "Name: ";
		cin >> namestr;
		cout << "Age: ";
		cin >> age;
		str[i] = new Student(namestr, age);
	}

	for (int i = 0; i < 3; i++)
	{
		str[i]->ShowInfo();
	}

	for (int i = 0; i < 3; i++)
	{
		delete str[i];
	}

	return 0;
}
728x90
반응형

+ Recent posts