Can someone help me with classes please? :)

This is a discussion on Can someone help me with classes please? :) within the C++ Programming forums, part of the General Programming Boards category; I want to make two classes and there are variables that need to be used in both classes. How do ...

  1. #1
    Back! ^.^ *Michelle*'s Avatar
    Join Date
    Oct 2001
    Posts
    568

    Can someone help me with classes please? :)

    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!
    Michelle (o^.^o)
    *Unique*
    Kaomoji House

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Inheritance? Friend function? Nested class? Friend class? It all depends.
    Last edited by 7stud; 04-05-2003 at 02:29 AM.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    How do the classes use the variables? Another solution is static.

    Kuphryn

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Thats a pretty broad question, why not tell us what your trying to accomplish with the program, and we'll go from there.

  5. #5
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Here are some examples:

    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
    };
    Hope that made sense, I think I did it right.
    If you ever need a hug, just ask.

  6. #6
    Back! ^.^ *Michelle*'s Avatar
    Join Date
    Oct 2001
    Posts
    568
    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
    Michelle (o^.^o)
    *Unique*
    Kaomoji House

  7. #7
    Back! ^.^ *Michelle*'s Avatar
    Join Date
    Oct 2001
    Posts
    568
    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!!
    Last edited by *Michelle*; 04-05-2003 at 04:21 PM.
    Michelle (o^.^o)
    *Unique*
    Kaomoji House

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    well are you incrementing the snakes position? Show some code

  9. #9
    Back! ^.^ *Michelle*'s Avatar
    Join Date
    Oct 2001
    Posts
    568
    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?
    Michelle (o^.^o)
    *Unique*
    Kaomoji House

  10. #10
    Back! ^.^ *Michelle*'s Avatar
    Join Date
    Oct 2001
    Posts
    568
    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
    Michelle (o^.^o)
    *Unique*
    Kaomoji House

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i dont think so, i think you would have to specify it....im not sure

  12. #12
    Back! ^.^ *Michelle*'s Avatar
    Join Date
    Oct 2001
    Posts
    568
    ya.... I seem like I'm missing something... How do you specify it? ^_^
    Michelle (o^.^o)
    *Unique*
    Kaomoji House

  13. #13
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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?

  14. #14
    Back! ^.^ *Michelle*'s Avatar
    Join Date
    Oct 2001
    Posts
    568
    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);
    }
    Michelle (o^.^o)
    *Unique*
    Kaomoji House

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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