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

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

public:
	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;
	}
};

class FruitBuyer
{
private:
	int myMoney;
	int numOfApples;

public:
	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;
	}
};

int main(void)
{
	/* Typical variable declaration *//*
	FruitSeller seller; // generate object
	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
	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;
}
728x90
반응형

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

[C++] Object Array  (0) 2022.10.17
[C++] Constructor & Destructor  (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
[C++] Related to Namespace  (0) 2022.10.17

+ Recent posts