Storing Class Objects

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1596 Views - Last Post: 14 April 2010 - 12:14 PM Rate Topic: -----

#1 BradyT88  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Storing Class Objects

Posted 04 April 2010 - 04:47 PM

I had to create a Rectangle class and for extra credit it said to modify your Rectangle2D class to store multiple rectangles while supporting all the member functions, including the constructors. I chose to use a vector to store the objects but I came up with to separate ways to go about doing it and so I was wondering if you guys could tell which way was better.

The first way is to add a static vector into the class implementation and set up all of my constructors in automatically add the objects into the vector when they are created. I have this all done except for the destructor. I am unsure how to get the destructor to delete a specified object.

The second way is to just create the vector in main and then everytime I create an object, write a line of code to manually add it to the vector.

Thanks for your help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Storing Class Objects

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • Joined: 09-June 09

Re: Storing Class Objects

Posted 04 April 2010 - 04:54 PM

you want to create a vector in main() then you can push new instances of your object in there

this will help clarify
#include <iostream>
#include <vector>

using namespace std;

class foo
{
	int x;
public:
	foo(int num)
		:x(num)
	{}
};

int main()
{
	vector<foo>stuff;
	stuff.push_back(foo(1));
	stuff.push_back(foo(2));
	stuff.push_back(foo(3));

	cin.ignore();
	cin.get();
	return 0;
}


This post has been edited by ImaSexy: 04 April 2010 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 689
  • Joined: 16-February 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:00 PM

I am not quite sure, but from what I interpret the call of the problem requires that you use a static member. Again, not quite sure it.

Also, it seems to me that it is much cleaner in hiding details behind public methods like mutators and accessors.

You can delete the object either from the destructor of the object or a static class method that works directly with the container storing the list of objects. The former requires a way to communicate with the vector about which element to remove.
Was This Post Helpful? 0
  • +
  • -

#4 BradyT88  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Storing Class Objects

Posted 04 April 2010 - 05:00 PM

Thanks for your quick reply. I guess to be more specific, it wasn't declared in the header but it was declared just above the actual implementation, so it works fine and contains all the rectangles like it should. One issue I have had is that I can't actually use the vector within main. It acts as though it doesn't exist.

#include "Rectangle2D.h"
#include <iostream>
using namespace std;

static int numberOfRectangles = 0;
static vector<Rectangle2D*> Collection;

Rectangle2D::Rectangle2D()
{
	x = 0;
	y = 0;
	width = 1;
	height = 1;
	numberOfRectangles++;
	Collection.push_back(this);
	cout << "Object added. Number of Rectangles: " << Rectangle2D::getNumberOfRectangles() << endl;
	cout << "Number of objects in Collection: " << Collection.size() << endl;
}
Rectangle2D::Rectangle2D(double newX, double newY, double newWidth, double newHeight)
{
	x = newX;
	y = newY;
	width = newWidth;
	height = newHeight;
	numberOfRectangles++;
	Collection.push_back(this);
	cout << "Object added. Number of Rectangles: " << Rectangle2D::getNumberOfRectangles() << endl;
	cout << "Number of objects in Collection: " << Collection.size() << endl;
}

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • Joined: 09-June 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:03 PM

its because your declaring your vector in the .cpp file, put the vector declaration in the .h file

This post has been edited by ImaSexy: 04 April 2010 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 689
  • Joined: 16-February 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:04 PM

It looks to me like your Collection vector is not a class member. Your class is dependent on an outside variable which isn't a good idea.

Two things you could do is: change the Collection vector to a static class member, or modify the vector from outside the class.
Was This Post Helpful? 0
  • +
  • -

#7 BradyT88  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Storing Class Objects

Posted 04 April 2010 - 05:05 PM

View PostImaSexy, on 04 April 2010 - 04:03 PM, said:

its because your declaring your vector in the .cpp file, put the vector declaration in the .h file

I did that originally but then it creates 2 separate vectors, one within the class and one in main.
Was This Post Helpful? 0
  • +
  • -

#8 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 689
  • Joined: 16-February 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:08 PM

View PostBradyT88, on 04 April 2010 - 04:05 PM, said:

