Archived

This topic is now archived and is closed to further replies.

Parsing parameters

This topic is 5720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I''ve created my objects in the main function, when I want to do an action I call the appropriate function by parsing pointers to these objects between the functions (i.e. some functions will inturn call other functions, which also require the objects). Problem is I have about 10 objects in total, there has to be a better option than this. Cos quite frankly it looks ugly and I''m pretty sure there going to be some performance issues!!!!! Suggestions please

Share on other sites
Perhaps you can have the objects store pointers to other objects and initialize them in the constructor. This way you won''t have to pass them around all the time. It''s a bit difficult to suggest how you can improve your code without knowing a little more about what it does.

(And btw, *passing* parameters is quite different from *parsing* parameters )

Share on other sites
Ok lets try and explain what i''m doing, heres my main method where I create the objects.

  int main(){	vector<int> pStack;	//Create the pegs	CPeg* A = new CPeg(pStack,"A");	CPeg* B = new CPeg(pStack,"B");	CPeg* C = new CPeg(pStack,"C");	//Create the difficulty	CDifficulty* diff = new CDifficulty();	/*Create menu options*/	COption* mainOpts = mainMenu();	COption* diffOpts = diffMenu();	COption* playOpts = playMenu();	//Create Menus	CMenu* mainMenu = new CMenu("MAIN MENU",mainOpts,5);	CMenu* diffMenu = new CMenu("DIFFICULTY MENU",diffOpts,4);	CMenu* playMenu = new CMenu("PLAY MENU",playOpts,7);//	showPegs();	mainMenu->showMenu();	mainSelect(diffMenu,playMenu);	delete A;	delete B;	delete C;	delete diff;	delete mainMenu;	delete diffMenu;	delete[] mainOpts;	delete[] diffOpts;	delete[] playOpts;	return 0;}

I am initially creating three pegs A,B + C , these pegs hold different size rings like a stack. The smallest rings on top and larger ones on the bottom. The goal of the game is to move all the rings from peg A on to peg B in the same order.

Here is how I have defined the Rings and Pegs

  struct ring{	int size;};

  class CPeg{public:	CPeg(vector<int> pStack,const string& itsName):	  pegStack(pStack),name(itsName)	{}	int addRing(int rSize){		if(pegStack.size() > 0){			if(rSize < pegStack.back()){				pegStack.push_back(rSize);				return OK;			}else				return FAILED;		}else{			pegStack.push_back(rSize);			return OK;		}	}	void removeRing(){ pegStack.pop_back(); }	void showStack(){		for(int i = pegStack.size() - 1; i >= 0; i--)			cout << pegStack[i] << endl;	}private:	vector<int> pegStack;	string name;};

I am unsure whether or not that ring definition is needed or not.

I have decided to allow the user to choose between easy, medium and hard. I have defined the difficulty class as follows.

  class CDifficulty{public:	CDifficulty(const char* itsSetting = "",const int nRings = 3):	  setting(itsSetting),numRings(nRings)	{}	CDifficulty(const string& itsSetting,int nRings):	  setting(itsSetting),numRings(nRings)	{}	int setDifficulty(const string itsSetting){		setting = itsSetting;		if(setting == "easy"){			numRings = 3;			return OK;		}else if(setting == "medium"){			numRings = 5;			return OK;		}else if(setting == "hard"){			numRings = 7;			return OK;		}else			return FAILED;	}private:	string setting;	int numRings;};

At the moment I am making the game a DOS application, and have therefore had to define a menu system, whereby a user can choose numbered options by the use of the switch statement. I have defined Menus and Options as being two different classes. They are defined as follows.

  class COption{public:	COption(const char* itsName = "",const int optNumber = 0):	  name(itsName),number(optNumber)	{}	COption(const string& itsName,const int optNumber):	  name(itsName),number(optNumber)	{}	string getName(){ return name;}	int getNumber(){ return number; }private:	int number;	string name;};class CMenu{public:	CMenu(const char* itsName = ""):	  mName(itsName),noOptions(0)	{}	CMenu(const string& itsName,COption* optList,const int numOptions):	  mName(itsName),o_array(optList),noOptions(numOptions)	{}	void showMenu(){		cout << "#### " << mName << " ####" << endl;		for(int i = 0; i<noOptions; i++)			cout << o_array[i].getNumber() << ". " 					<< o_array[i].getName() << endl;		cout << "\nPlease choose an option: ";	}private:	string mName;	int noOptions;	COption* o_array;};

You can see where I create the options in the main method I am calling the appropriate method, i.e. mainMenu() . Which is defined as.

  COption* mainMenu(){	COption* o_array = new COption[5];	COption num1("Choose Difficulty",1);	COption num2("Read rules",2);	COption num3("Play game",3);	COption num4("Leader board",4);	COption num5("Exit",5);	o_array[0] = num1;	o_array[1] = num2;	o_array[2] = num3;	o_array[3] = num4;	o_array[4] = num5;	return o_array;}

This method is basically setting up the menu display by creating a pointer to 5 COption instances. When I create the menu which goes with these options, I pass the name of the menu, the pointer to the options and the number of options in the menu. The difficulty menu and play menu are very similar to the above.

For the selection in the menus I have defined functions such as this.

  void mainSelect(CMenu* diffMenu,CMenu* playMenu){	int choice = 0;	cin >> choice;	switch(choice){	case 1:		system("CLS");		diffMenu->showMenu();		break;	case 2:		cout << "\nNo Biting";		break;	case 3:		playMenu->showMenu();		break;	case 4:		cout << "\nThat would be me!!";		break;	case 5:		exit(0);		break;	default:		cout << "Invalid";	}}

Which is for the main menu display, this is where I realised problems could arise. Because in the difficulty menu there is an option to go back to the main menu. Which would mean the function would have to be defined as.

void mainSelect(CMenu* mainMenu,CMenu* diffMenu,CMenu* playMenu){

And if the user chose to play the game, then I would need to display the pegs by calling the showPegs() function. Which would mean I''d also have to pass the pointers to the pegs making the function be as follows.

void mainSelect(CMenu* mainMenu,CMenu* diffMenu,CMenu* playMenu,CPeg* A,CPeg* B,CPeg* C){

The list goes on, how am I going to change my code to make it better, cos altho this method will probably work, it''s neither attractive nor efficient.