I want to make two classes and there are variables that need to be used in both classes. How do I create variables that can be accessible by both of the classes? Thanks! :)
Printable View
I want to make two classes and there are variables that need to be used in both classes. How do I create variables that can be accessible by both of the classes? Thanks! :)
Inheritance? Friend function? Nested class? Friend class? It all depends.
How do the classes use the variables? Another solution is static.
Kuphryn
Thats a pretty broad question, why not tell us what your trying to accomplish with the program, and we'll go from there.
Here are some examples:
Hope that made sense, I think I did it right.Code:
class first{
private:
int a;
protected:
int b;
public:
int c;
};
class second : public first{
private:
// none
protected:
// b is here
public:
// c is here
};
Well, ok, I'm trying to make this "Snake" game. I have a FruitClass and I have a SnakeClass. But there are these two vectors that I need to use in both classes. The vectors are declared in the public part of the Snake Class. How can I use those vectors in the FruitClass? btw, cheesymoo, I tried that it didn't work :(
Oh wait, after I moved the fruit class underneath the snake class and added the public SnakeClass thing, I can compile it now, but the snake does not move O.O
EDIT: It moves now, nevermind ^.^;; Thanks so much everyone!! :D
well are you incrementing the snakes position? Show some code
It moves now, but the vectors don't seem to change in the FruitClass. If you put the public SnakeClass thing into the FruitClass, do the vectors accessed from FruitClass change as they are changed in SnakeClass?
Yup, I just confirmed that by "cout"ing the coordinates of the snake head in the FruitClass, they do not change at all even though the snake is moving. O.O
i dont think so, i think you would have to specify it....im not sure
ya.... I seem like I'm missing something... How do you specify it? ^_^
i'm not entirely sure, can you post the code or attach the cpp file so i can see it and get an idea of what you need to do?
this is my entire game ^_^;;
Code:#include <iostream.h>
#include <stdlib.h>
#include <utility.h>
#include <conio.h>
#include <lvp\vector.h>
const char KB_ARROWUP='i';
const char KB_ARROWLEFT='j';
const char KB_ARROWRIGHT='l';
const char KB_ARROWDOWN='k';
class SnakeClass
{
public:
SnakeClass();
~SnakeClass();
void Initializer();
void EraseSnake();
void DisplaySnake();
void CheckMove();
void MoveSnake();
void SnakeDirection();
void Delay();
bool CheckCrash();
vector<int> SnakeX;
vector<int> SnakeY;
private:
bool bCrash;
char cKeyPressed, cTemp;
int iXerase, iYerase;
};
//Default Constructor
SnakeClass::SnakeClass()
{
SnakeX.resize(4);
SnakeY.resize(4);
bCrash=false;
cKeyPressed=' ';
cTemp=' ';
}
//Deconstructor
SnakeClass::~SnakeClass()
{
}
//Initializes the game
void SnakeClass::Initializer()
{
//Draws the box
for(int iSides=10; iSides<=40; iSides++) // Leftwall 20 Rightwall 60
{
gotoxy(20, iSides);
cout<<"#";
gotoxy(60, iSides);
cout<<"#";
}
for(int iTopBot=21; iTopBot<=59; iTopBot++) // Topwall 10 Botwall 40
{
gotoxy(iTopBot, 10);
cout<<"#";
gotoxy(iTopBot, 40);
cout<<"#";
}
// Initialize snake values
for (int iIndex=0; iIndex<4; iIndex++)
{
SnakeX[iIndex]=iIndex+40;
SnakeY[iIndex]=25;
}
}
//Erase snake as it moves
void SnakeClass::EraseSnake()
{
gotoxy(iXerase, iYerase);
cout << " ";
iXerase=SnakeX[SnakeX.length()-1];
iYerase=SnakeY[SnakeY.length()-1];
}
//Draw a snake on the screen
void SnakeClass::DisplaySnake()
{
//Display Snake
for(int iIndex=0; iIndex<SnakeX.length(); iIndex++)
{
gotoxy(SnakeX[iIndex], SnakeY[iIndex]);
cout << "o";
}
}
void SnakeClass::CheckMove()
{
// Check for player move
if(kbhit())
{
cTemp=getch();
if(cTemp==KB_ARROWUP || cTemp==KB_ARROWLEFT || cTemp==KB_ARROWRIGHT || cTemp==KB_ARROWDOWN)
cKeyPressed=cTemp;
}
}
void SnakeClass::MoveSnake()
{
if (cKeyPressed!=' ')
{
//Move snake body pieces
for(int iIndex=SnakeX.length()-1; iIndex>0; iIndex--)
{
SnakeX[iIndex]=SnakeX[iIndex-1];
SnakeY[iIndex]=SnakeY[iIndex-1];
}
}
}
void SnakeClass::SnakeDirection()
{
//Set continuous direction for snake
if (cKeyPressed!=' ')
{
if(cKeyPressed==KB_ARROWUP)
SnakeY[0]--;
else if(cKeyPressed==KB_ARROWLEFT)
SnakeX[0]--;
else if(cKeyPressed==KB_ARROWRIGHT)
SnakeX[0]++;
else if(cKeyPressed==KB_ARROWDOWN)
SnakeY[0]++;
else;
}
}
//Checks if the snake crashed
bool SnakeClass::CheckCrash()
{
if(SnakeX[0]==20 || SnakeX[0]==60)
bCrash=true;
else if (SnakeY[0]==10 || SnakeY[0]==40)
bCrash=true;
else;
for (int iCounter=1; iCounter<SnakeX.length(); iCounter++)
{
if (SnakeX[0]==SnakeX[iCounter]&&SnakeY[0]==SnakeY[iCounter])
{
bCrash=true;
}
}
return(bCrash);
}
//Slows down the snake
void SnakeClass::Delay()
{
//Delay and erase code
Sleep(200);
}
class FruitClass : public SnakeClass
{
public:
FruitClass();
~FruitClass();
void Initializer();
void GenerateFruit();
void CheckFruit();
int iScore;
private:
int iXFruit, iYFruit;
bool bFruit;
};
//Default Constructor
FruitClass::FruitClass()
{
randomize();
iXFruit=random(39)+21;
iYFruit=random(29)+11;
iScore=0;
bFruit=true;
}
//Deconstructor
FruitClass::~FruitClass()
{
}
//Initializes the game
void FruitClass::Initializer()
{
iXFruit=random(39)+21;
iYFruit=random(29)+11;
bFruit=true;
}
//Generate Fruits
void FruitClass::GenerateFruit()
{
//if there is no fruit, generate one
if(bFruit==false)
{
iXFruit=21+(random(39));
iYFruit=11+(random(29));
bFruit=true;
}
//if there is a fruit, draw it on the screen
else if(bFruit==true)
{
gotoxy(iXFruit, iYFruit);
cout << "*";
}
}
//Check if fruit is where the snake head is
void FruitClass::CheckFruit()
{
gotoxy(1,1);
cout<<SnakeX[0]<<" "<<SnakeY[0]<<endl;
cout<<iXFruit<<" "<<iYFruit;
if(SnakeX[0]== iXFruit && SnakeY[0]==iYFruit)
{
iScore+=10;
SnakeX.resize(SnakeX.length()+1);
SnakeY.resize(SnakeY.length()+1);
bFruit=false;
}
}
int main()
{
bool bCrash;
int iScore;
bool bFruit;
SnakeClass Snake;
FruitClass Fruit;
Snake.Initializer();
Fruit.Initializer();
Snake.DisplaySnake();
// Start of game tick
do
{
Snake.CheckMove();
Fruit.GenerateFruit();
Fruit.CheckFruit();
Snake.EraseSnake();
Snake.MoveSnake();
Snake.DisplaySnake();
Snake.SnakeDirection();
bCrash=Snake.CheckCrash();
Snake.Delay();
gotoxy(20,9);
cout<<"Score: "<<Fruit.iScore;
}
while(!bCrash);
gotoxy(38, 25);
cout << "CRASH!!";
gotoxy(1, 49);
system("PAUSE");
return (0);
}
Why not make the members of each class pointers to a vector, and then have them point to the same vector? Then any changes to the vector will be reflected in both classes.