구조체에서 클래스로

~cpp
struct Date
{
	int d, m, y;
};

void init_date(Date *date, int d, int m, int y)
{
	date->d = d;
	date->m = m;
	date->y = y;
}

void add_year(Date *date, int n)
{
	...
}

void add_month(Date *date, int n)
{
	...
}

void add_day(Date *date, int n)
{
	...
}
~cpp
class Date
{
	int d, m, y;
public:
	void init(int dd, int mm, int yy);
	void add_year(int n);
	void add_month(int n);
	void add_day(int n);
};

void Date::init(int dd, int mm, int yy)
{
	d = dd;
	m = mm;
	y = yy;
}

void Date::add_year(int n)
{
	...
}

void Date::add_month(int n)
{
	...
}

void Date::add_day(int n)
{
	...
}

접근 제어

private - 클래스 멤버만 사용 가능
public - 외부에서도 사용 가능

생성자

~cpp
class Date
{
	int d, m, y;
public:
	Date(int dd, int mm, int yy);
	void add_year(int n);
	void add_month(int n);
	void add_day(int n);
};

Date::Date(int dd, int mm, int yy)
{
	d = dd;
	m = mm;
	y = yy;
}
~cpp
class Date
{
	int d, m, y;
public:
	Date();
	Date(int yy);
	Date(int dd, int mm, int yy);
	void add_year(int n);
	void add_month(int n);
	void add_day(int n);
};

Date::Date()
{
	d = m = y = 1;
}

Date::Date(int yy)
{
	d = m = 1;
	y = yy;
}

Date::Date(int dd, int mm, int yy)
{
	d = dd;
	m = mm;
	y = yy;
}
~cpp
class X
{
	const int i;
	Club c;
	Club& pc;
public:
	X(int ii, const string& n, Date d, Club& c) : i(ii), c(n, d), pc(c) {}
}

소멸자

~cpp
class Date
{
	int d, m, y;
public:
	Date();
	Date(int yy);
	Date(int dd, int mm, int yy);
	~Date();
	void add_year(int n);
	void add_month(int n);
	void add_day(int n);
};

Date::~Date()
{
	...
}

static 멤버

- 모든 인스턴스에 의해 공유됨
- 인스턴스 없이 접근 가능
~cpp
class Date
{
	int d, m, y;
	static Date default_date;
public:
	Date();
	static void set_default(int dd, int mm, int yy);
};

Date Date::default_date;

void Date::set_default(int dd, int mm, int yy)
{
	default_date.d = dd;
	default_date.m = mm;
	default_date.y = yy;
}

Date::Date()
{
	d = default_date.d;
	m = default_date.m;
	y = default_date.y;
}

const 멤버 함수

~cpp
class Date
{
	int d, m, y;
public:
	Date();
	Date(int dd, int mm, int yy);
	int year() const;
	int month() const;
	int day() const;
	void add_year(int n);
	void add_month(int n);
	void add_day(int n);
};

int Date::year() const
{
	return y;
}

int Date::month() const
{
	return m;
}

int Date::day() const
{
	return d;
}

this 포인터

~cpp
class Date
{
	int d, m, y;
public:
	void add_year(int n);
	void add_month(int n);
	void add_day(int n);
};

void Date::add_year(int n)
{
	if(this->d == 29 && this->m == 2 && !leapyear(this->y + this->n))
	{
		this->d = 1;
		this->m = 3;
	}
	this->y += this->n;
}

mutable

~cpp
class Date
{
	int d, m, y;
	mutable bool cache_valid;
	mutable string cache;
	void compute_cache_value() const;
public:
	Date();
	string string_rep() const;
};

string Date::string_rep() const
{
	if(cache_valid == false)
	{
		compute_cache_value();
		cache_valid = true;
	}
	return cache;
}
Retrieved from http://wiki.zeropage.org/wiki.php/OurMajorLangIsCAndCPlusPlus/Class
last modified 2021-02-07 05:23:56