티스토리 뷰

언어/C++

8.13 익명 객체

js0331 2020. 4. 24. 19:14
#include <iostream>

using namespace std;

class A
{
	public:
	void print()
	{
		cout << "Hello" << endl;
	}
};


int main()
{
	A().print();

	return 0;
}

// Hello

 

#include <iostream>

using namespace std;

class A
{
	public:
	int m_value;
	
	A(const int& input)
		: m_value(input)
	{
		cout << "Constructor" << endl;
	}
	
	~A()
	{
		cout << "Destructor" << endl;
	}
	
	void printDouble()
	{
		cout << m_value * 2 << endl;
	}
};


int main()
{
	A a(1);
	a.printDouble();

	
	A(1).printDouble();

	return 0;
}

// Constructor
// 2
// Constructor
// 2
// Destructor
// Destructor

 

#include <iostream>

using namespace std;

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

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


int main()
{
	cout << add(Cents(6), Cents(8)).getCents() << endl;
   	cout << int(6) + int(8) << endl;


	return 0;
}

// 14
// 14
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함