View PostImaSexy, on 04 April 2010 - 04:03 PM, said:

its because your declaring your vector in the .cpp file, put the vector declaration in the .h file

I did that originally but then it creates 2 separate vectors, one within the class and one in main.


Is it declared something like this:
class ClassName
{
    static vector<Rectangle2D*> Collection;
};


or like this:
static vector<Rectangle2D*> Collection;

class ClassName
{
};


Was This Post Helpful? 0
  • +
  • -

#9 BradyT88  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Storing Class Objects

Posted 04 April 2010 - 05:09 PM

Here's the entire program:
#ifndef RECTANGLE2D_H
#define RECTANGLE2D_H
#include <vector>
using namespace std;

class Rectangle2D
{
public:
	Rectangle2D();
	Rectangle2D(double, double, double, double);
	~Rectangle2D();
	Rectangle2D(Rectangle2D &);
	double getX() const;
	double getY() const;
	double getWidth() const;
	double getHeight() const;
	void setX(double);
	void setY(double);
	void setWidth(double);
	void setHeight(double);
	double getArea();
	double getPerimeter();
	bool contains(double, double);
	bool contains(Rectangle2D &);
	bool overlaps(Rectangle2D &);
	static int getNumberOfRectangles();

private:
	double x, y, width, height;
};
#endif

#include "Rectangle2D.h"
#include <iostream>
using namespace std;

static int numberOfRectangles = 0;
static vector<Rectangle2D*> Collection;

Rectangle2D::Rectangle2D()
{
	x = 0;
	y = 0;
	width = 1;
	height = 1;
	numberOfRectangles++;
	Collection.push_back(this);
	cout << "Object added. Number of Rectangles: " << Rectangle2D::getNumberOfRectangles() << endl;
	cout << "Number of objects in Collection: " << Collection.size() << endl;
}
Rectangle2D::Rectangle2D(double newX, double newY, double newWidth, double newHeight)
{
	x = newX;
	y = newY;
	width = newWidth;
	height = newHeight;
	numberOfRectangles++;
	Collection.push_back(this);
	cout << "Object added. Number of Rectangles: " << Rectangle2D::getNumberOfRectangles() << endl;
	cout << "Number of objects in Collection: " << Collection.size() << endl;
}
double Rectangle2D::getX() const
{
	return x;
}
double Rectangle2D::getY() const
{
	return y;
}
double Rectangle2D::getWidth() const
{
	return width;
}
double Rectangle2D::getHeight() const
{
	return height;
}
void Rectangle2D::setX(double newX)
{
	x = newX;
}
void Rectangle2D::setY(double newY)
{
	y = newY;
}
void Rectangle2D::setWidth(double newWidth)
{
	width = newWidth;
}
void Rectangle2D::setHeight(double newHeight)
{
	height = newHeight;
}
double Rectangle2D::getArea()
{
	return width * height;
}
double Rectangle2D::getPerimeter()
{
	return 2 * (width + height);
}
bool Rectangle2D::contains(double x, double y)
{
	if(x <= this->x + width / 2 && x >= this->x - width / 2)
		if(y <= this->y + height / 2 && y >= this->y - height / 2)
			return true;
	return false;
}
bool Rectangle2D::contains(Rectangle2D &r)
{
	if((this->x + width / 2) >= (r.getX() + r.getWidth() / 2) && (this->x - width / 2) <= (r.getX() - r.getWidth() / 2))
		if((this->y + height / 2) >= (r.getY() + r.getHeight() / 2) && (this->y - height / 2) <= (r.getY() - r.getHeight() / 2))
			return true;
	return false;
}
bool Rectangle2D::overlaps(Rectangle2D &r)
{
	if((this->x + width / 2) <= (r.getX() + r.getWidth() / 2) && (this->x - width / 2) >= (r.getX() - r.getWidth() / 2))
		if((this->y + height / 2) <= (r.getY() + r.getHeight() / 2) && (this->y - height / 2) >= (r.getY() - r.getHeight() / 2))
			return true;
	if((this->x + width / 2) >= (r.getX() + r.getWidth() / 2) && (this->x - width / 2) <= (r.getX() + r.getWidth() / 2))
		return true;
	if((this->x + width / 2) >= (r.getX() - r.getWidth() / 2) && (this->x - width / 2) <= (r.getX() - r.getWidth() / 2))
		return true;
	if((this->y + height / 2) >= (r.getY() + r.getHeight() / 2) && (this->y - height / 2) <= (r.getY() - r.getHeight() / 2))
		return true;
	if((this->y + height / 2) >= (r.getY() - r.getHeight() / 2) && (this->y - height / 2) <= (r.getY() - r.getHeight() / 2))
		return true;
	return false;
}
int Rectangle2D::getNumberOfRectangles()
{
	return numberOfRectangles;
}
Rectangle2D::~Rectangle2D()
{
	numberOfRectangles--;
	Collection.erase(Collection.begin()+3);
	cout << "Object has been deleted. Number of Rectangles remaining: " << Rectangle2D::getNumberOfRectangles() << endl;
	cout << "Number of objects remaining in Collection: " << Collection.size() << endl;


}
Rectangle2D::Rectangle2D(Rectangle2D &r)
{
	x = r.getX();
	y = r.getY();
	width = r.getWidth();
	height = r.getHeight();
	numberOfRectangles ++;
	Collection.push_back(this);
	cout << "Object copied. Number of Rectangles: " << Rectangle2D::getNumberOfRectangles() << endl;
	cout << "Number of objects in Collection: " << Collection.size() << endl;
}

