728x90
반응형
#include <iostream>
using namespace std;

class FruitSeller
{
private:
	const int APPLE_PRICE;
	//int APPLE_PRICE;
	int numOfApples;
	int myMoney;

public:
	/* Constructor Function *//*
	FruitSeller()
	{
		APPLE_PRICE = 1000;
		numOfApples = 0;
		myMoney = 0;
	}
	*/
	/*Constructor Function : Overloading(diff parameter)*//*
	FruitSeller(int price, int num, int money)
	{
		APPLE_PRICE = price;
		numOfApples = num;
		myMoney = money;
	}
	*/
	FruitSeller(int price, int num, int money)
		:APPLE_PRICE(price), numOfApples(num), myMoney(money)
	{
		// empty
	}

	/*
	void InitMembers(int price, int num, int money)
	{
		APPLE_PRICE = price;
		numOfApples = num;
		myMoney = money;
	}
	*/
	int SaleApples(int money)
	{
		int num = money / APPLE_PRICE;
		numOfApples -= num;
		myMoney += money;
		return num;
	}

	void ShowSalesResult()
	{
		cout << "remain apple: " << numOfApples << endl;
		cout << "Revenue : " << myMoney << endl;
	}

	// Called Destructor function
	~FruitSeller()
	{
		// delete
	}
};

class FruitBuyer
{
private:
	int myMoney;
	int numOfApples;

public:
	/* Constructor */
	FruitBuyer()
	{
		myMoney = 5000;
		numOfApples = 0;
	}
	/* Constructor Function Overloading different parameters */
	FruitBuyer(int money, int num)
	{
		myMoney = money;
		numOfApples = num;
	}

	void InitMembers(int money)
	{
		myMoney = money;
		numOfApples = 0;
	}
	//void BuyApples(FruitSeller& seller, int money)	// Reference
	void BuyApples(FruitSeller* seller, int money)	// Pointer
	{
		//numOfApples += seller.SaleApples(money); // Reference
		numOfApples += seller->SaleApples(money);	// Pointer
		myMoney -= money;
	}
	void ShowbuyResult()
	{
		cout << "Current balance: " << myMoney << endl;
		cout << "Number of apples: " << numOfApples << endl;
	}

	// Called Destructor function
	~FruitBuyer()
	{
	}
};

int main(void)
{
	/* Typical variable declaration *//*
	FruitSeller seller; // Generate object (un-defined constructor)
	seller.InitMembers(1000, 20, 0); // initialize object using function

	FruitBuyer buyer; // generate object
	buyer.InitMembers(5000); // initialize object using function
	buyer.BuyApples(seller, 2000);	// Puchase fruits from seller
	
	cout << "Current Seller state : " << endl;
	seller.ShowSalesResult();
	cout << "Current Buyer state: " << endl;
	buyer.ShowbuyResult();
	*/

	/* Dynamic memory allocation (Heap) *//*
	FruitSeller* pseller = new FruitSeller; // Generate object and memory allocation
											// (un-defined constructor)
	pseller->InitMembers(1000, 20, 0); // initialize object (Access using arrow operation)
	FruitBuyer* pbuyer = new FruitBuyer; // Generate object and memory allocation
	pbuyer->InitMembers(5000);
	pbuyer->BuyApples(pseller, 2000);

	cout << "Current Seller state : " << endl;
	pseller->ShowSalesResult();
	cout << "Current Buyer state: " << endl;
	pbuyer->ShowbuyResult();

	delete pseller;
	delete pbuyer;
	*/

	
	return 0;
}
#include <iostream>
using namespace std;
class AAA
{
public:
	AAA()
	{
		cout << "Empty object" << endl;
	}
	void ShowYourName()
	{
		cout << "I'm class AAA" << endl;
	}
};

class BBB
{
private:
	AAA& ref;
	const int& num;

public:
	BBB(AAA& r, const int& n)
		:ref(r), num(n)
	{
		//empty constructor body
	}
	void ShowYourName()
	{
		ref.ShowYourName();
		cout << "and" << endl;
		cout << "I ref num" << num << endl;
	}
};

int main(void)
{
	AAA obj1;
	BBB obj2(obj1, 20);
	obj2.ShowYourName();

	return 0;
}
728x90
반응형

'Language > C & C++' 카테고리의 다른 글

[C++] this pointer  (0) 2022.10.17
[C++] Object Array  (0) 2022.10.17
[C++]How to create two Class-based object(*, &)  (0) 2022.10.17
[C++] Dynamic Memory Allocation(compare malloc with new)  (0) 2022.10.17
[C++] Related to Bool datatype  (0) 2022.10.17

+ Recent posts