C++为noobie声明类/对象

C++ declaring classes/objects for a noobie

本文关键字:对象 声明 noobie C++      更新时间:2023-10-16

我最近决定尝试学习c++。到目前为止,我的主要障碍是对象/类声明。我在java方面有相当的经验,但c++的工作方式让我感到困惑。有人能给我举一个在一个或多个文件中使用对象(如果可能的话,如下图所示)的例子吗?这是我当前的代码(对象未实现)。

//inclusions
#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//declarations

void keyboard (unsigned char key, int x, int y);
void keyboardUp (unsigned char key, int x, int y);
void mouseLocation(int,int);
int onMouse;
int rightClick;
int a = 0;
int mx;
int my;
float red=1.0, blue=0, green=0;
class Ant
{
   private:
      int x;
      int y;
      int speed;
      Ant() { } // private default constructor
   public:
      Ant(int nx, int ny, int nspeed)
      {
         SetX(nx);
         SetY(ny);
         SetSpeed(nspeed);
      }
      void SetX(int nx)
      {
         x = nx;
      }
      void SetY(int ny)
      {
         y = ny;
      }
      void SetSpeed(int nspeed)
      {
         speed = nspeed;
      }
      int GetX() { return x; }
      int GetY()  { return y; }
      int GetSpeed() { return speed; }
};
//mouse listener methods
void mouseLocation(int x,int y) {
   mx = x;
   my = y;
} 
void mouseClicks(int button, int state, int x, int y) {
   mx = x;
   my = y;
   if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
      onMouse = 1;
   }
   if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
      onMouse = 0;
   }
   if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
      rightClick = 0;
   }
   if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
      rightClick = 1;
   }
}
//keylistner methods
void keyboard (unsigned char key, int x, int y)
{
   if (key == 'a')
   {
      a = 1;
   }
}
void keyboardUp (unsigned char key, int x, int y)
{
   if(key == 'a')
   {
      a = 0;
   }
}

//drawing methods
void line1()
{
   if(a == 1)
   {
      glLineWidth(2.5);
      glBegin(GL_LINES);
      glVertex2i(20,20);
      glVertex2i(100,400);
      glEnd();
   }
}
void line2()
{
   if(onMouse == 1)
   {
      glLineWidth(2.5);
      glBegin(GL_LINES);
      glVertex2i(200,20);
      glVertex2i(100,400);
      glEnd();
   }
}
void rect1()
{
   if(mx >= 50 && my >= 50)
   {
      int x = 100;
      int y = 100;
      int w = 100;
      int h = 100;
      unsigned int rgba = 0xff0000ff; // red, no alpha
      glBegin(GL_QUADS);
      glColor4f(((rgba>>24)&0xff)/255.0f,
                ((rgba>>16)&0xff)/255.0f,
                ((rgba>>8)&0xff)/255.0f,
                (rgba&0xff)/255.0f);
      glVertex3f(x,y,0);
      glVertex3f(x+w,y,0);
      glVertex3f(x+w,y+h,0);
      glVertex3f(x,y+h,0);
      glEnd();
      glColor4f(1, 1, 1, 1);
   }
}
void rect2()
{
   if(rightClick == 1)
   {
      int x = 100;
      int y = 100;
      int w = 100;
      int h = 100;
      unsigned int rgba = 0xff0000ff; // red, no alpha
      glBegin(GL_QUADS);
      glColor4f(((rgba>>24)&0xff)/255.0f,
                ((rgba>>16)&0xff)/255.0f,
                ((rgba>>8)&0xff)/255.0f,
                (rgba&0xff)/255.0f);
      glVertex3f(mx,my,0);
      glVertex3f(mx+w,my,0);
      glVertex3f(mx+w,my+h,0);
      glVertex3f(mx,my+h,0);
      glEnd();
      glColor4f(1, 1, 1, 1);
   }
}
//render drawing methods
void renderScence(void){
   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f(red, green, blue);
   line1();
   line2();
   rect1();
   rect2();
   glFlush();
}
//graphics init method
void init(void)
{
   glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
   glutInitWindowSize (500,500);
   glutInitWindowPosition (100, 100);
   glutCreateWindow ("Testing");
   glClearColor (0, 0, 0, 1.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(0, 500, 0, 500);
}
//main/graphics calls
int main(int argc,char** argv)
{
   glutInit(&argc, argv);
   init();
   glutDisplayFunc(renderScence);
   glutIdleFunc(renderScence);
   glutMouseFunc(mouseClicks);
   glutPassiveMotionFunc(mouseLocation);
   glutKeyboardUpFunc (keyboardUp);
   glutKeyboardFunc (keyboard);
   glutMainLoop();
   return 0;
}

您的类定义是正确的(尽管有点Javaish)。您可以创建并使用Ant类型的对象,如下所示:

Ant a(3, 7, 82);
a.SetSpeed(42);
int speed = a.GetSpeed();