search

/*
Athor Dombrovsky I.V.
Demonstration polimorfizm and exception
*/
#include <iostream>
#include <vector>
using namespace std; 
#define TEXT_COLOR_GREEN system("color 0A"); 
#define STOP_CMD_WINDOWS system("pause"); 

vector<int> food = {54, 23, 112}; // Vector food game!

class CraftBox // Class abstract! My methods cannot be created!
{
public:
	virtual void workBox() = 0; // Virtual method
};

class HandlerOne :public CraftBox // work one thing
{
public: 
	HandlerOne() : t(0) {} // CONSTRUCTOR
	void setT(int t) { this->t = t; }
	void workBox() override // always uses ==>> override 
	{
		cout << "I work with element = " << food.at(t) << endl;
	}
private:
	int t; 
};

class HandlerTwo :public CraftBox // work two thing
{
public:
	HandlerTwo() : t(0) {} // CONSTRUCTOR
	void setT(int t) { this->t = t; }
	void workBox() override // always uses ==>> override 
	{
		cout << "I work with element = " << food.at(t) << endl;
	}
private:
	int t;
};

class HandlerThree :public CraftBox // work two thing
{
public:
	HandlerThree() : t(0) {} // CONSTRUCTOR
	void setT(int t) { this->t = t; }
	void workBox() override // always uses ==>> override 
	{
		cout << "I work with element = " << food.at(t) << endl;
	}
private:
	int t;
};

class Player { // The player who will using virtual methods!
public: 
	void workBox(CraftBox * take)
	{
		take->workBox();
	}
};

int main() { // Function main
	TEXT_COLOR_GREEN // color green

	//-------------------------------------------
	cout << "** food in box ***" << endl;
		for (size_t i = 0; i < food.size(); i++) cout << food[i] << " ";
	cout << "\n** food in box ***" << endl;
	//-------------------------------------------

	HandlerOne workOne;
	workOne.setT(0);
	HandlerTwo workTwo;
	workTwo.setT(1);
	HandlerThree workThree;
	workThree.setT(2);

	Player player; // Demonstration polimorfizm
	player.workBox(&workOne);
	player.workBox(&workTwo);
	player.workBox(&workThree);

	try { // Demonstration exeption
		cout << food.at(15) << endl; // I'm trying to take food from someone else's memory.
	}
	catch(const out_of_range & ex) {
		cerr << ex.what() << endl;
	}

	cout << "Hi! I continue work! Thanks very much!" << endl; // After the error we continue the work of the program!
	cout << "End program!" << endl; 
	STOP_CMD_WINDOWS // stop program
	return 0; 
}

array