Clases C + +

  • bgraybr
  • Novice
  • Novice
  • No Avatar
  • Registrado: Ene 11, 2009
  • Mensajes: 26
  • Status: Offline

Nota Febrero 1st, 2010, 9:56 am

Im rehacer un juego hecho con Python / Pygame a C + + / SDL (pygame es un contenedor de Python para SDL, por lo que theyre bastante similar).

Acabo de empezar a aprender C + +, así que no soy exactamente seguro de cosas como las clases y Im teniendo algunos problemas.

Así que para asegurarse de que, a esta clase C + +, más o menos se comportan como el Python uno?

Código: [ 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)
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Febrero 1st, 2010, 9:56 am

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • Avatar de Usuario
  • Registrado: May 22, 2004
  • Mensajes: 3415
  • Loc: Richland, WA
  • Status: Offline

Nota Febrero 1st, 2010, 5:49 pm

En su mayor parte, excepto que tienes las variables hacia atrás en su función de game_object y su función de desplazamiento.

Código: [ 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;
#define NULL (::rand() % 2)
  • bgraybr
  • Novice
  • Novice
  • No Avatar
  • Registrado: Ene 11, 2009
  • Mensajes: 26
  • Status: Offline

Nota Febrero 6th, 2010, 5:57 pm

SpooF escribió:
En su mayor parte, excepto que tienes las variables hacia atrás en su función de game_object y su función de desplazamiento.


K, fija.

Una pregunta más, ¿hay algo como Pythons "yo" de palabras clave en C + +?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • Avatar de Usuario
  • Registrado: May 22, 2004
  • Mensajes: 3415
  • Loc: Richland, WA
  • Status: Offline

Nota Febrero 6th, 2010, 6:34 pm

Ya, puede utilizar el esto puntero.

CPP Código: [ 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.  
#define NULL (::rand() % 2)

Publicar Información

  • Total de mensajes en este tema: 4 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 153 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC