코딩

상점_인벤토리(복사생성자 추가)

찬이2 2022. 11. 29. 04:02

앞서 만들었던 상점_인벤토리에서 복사생성자를 이용한 깊은 복사를 추가했습니다.

//item.h
#pragma once
class Item
{
public:
	string name;
	int price;

public:
	Item();
	//추상화로 생성했다면 가상함수로 소멸
	virtual ~Item();
	Item(string _name, int _price);
    
    //복사생성자
	Item(const Item& src);

	//가상함수 : 자식클래스에서 재정의가 이루어질거라고 예상될 때
	virtual void Print();
	virtual Item* Create(const Item* src);
};

class Weapon : public Item //상속
{
public:
	int att;

	Weapon();
	~Weapon();
	Weapon(string _name, int _price, int _att);
	Weapon(const Weapon& src);

	void Print() override; //재정의
	Item* Create(const Item* src) override;
};

class Armor : public Item //상속
{
public:
	int def;

	Armor();
	~Armor();
	Armor(string _name, int _price, int _def);
	Armor(const Armor& src);
		
	void Print() override;
	Item* Create(const Item* src) override;
};

//========================================================================================
//========================================================================================

//item.cpp
#include "stdafx.h"

Item::Item()
{
}

Item::~Item()
{
}

Item::Item(string _name, int _price) 
	: name(_name), price(_price)
{
}

Item::Item(const Item& src)
	: name(src.name), price(src.price)
{
}

void Item::Print()
{
	cout << "아이템 : " << name << "\t"
		<< "가격 : " << price << endl;
}

Item* Item::Create(const Item* src)
{
	return new Item(*src);
}

Weapon::Weapon()
{
}

Weapon::~Weapon()
{
}

Weapon::Weapon(string _name, int _price, int _att)
	: att(_att), Item(_name, _price) //멤버이니셜라이저 , a(_att)
{
}

Weapon::Weapon(const Weapon& src)
	: att(src.att), Item(src)
{
}

void Weapon::Print()
{
	cout << "아이템 : " << name << "\t"
		<< "가격 : " << price << "\t"
		<< "공격력 : " << att << endl;
}

Item* Weapon::Create(const Item* src)
{
	return new Weapon(*(Weapon*)src);
}

Armor::Armor()
{
}

Armor::~Armor()
{
}

Armor::Armor(string _name, int _price, int _def)
	: def(_def), Item(_name, _price)
{
}

Armor::Armor(const Armor& src)
	: def(src.def), Item(src)
{
}

void Armor::Print()
{
	cout << "아이템 : " << name << "\t"
		<< "가격 : " << price << "\t"
		<< "방어력 : " << def << endl;
}

Item* Armor::Create(const Item* src)
{
	return new Armor(*(Armor*)src);
}

 

//Shop.h
#pragma once
class Shop
{
public:
	int input;
	int count = 0;
	int money = 10000;
	Item* items[6]; //추상화
	//추상화 <-> 구체화

	vector<Item*> inven;

public:
	void GoShop();
};

//======================================================================================
//======================================================================================

//Shop.cpp
#include "stdafx.h"

void Shop::GoShop()
{
	items[0] = new Item("잡템", 100);
	items[1] = new Item("잡템2", 200);
	items[2] = new Weapon("목검", 200, 10);
	items[3] = new Weapon("철검", 300, 20);
	items[4] = new Armor("투구", 100, 10);
	items[5] = new Armor("갑옷", 300, 30);

	while (true)
	{
		cout << "===========================================" << endl;
		cout << "소지금 : " << money << endl;
		cout << "1)구매 / 2)보관함 / 3)나가기" << endl << "선택 : ";
		cin >> input;

		if (input == 1) //구매
		{
			for (int i = 0; i < 6; i++)
			{
				cout << i << ") ";
				items[i]->Print();
			}
			cout << "구매할 물품 번호 : ";
			cin >> input;

			if (input > 5)
			{
				cout << "해당하는 물품이 없습니다." << endl;
				continue;
			}

			money -= items[input]->price;
            
            //얕은 복사 말고 깊은 복사로 변경
			//inven.push_back(items[input]); 
			inven.push_back(items[input]->Create(items[input]));
		}
		else if (input == 2) //보관함
		{
			count = 0;
			for (vector<Item*>::iterator i = inven.begin(); i != inven.end(); i++)
			{
				cout << count++ << ") ";
				(*i)->Print();
			}
			cout << "판매할 아이템 번호 : ";
			cin >> input;

			if (input > inven.size())
			{
				cout << "해당하는 물품이 없습니다." << endl;
				continue;
			}
			else
			{
				cout << "중고니까 반값만 드리겠습니다." << endl;
				money += (inven[input]->price) / 2;
				//delete inven[input];
				inven.erase(inven.begin() + input);
			}
		}
		else //종료
		{
			cout << "상점을 나갔습니다." << endl;
			break;
		}
	}
	
	for (int i = 0; i < 6; i++)
	{	
		delete items[i];
	}
}