Problem Using Object Oriented c++

Started by
12 comments, last by bushimports 15 years, 2 months ago
Well I finally got this thing to compile with the get functions but the program is still not drawing anything, all I am getting is a black screen which is the color I set the background to. I really don't think my get functions are correct because from what I remember reading about functions is that you could only return 1 thing and mine are returning the three parameters that I am asking for. I had my formula wrong inside my for loop for my circle so I changed that and it works properly if I don't use the class.I was already using member variables as pivate.I declared my 3 circle objects in the main file on a global scope,and did the setting and drawing operations in the display function in the main file. I had already cleared the screen and swapped the buffers. Below is the code I have now. If you would like I can post all of the code from each of the 3 files, it is not really a great deal of code because this was just a practice program for me to try to learn OOP with Glut/Open GL.

Here is the complete class info from my circle.hpp file
class Circle                     {   private:      GLfloat x, y, z;           //coordinates of center      float Cx, Cy, Cz;       float radius;      int circlePoints,pnts;      GLfloat r,g,b;             //color         public:                          void set(GLfloat x, GLfloat y, GLfloat  z, float r, int pnts);      void setColor(GLfloat r, GLfloat g, GLfloat b);      float get(GLfloat x, GLfloat y, GLfloat z, float r, int pnts);      float getColor(GLfloat r, GLfloat g, GLfloat b);      void draw();         };

Here is the complete code from my circle.cpp file
 #include "circle.hpp"void Circle::setColor(GLfloat r, GLfloat g, GLfloat b){  glColor3f(r, g, b);   }void Circle::set(GLfloat x, GLfloat y, GLfloat z, float r, int pnts){          Cx = x;      Cy = y;      Cz = z;      radius = r;            circlePoints = pnts;     }float Circle::get(GLfloat x, GLfloat y, GLfloat z, float r, int pnts){      return  x, y, z, r, pnts;      } float Circle::getColor(GLfloat r, GLfloat g, GLfloat b){      return  r, g, b;      }   void Circle::draw(){      float get();   float getColor();    glColor3f(r, g, b);   glPushMatrix();      glEnable(GL_LINE_SMOOTH);   glLineWidth(2);                             glBegin(GL_LINE_LOOP);   for(int i=0; i <= pnts; i++){   float angle = 2 * PI * r / pnts;   glNormal3f(cos(angle), 0, -sin(angle));   angle = 2*PI * i / pnts;   glVertex3f(Cx+r * cos(angle),Cy+r * sin(angle),Cz);   }   glEnd();   glPopMatrix();    }

If you like I can post all or part of the code fom the main file
Thank all of you for all of the help you have given me so far, I really do appreciate it. Much Thanks, Jody Bush

Advertisement
You have several ways of returning the color:

(Notice 1st please that you should use more elaborate names. E.g. you use the member variable r for both the radius and the red color channel. Use e.g. radius, colorR, ... Notice 2nd the use of the const specifier.)

(1) One getter for each element:
float Circle::getColorR() const {   return this->colorR;}float Circle::getColorG() const {   return this->colorG;}float Circle::getColorB() const {   return this->colorB;}


(2) References:
void Circle::getColor(float& red,float& green,float& blue) const {   red = this->colorR;   green = this->colorG;   blue = this->colorB;}

(AFAIK this isn't a getter in the strict sense, but it works fine.)

(3) Compound structure:
struct RGB {   float R,G,B;};RGB Circle::getColor() const {   return this->color;}RGB Circle::color;

I've omitted initializers and similar stuff of RGB here. Usually RGB isn't nested with Circle but available by its own.

(4) Member access:
struct RGB {   float R,G,B;};const RGB& Circle::color() const {   return this->color;}RGB Circle::color;

what is also not a getter in the common sense. Especially this version requires the RGB class itself to be written more carefully than I did for this examples.


Some combinations of the above are possible, too.


Coming to the drawing routine, the belonging accesses were

(1)
void Circle::draw() {      glColor3f(getColorR(), getColorG(), getColorB());   ...}


(2)
void Circle::draw() {      float red, green, blue;   getColor(red, green, blue);   glColor3f(red, green, blue);   ...}


(3)
void Circle::draw() {      RGB rgb = getColor();   glColor3f(rgb.R, rgb.G, rgb.B);   ...}


(4)
void Circle::draw() {      const RGB& rgb = getColor();   glColor3f(rgb.R, rgb.G, rgb.B);   ...}


or directly (and with least effort, but not strict OOP style like)
void Circle::draw() {      glColor3f(this->colorR, this->colorG, this->colorB);   ...}


Quote:Original post by bushimports
Thanks guys. thanks iMalc, you were right I did define PI with an assignment operator,even I should have known better than that.


Yes, you should.

const double PI = 3.141592654;
Hi Guys, Well it turns out that the program was drawing everything. I used a call to gl translatef() to push everything back 15 units and it showed up on the screen. Now I have a different problem. Even though my member variables are all private I was able to translate,scale,and rotate my circle objects with standard OpenGL calls,so my private member variables are not very encapsulated. I can set the parameters with either my class constructor or a setter,no getters are necessary. I have tried declaring my draw function as private but the only way the compiler will accept it is if I call it from the constructor. Do any of you have any clue what I could be doing wrong? Much Thanks, Jody Bush

This topic is closed to new replies.

Advertisement