티스토리 뷰

#include <iostream>

using namespace std;

class Cents
{
	private:
	int m_cents;
	
	public:
	Cents(int cents = 0)
	{
		m_cents = cents;
	}
	
	int getCents() const
	{
		return m_cents;
	}
	
	int& getCents()
	{
		return m_cents;
	}
};

void add(const Cents &c1, const Cents &c2, Cents &c_out)
{
	c_out.getCents() = c1.getCents() + c2.getCents();
}

int main()
{
	Cents cents1(6);
	Cents cents2(8);

	Cents sum;
	
	add(cents1, cents2, sum);
	
	cout << sum.getCents() << endl;
	
	return 0;
}

// 14

 

 

Cents operator + (const Cents &c1, const Cents &c2)
{
	return Cents(c1.getCents() + c2.getCents());
}

int main()
{
	Cents cents1(6);
	Cents cents2(8);

// 	Cents sum;
	
// 	add(cents1, cents2, sum);
	
	// cout << add(cents1, cents2).getCents() << endl;
	
	cout << (cents1 + cents2 + Cents(6)).getCents() << endl;
	
	return 0;
}

// 20

 

 

class Cents
{
	private:
	int m_cents;
	
	public:
	Cents(int cents = 0)
	{
		m_cents = cents;
	}
	
	int getCents() const
	{
		return m_cents;
	}
	
	int& getCents()
	{
		return m_cents;
	}
	
	friend Cents operator + (const Cents &c1, const Cents &c2)
	{
		return Cents(c1.getCents() + c2.getCents());
	}
};

 

// 오버로딩 안되는 친구들
// ?:, ::, sizeof, ., .*

 

멤버펑션으로 구현하는 방법.

Cents operator + (const Cents &c2)
	{
		return Cents(this->m_cents + c2.m_cents);
	}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함