Passing parameters

This is a discussion on Passing parameters within the C++ Programming forums, part of the General Programming Boards category; I've created my objects in the main function, when I want to do an action I call the appropriate function ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    87

    Passing parameters

    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!!!!!

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

    Code:
    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

    Code:
    struct ring{
    	int size;
    };
    Code:
    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.

    Code:
    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.

    Code:
    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.

    Code:
    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.

    Code:
    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.

    Code:
    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.

    Code:
    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.
    PuterPaul.co.uk - Portfolio site

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Ah never mind, I got the answer, I was given the idea of putting the pointers into a struct and passing the struct instead. Heres the update.

    Code:
    struct menuStruct{
    	CMenu* mainMenu;
    	CMenu* diffMenu;
    	CMenu* playMenu;
    };
    
    struct gameStruct{
    	CPeg* A;
    	CPeg* B;
    	CPeg* C;
    	CDifficulty* diff;
    };
    PuterPaul.co.uk - Portfolio site

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Question about passing & receiving parameters
    By TCB in forum C Programming
    Replies: 9
    Last Post: 04-11-2006, 06:08 AM
  4. Passing parameters to modal dialogs
    By Halloko in forum Windows Programming
    Replies: 2
    Last Post: 10-11-2005, 07:15 AM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 02:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21