#include <iostream>
#include <vector>
#include "Rectangle2D.h"
using namespace std;

int main()
{
	//Test getNumberOfRectangles fuction
	cout << "Number of rectangles to begin with: " << Rectangle2D::getNumberOfRectangles() << endl << endl;
	//Rectangle 1 called through class
	Rectangle2D *r1 = new Rectangle2D(2, 2, 5.5, 4.9);
	cout << "r1 parameters are: x = " << (*r1).getX() << ", y = " << (*r1).getY() << ", Width = " << (*r1).getWidth() << ", Height = " << (*r1).getHeight() << endl;
	cout << "The perimeter of r1 is: " << (*r1).getPerimeter() << ". The area of r1 is: " << (*r1).getArea() << "." << endl << endl;
	//Rectangle 2 called through vector
	Rectangle2D *r2 = new Rectangle2D(4, 5, 10.5, 3.2);
	cout << "r2 parameters are: x = " << (*r2).getX() << ", y = " << (*r2).getY() << ", Width = " << (*r2).getWidth() << ", Height = " << (*r2).getHeight() << endl;
	cout << "The perimeter of r2 is: " << (*r2).getPerimeter() << ". The area of r2 is: " << (*r2).getArea() << "." << endl << endl;
	//Rectangle 3
	Rectangle2D *r3 = new Rectangle2D(3, 5, 2.3, 5.4);
	cout << "r3 parameters are: x = " << (*r3).getX() << ", y = " << (*r3).getY() << ", Width = " << (*r3).getWidth() << ", Height = " << (*r3).getHeight() << endl;
	cout << "The perimeter of r3 is: " << (*r3).getPerimeter() << ". The area of r3 is: " << (*r3).getArea() << "." << endl << endl;
	//Test contains point function
	cout << "Does r1 contain (3, 3): ";
	if((*r1).contains(3, 3) == true)
		cout << "Yes." << endl << endl;
	else
		cout << "No." << endl << endl;
	//Test contains rectangle function
	cout << "Does r1 contain r2: ";
	if((*r1).contains(*r2) == true)
		cout << "Yes." << endl << endl;
	else
		cout << "No." << endl << endl;
	//Test overlap function
	cout << "Does r1 overlap r3: ";
	if((*r1).overlaps(*r3) == true)
		cout << "Yes." << endl << endl;
	else
		cout << "No." << endl << endl;
	//Rectangle 4
	Rectangle2D *r4 = new Rectangle2D(*r1);//copy constructor
	cout << "r4 added as a copy of r1." << endl;
	cout << "r4 parameters are: x = " << (*r4).getX() << ", y = " << (*r4).getY() << ", Width = " << (*r4).getWidth() << ", Height = " << (*r4).getHeight() << endl;
	cout << "The perimeter of r4 is: " << (*r4).getPerimeter() << ". The area of r4 is: " << (*r4).getArea() << "." << endl << endl;
	//Test setters
	(*r4).setX(4);
	(*r4).setY(4);
	(*r4).setWidth(10);
	(*r4).setHeight(10);
	cout << "r4 has been modified. New parameters are: x = " << (*r4).getX() << ", y = " << (*r4).getY() << ", Width = " << (*r4).getWidth()
		<< ", Height = " << (*r4).getHeight() << endl;
	cout << "The perimeter of r4 is: " << (*r4).getPerimeter() << ". The area of r4 is: " << (*r4).getArea() << "." << endl << endl;
	//Test destructor
	delete r4;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#10 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 689
  • Joined: 16-February 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:12 PM

My question was referring to your following quote:

Quote

I did that originally but then it creates 2 separate vectors, one within the class and one in main.


Sorry for any confusion.
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • Joined: 09-June 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:13 PM

View PostBradyT88, on 04 April 2010 - 11:05 PM, said:

View PostImaSexy, on 04 April 2010 - 04:03 PM, said:

its because your declaring your vector in the .cpp file, put the vector declaration in the .h file

I did that originally but then it creates 2 separate vectors, one within the class and one in main.


I think you have it backwards, if you put it as a global in your .h file it will be global to your Rectangle.cpp and main.cpp

EDIT:: mistake

This post has been edited by ImaSexy: 04 April 2010 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

#12 BradyT88  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Storing Class Objects

Posted 04 April 2010 - 05:16 PM

View Postsparkart, on 04 April 2010 - 04:08 PM, said:

View PostBradyT88, on 04 April 2010 - 04:05 PM, said:

View PostImaSexy, on 04 April 2010 - 04:03 PM, said:

its because your declaring your vector in the .cpp file, put the vector declaration in the .h file

I did that originally but then it creates 2 separate vectors, one within the class and one in main.


Is it declared something like this:
class ClassName
{
    static vector<Rectangle2D*> Collection;
};


or like this:
static vector<Rectangle2D*> Collection;

class ClassName
{
};



I have tried it both of those ways. The first gives me a link error and Collection still isn't recognized in main and the 2nd way gives me two separate collections.

View PostImaSexy, on 04 April 2010 - 04:13 PM, said:

View PostBradyT88, on 04 April 2010 - 11:05 PM, said:

View PostImaSexy, on 04 April 2010 - 04:03 PM, said:

its because your declaring your vector in the .cpp file, put the vector declaration in the .h file

I did that originally but then it creates 2 separate vectors, one within the class and one in main.


I think you have it backwards, if you put it as a global in main it will be global to your Rectangle.cpp and main.cpp


So you want me to put it as a global vector in the main.cpp... will that still allow my constructors to use it as they are?

This post has been edited by BradyT88: 04 April 2010 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • Joined: 09-June 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:25 PM

i meant to declare it in your .h file then it will be global.

declare the vector like this in your .h file
class Rectangle2D;
vector<Rectangle2D*> Collection;

class Rectangle2D;
{

};



Was This Post Helpful? 0
  • +
  • -

#14 BradyT88  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Storing Class Objects

Posted 04 April 2010 - 05:30 PM

I have just done that, but the issue I have is like I said before about the 2 collections. For example, when I cout Collection.size() in my constructors like I have, they display what the are supposed to display, but when I cout Collection.size() in main, it will cout 0, unless I actually use Collection.push_back(r1) for each rectangle in main.

This post has been edited by BradyT88: 04 April 2010 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#15 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 689
  • Joined: 16-February 09

Re: Storing Class Objects

Posted 04 April 2010 - 05:41 PM

View PostBradyT88, on 04 April 2010 - 04:16 PM, said:

I have tried it both of those ways. The first gives me a link error and Collection still isn't recognized in main and the 2nd way gives me two separate collections.


The reason for the link error is because you static vector<Collection*> Collection simply declares the particular member. You need to also define it: vector<Collection*> Rectangle2D::Collection;

On a side note, I do not think it is a good idea to make Collection global, especially if it is being manipulated from within your class.

This post has been edited by sparkart: 04 April 2010 - 05:42 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2