How to implement my own classes?

Hi,
I’m working with the standard testApp of OF.
I now want to make a class called shape which can create different shapes using OF.
This class will be called within update or draw (I don’t know which is best) to draw new shapes.

Can I do this using just a shape.cpp or do I need shape.h as well?
Then I’ll need to add ‘#include ‘shape.cpp’’ or ‘#include ‘shape.h’’ in testApp.cpp to make them work together?

*edit*
Too clarify… I’ve got two files shape.cpp and shape.h which like as follows:

  
  
// shape.cpp  
#include "shape.h"  
#include "stdio.h"  
  
moduleAmount = 1;  
shapeNumber = 0;  
radius;  
  
class shape{  
  
    public:  
        // Constructor, no arguments are given so initialize using random values.  
        shape(){  
            shapeNumber = int(ofRandom(1, moduleAmount));  
        }  
  
        shape(number){  
            shapeNumber = number;  
        }  
  
        createShape(){  
            switch (shapeNumber) {  
                // Circle  
                case 1:  
                    ofSetCircleResolution(int(ofRandom(1, 100)));  
                    radius = int(ofRandom(1, 100));  
                    ofCircle(ofRandom(1 + radius, 1024 - radius), ofRandom(1 + radius, 768 - radius), radius);  
            }  
        }  
  
};  
  

  
  
// shape.h  
#ifndef Shape  
#define Shape  
  
#include "ofMain.h"  
  
int moduleAmount;  
int shapeNumber;  
int radius;  
  
class shape{  
    public:  
        // Constructor, no arguments are given so initialize using random values.  
        shape::shape();  
  
        shape::shape(int);  
  
        void createShape();  
};  
  
#endif  
  

This gives me the following errors:

  
  
firstTestProject\src\shape.cpp|4|error: expected constructor, destructor, or type conversion before '=' token|  
firstTestProject\src\shape.cpp|5|error: expected constructor, destructor, or type conversion before '=' token|  
firstTestProject\src\shape.cpp|6|error: expected constructor, destructor, or type conversion before ';' token|  
firstTestProject\src\shape.cpp|8|error: redefinition of `class shape'|  
firstTestProject\src\shape.h|10|error: previous definition of `class shape'|  
||=== Build finished: 5 errors, 0 warnings ===|  
  

I’m sure these are quit n00b C++ questions, but I couldn’t find the answer after half a day of searching :confused:

the class definition only have to be in *.h file. your *.cpp syntax ist wrong. instead of “class shape{}” you have to write “shape::” in front of all your member functions, attributes and constructors. and not in the *.h file.

somtething like that:

  
  
  
// shape.cpp  
#include "shape.h"  
#include "stdio.h"  
  
moduleAmount = 1;  
shapeNumber = 0;  
//radius;  
  
        // Constructor, no arguments are given so initialize using random values.  
        shape::shape(){  
            shapeNumber = int(ofRandom(1, moduleAmount));  
        }  
  
        shape::shape(number){  
            shapeNumber = number;  
        }  
  
        void shape::createShape(){  
            switch (shapeNumber) {  
                // Circle  
                case 1:  
                    ofSetCircleResolution(int(ofRandom(1, 100)));  
                    radius = int(ofRandom(1, 100));  
                    ofCircle(ofRandom(1 + radius, 1024 - radius), ofRandom(1 + radius, 768 - radius), radius);  
            }  
        }  
  
  
// shape.h  
#ifndef Shape  
#define Shape  
  
#include "ofMain.h"  
  
int moduleAmount;  
int shapeNumber;  
int radius;  
  
class shape{  
    public:  
        // Constructor, no arguments are given so initialize using random values.  
        shape();  
  
        shape(int);  
  
        void createShape();  
};  
  
#endif  
  

perhaps there are other errors but this was your main mistake.

fxlange is right, however, try to put the variables you use inside your class, and initialize them in the constructor :

  
  
Code:  
  
// shape.cpp  
#include "shape.h"  
#include "stdio.h"  
  
  
//radius;  
  
        // Constructor, no arguments are given so initialize using random values.  
        shape::shape(){  
            moduleAmount = 1;  
            shapeNumber = 0;  
  
            shapeNumber = int(ofRandom(1, moduleAmount));  
        }  
  
        shape::shape(number){  
            moduleAmount = 1;  
            shapeNumber = 0;  
  
            shapeNumber = number;  
        }  
  
        void shape::createShape(){  
            switch (shapeNumber) {  
                // Circle  
                case 1:  
                    ofSetCircleResolution(int(ofRandom(1, 100)));  
                    radius = int(ofRandom(1, 100));  
                    ofCircle(ofRandom(1 + radius, 1024 - radius), ofRandom(1 + radius, 768 - radius), radius);  
            }  
        }  
  
  
// shape.h  
#ifndef Shape  
#define Shape  
  
#include "ofMain.h"  
  
class shape{  
    private:  
        int moduleAmount;  
        int shapeNumber;  
        int radius;  
  
    public:  
        // Constructor, no arguments are given so initialize using random values.  
        shape();  
  
        shape(int number);  
  
        void createShape();  
};  
  
#endif  
  
  

Otherwise, if you only need to use these variables in your class use them as private.