So need my life would be so much easier if my functions could access my objects. My
problem is my objects are in the main function and my functions, which are stored in a
class somewhere cannot access them. I can solve that problem by declaring the objects
globally but I hear that it isn't so good to practice...so is there a way functions can have
access to objects without being declared globally??? My book only shows objects being
declared in the main function. Thanks for any response!
Just tried that out. Thanks because I did not know I could pass an object through a function but my problem is more on the lines of my project not even being able to be built because objects that have not been declared yet...[declared in main]...reside within my member functions and do not even know what they are. Here is a bit of my code...I have 4 objects named p1, p2, p3, and p4 and they are declared in the main function. I will be changing the 4 object into arrays but I still want to know how to get functions to recognize member class variable/objects...
Lines 60 through 80 are where the objects "were not declared"
Lines 144 through 147 are where I declared the objects
/*___________________________________"WHO GUESSED THE BEST???"___________________________________
- There will be five rounds
- The player who guesses closest to the hidden number wins
- If no one has guessed the correct answer within 5 rounds, the player with the highest score wins
- To score high points, a player has to consistently guess very close to the hidden number!
- If the player misses the hidden numnber by 1, then that player will get a hint the next time around as to what the hidden number is
*/
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
usingnamespace std;
class GET_SET_CLASS/** ______________________________ M.Y F.I.R.S.T C.L.A.S.S ______________________________ **/
{
public:
void SetPoints(int x)///set points for having a good guess
{
m_points += x;
if(x==50)
cout<<"This will be a very interesting game IF this goes through!";
}
int GetPoints()///returns a players points
{
return m_points;
}
void SetPlayerName()///sets the players name
{
cout<<"what is your name: ";
getline(cin, m_name);
while(m_name == "")
{
cout<<"Please enter a name: ";
getline(cin, m_name);
}
}
string GetPlayerName()
{
return m_name;
}
int EvaluatePlayersGuess(int x)//Come back to this function after I revamp the objects in this class to be arrays...HOLY SHIT!!!!
{
if(x == m_hiddenNumber)///if the player guesses the number
{
void StatScreen();
return 0;
}
elseif((x == m_hiddenNumber -1) || (x == m_hiddenNumber +1))///if players's guess is just shy of the hidden number by 1
{
if(GetPlayerCurrentlyPlaying() == 1)
p1.SetPoints(25);
elseif(GetPlayerCurrentlyPlaying() == 1)
p2.SetPoints(25);
elseif(GetPlayerCurrentlyPlaying() == 1)
p3.SetPoints(25);
elseif(GetPlayerCurrentlyPlaying() == 1)
p4.SetPoints(25);
}
elseif((x >= m_hiddenNumber - 10) && (x <= m_hiddenNumber + 10) && (x != m_hiddenNumber))///if player's guess is shy by ten
{
if(GetPlayerCurrentlyPlaying() == 1)
p1.SetPoints(10);
elseif(GetPlayerCurrentlyPlaying() == 1)
p2.SetPoints(10);
elseif(GetPlayerCurrentlyPlaying() == 1)
p3.SetPoints(10);
elseif(GetPlayerCurrentlyPlaying() == 1)
p4.SetPoints(10);
}
}
void SetRandomNumber()
{
m_hiddenNumber = rand() % 50 + 1;
}
int GetRandomNumber()
{
return m_hiddenNumber;
}
void SetPlayerCurrentlyPlaying(int x)
{
m_currentPlayer = x;
}
int GetPlayerCurrentlyPlaying()
{
return m_currentPlayer;
}
void Instructions()
{
cout<<"(1.) There will be five rounds\n"
<<"(2.) The player who guesses the hidden number wins\n"
<<"(3.) If game ends w/out number being guessed, player with highest score wins\n"
<<"(4.) To score high points, consistently guess very close to the hidden number\n"
<<"(5.) *** Players receives hints when they guess really close to the number ***\n";
}
void OutputTheNamesOfThePlayers(int x)
{
}
GET_SET_CLASS()
{
m_hiddenNumber = 0;
m_points = 0;
m_currentPlayer = 0;
}
private:
string m_name;///specifies the name of the character
int m_hiddenNumber;///the random number
int m_points;///the points of each player
int m_numOfPlayersToPlay;///specifies how many players the user specifies will play
int m_currentPlayer;///To let the program know that it is currently player 1's turn
///comment for the above variable - if 1: player 1 is currently active.....if 2: player 2 is currently active....and so on.....
};
int main()/** ________________________________________T.H.E M.A.I.N F.U.N.C.T.I.O.N________________________________________ **/
{
GET_SET_CLASS universal;///object to call everything that does not have to do with the players
GET_SET_CLASS players[4];//drop the other 4 objects and make one array of 4 elements
GET_SET_CLASS p1;
GET_SET_CLASS p2;
GET_SET_CLASS p3;
GET_SET_CLASS p4;
The problem is you are trying to work with class objects inside of the class they were created from (which is possible..but not needed).
Each object has it's own set of variables to work with when created so when you call a function from an object it knows which values to refer to automatically (Which is all related to the 'this' pointer...but you can ignore all that for now).