C++ classes

  • bgraybr
  • Novice
  • Novice
  • bgraybr
  • Posts: 26

Post 3+ Months Ago

I'm remaking a game made with Python/Pygame to C++/SDL (pygame is a Python wrapper for SDL, so they're pretty similar).

I just started learning C++, so I'm not exactly sure about stuff like classes and I'm having some trouble.

So just to make sure, will this C++ class more or less behave like the Python one?

Code: [ Select ]
/***************
* C++ class
***************/

class game_object {
    int x, y;
    SDL_Surface* image;
    public:
        game_object(int a, int b, const *char filename) {
            a = x;
            b = y;

            image = load_image(filename);
        }
        void display() {
            //blit image
        }
        void move(int a, int b) {
            if (a < WIDTH && b < HEIGHT) {
                a = x;
                b = y;
            }
        } 
        void change_image(const *char filename) {
            image = load_image(filename);
        }
};

###############
# Python class
###############

class game_object:
    def __init__(self, a, b, filename):
        self.x = a
        self.y = b

        self.image = load_image(filename)
    def display():
        #blit image
    def move(a, b):
        if a < WIDTH and b < HEIGHT:
            self.x = a
            self.y = b
    def change_image(filename):
        image = load_image(filename)
  1. /***************
  2. * C++ class
  3. ***************/
  4. class game_object {
  5.     int x, y;
  6.     SDL_Surface* image;
  7.     public:
  8.         game_object(int a, int b, const *char filename) {
  9.             a = x;
  10.             b = y;
  11.             image = load_image(filename);
  12.         }
  13.         void display() {
  14.             //blit image
  15.         }
  16.         void move(int a, int b) {
  17.             if (a < WIDTH && b < HEIGHT) {
  18.                 a = x;
  19.                 b = y;
  20.             }
  21.         } 
  22.         void change_image(const *char filename) {
  23.             image = load_image(filename);
  24.         }
  25. };
  26. ###############
  27. # Python class
  28. ###############
  29. class game_object:
  30.     def __init__(self, a, b, filename):
  31.         self.x = a
  32.         self.y = b
  33.         self.image = load_image(filename)
  34.     def display():
  35.         #blit image
  36.     def move(a, b):
  37.         if a < WIDTH and b < HEIGHT:
  38.             self.x = a
  39.             self.y = b
  40.     def change_image(filename):
  41.         image = load_image(filename)
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

For the most part except you got your variables backwards in your game_object function and your move function.

Code: [ Select ]
a = x;
b = y;

// You want this:
x = a;
y = b;
  1. a = x;
  2. b = y;
  3. // You want this:
  4. x = a;
  5. y = b;
  • bgraybr
  • Novice
  • Novice
  • bgraybr
  • Posts: 26

Post 3+ Months Ago

SpooF wrote:
For the most part except you got your variables backwards in your game_object function and your move function.


K, fixed.

One more question, is there anything like Python's "self" keyword in C++?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Ya, you can use the this pointer.

CPP Code: [ Select ]
Account::Account (int accNumber, double balance, string customerName, string openDate)
{
   this->mAccNumber  = accNumber;
   this->mCustomerName  = customerName;
   this->mOpenDate      = openDate;
   this->mBalance       = balance;
 
   if (mBalance < 0)
   {
      this->mBalance = 0;
      cout << ERROR_BALANCE << endl;       
   } 
}
 
  1. Account::Account (int accNumber, double balance, string customerName, string openDate)
  2. {
  3.    this->mAccNumber  = accNumber;
  4.    this->mCustomerName  = customerName;
  5.    this->mOpenDate      = openDate;
  6.    this->mBalance       = balance;
  7.  
  8.    if (mBalance < 0)
  9.    {
  10.       this->mBalance = 0;
  11.       cout << ERROR_BALANCE << endl;       
  12.    } 
  13. }
  14.  

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 37 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.