본문 바로가기

코딩

상점_인벤토리 추가(가상함수, 추상화)

가상 함수, 추상화를 사용해서 바꾸어 봤다.

몇가지 예외처리 및 보관함에서 판매기능까지 추가해봤다.

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

public:
	Item();
	virtual ~Item(); //가상소멸자
	Item(string _name, int _price);
    
	virtual void Print(); //가상함수 Print()
};

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

	Weapon();
	~Weapon();
	Weapon(string _name, int _price, int _att);
    
	void Print() override;
};

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

	Armor();
	~Armor();
	Armor(string _name, int _price, int _def);
    
	void Print() override;
};

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

Item::Item()
{
}

Item::~Item()
{
}

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

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

Weapon::Weapon()
{
}

Weapon::~Weapon()
{
}

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

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

Armor::Armor()
{
}

Armor::~Armor()
{
}

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

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

 

//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]);
		}
		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;
			}

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

'코딩' 카테고리의 다른 글

포물선을 그리며 날아가는 화살쏘기  (0) 2022.12.29
상점_인벤토리(복사생성자 추가)  (0) 2022.11.29
블랙잭 ver2  (0) 2022.11.15
블랙잭  (0) 2022.11.10
빙빙 돌아가는 회전 배열처럼...  (0) 2022.11.08