Using classes to draw shapes

I’m having trouble setting up my class to draw 4 Rectangles X 6 Rectangles.
This is my Rectangles.hpp

class Rectangles{
public:
    
    int x;
    int y;
    int width;
    int height;
    
    Rectangles();
    Rectangles(int x_in, int y_in, int width_in, int height_in);
    ~Rectangles();
    void setRectangles(int x_in, int y_in, int width_in, int height_in);
    void print(string label);
    
};

This is my Rectangles.cpp

#include "Rectangles.hpp"

Rectangles::Rectangles()
{
    x = 0;
    y = 0;
    width = 0;
    height = 0;
}
void Rectangles::setRectangles(int x_in, int y_in, int width_in, int height_in)
{
    x = x_in;
    y = y_in;
    width = width_in;
    height = height_in;
}
Rectangles::~Rectangles()
{
    
}
void Rectangles::print(string lable)
{
    cout<<"\nRectangle: "<<lable<<endl;
    cout<<"\n x: "<<x;
    cout<<"\n y: "<<y;
    cout<<"\n width: "<<width;
    cout<<"\n height: "<<height;
}

This is my draw()

void ofApp::draw(){

    for (int i = 0; i<25; i++) {
        ofDrawRectangle(i+180, i + 100, 40, 80);
    }
}

I’m confused on how to set up my class in a way that I am able to use the member functions in my draw() to make 4 Rectangles X 6 Rectangles.

Or, if there is an easier way to draw 4 Rectangles X 6 Rectangles please leave a suggestion!

if you want to draw a 4x6 grid of rectangles, you can move your ofDrawRectangle(...) calls in a Rectangles::draw() method :

class Rectangles
{
private:

	int x = 0;
	int y = 0;
	int width = 0;
	int height = 0;
	float gap = 0.f;

public:

	Rectangles();
	~Rectangles();

	void setup(int x, int y, int width, int height, float gap = 0.f);
	void draw();
	// ...
};
#include "rectangles.h"

Rectangles::Rectangles(){}
Rectangles::~Rectangles(){}

void Rectangles::setup(int x, int y, int width, int height, float gap)
{
	this->x = x;
	this->y = y;
	this->width = width;
	this->height = height;
	this->gap = gap;
}

void Rectangles::draw()
{
	for (int ix = 0; ix < 4; ++ix)
	{
		for (int iy = 0; iy < 6; ++iy)
			ofDrawRectangle(x + ix * (width + gap),
							y + iy * (height + gap), width, height);
	}
}

//...

// ofApp.h

Rectangles grid;

// ofApp.cpp

void ofApp::setup(){
	grid.setup(180, 100, 40, 80);
}

void ofApp::draw(){
	grid.draw();
}

the row and column sizes are hardcoded, but you can make them dynamic as well

Hi, there is already a class for rectangles, ofRectangle which already does what you have in your own class as well as a lot more. On top of this you can pass an ofRectangle as the single argument for ofDrawRectangle(...)

// declare an ofRectangle and initialize 
ofRectangle rect1(100, 200, 100, 300);// x, y , width , height

// then in draw() call
ofDrawRectangle(rect1);

ofRectangle is one of my favorite OF classes, it is super useful.