본문 바로가기
공부

C++ 상점.인벤토리(추상화,가상함수 사용하기)

by MY블로그 2022. 11. 27.
Item.h
#pragma once
class Item
{
public:
	string name;
	int price;
	string info;

public:
	Item();
	virtual ~Item(); //가상소멸자
	Item(string _name,int _price, string _info);

	virtual void Print(); //가상함수 Print()
};

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

	Weapon();
	~Weapon();
	Weapon(string _name, int _price, int _att, string _info);

	void Print() override;
};

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

	Armor();
	~Armor();
	Armor(string _name, int _price, int _def, string _info);

	void Print() override;
};
Item.cpp
#include "stdafx.h"

Item::Item()
{
}

Item::~Item()
{
}

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

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

Weapon::Weapon()
{
}

Weapon::~Weapon()
{
}

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

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

Armor::Armor()
{
}

Armor::~Armor()
{
}

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

void Armor::Print()
{
	cout << "아이템 : " << name << "\t"
		<< "가격 : " << price << "\t"
		<< "방어력 : " << def << "\t"
		<< "설명 : " << info << endl;
}
Shop.h
#pragma once
class Shop
{
public:
	int input;
	int money;
	Item* items[6]; //추상화
	vector<Item*> inven; //인벤토리를 아이템 포인터에 생성

public:
	void GoShop();
};
Shop.cpp
#include "stdafx.h"

void Shop::GoShop()
{
	items[0] = new Item("잡초", 10, "잡초이다");
	items[1] = new Item("낙엽", 10, "떨어진나뭇잎");
	items[2] = new Weapon("나뭇가지", 200, 1, "금방부러질거같다");
	items[3] = new Weapon("나무몽둥이", 1000, 10, "조금튼튼하다");
	items[4] = new Armor("종이방패", 2000, 5, "약하다");
	items[5] = new Armor("나무방패", 3000, 10, "조금튼튼하다");
	
	system("cls");
	cout << "[상점에 입장 하였습니다]" << endl;
	cout << endl;
	restart1:
	cout << "소지금입력 : "; cin >> money;
	if (money < 10000)
	{
		cout << "금액이 너무 적습니다. 10000원 이상은 가져와주세요" << endl;
		cout << endl;
		goto restart1;
	}
	cout << endl;

	while (true)
	{
		restart2:
		system("cls");
		cout << "[1]구매  [2]인벤토리 [3]나가기" << "\t" << "[ " << "현재 소지금 : " << money <<" G"<< " ]" << endl;
		cout << "원하시는 메뉴를 선택해주세요 : "; cin >> input;

		if (input == 1) //구매
		{
			system("cls");
			cout << "초라한 상점에 입장하였습니다!"<<"\t" << "[ " << "현재 소지금 : " << money << " G" << " ]" << endl;
			cout << endl;
			for (int i = 0; i < 6; i++)
			{
				cout<<"["<< i << "]";
				items[i]->Print();
			}
			cout << endl;
			cout << "몇번 제품을 구매하시겠습니까 ? ";
			cin >> input;
			cout << endl;
			cout << "[ " << items[input]->name << " ]" << " 구매 성공"<< "\t";
			cout << items[input]->price << "G 가 차감되었습니다." << endl;
			if (input > 5)
			{
				cout << endl;
				cout << "현재 5번제품까지밖에 없습니다. 다시입력해 주세요." << endl;
				continue;
			}
			money -= items[input]->price; // 소지금액마이너스(아이템의번호가가지고있는 금액만큼)
			inven.push_back(items[input]); // 구매한 아이템 인벤토리에 배열 추가

			Sleep(750);
			cout << "이용해 주셔서 감사합니다." << endl;
			Sleep(1000);
		}
		else if (input == 2) //보관함
		{
			system("cls");
			cout << "별로 가진게 없는 인벤토리" <<"\t" << "[ " << "현재 소지금 : " << money << " G" << " ]" << endl;
			cout << endl;
			int invennum = 0;
			for (vector<Item*>::iterator it = inven.begin(); it != inven.end(); it++)
			{
				cout <<"[" << invennum<<"]";
				(*it)->Print(); invennum++;
			}
			cout << endl;
			cout << "[1]-----  [2]----- [3]나가기"; cin >> input;
			if (input == 3)
			{
				goto restart2;
			}
			
		}
		else //종료
		{
			cout << "안녕히가세요." << endl;
			break;
		}
	}

	for (int i = 0; i < 6; i++)
	{
		delete items[i];
	}
}

'공부' 카테고리의 다른 글

C++ 인벤토리 판매기능 추가  (0) 2022.11.28
C++ 복사생성자 & 팩토리패턴  (0) 2022.11.28
C++ 가상함수 & 재정의  (0) 2022.11.24
C++ 코드작동 확인하는 참고 사이트  (0) 2022.11.24
C++ 생성자 & 소멸자  (0) 2022.11.23

댓글