25 Replies - 1245 Views - Last Post: 14 April 2010 - 12:14 PM
#1
Storing Class Objects
Posted 04 April 2010 - 04:47 PM
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.
Replies To: Storing Class Objects
#2
Re: Storing Class Objects
Posted 04 April 2010 - 04:54 PM
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
#3
Re: Storing Class Objects
Posted 04 April 2010 - 05:00 PM
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.
#4
Re: Storing Class Objects
Posted 04 April 2010 - 05:00 PM
#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;
}
#5
Re: Storing Class Objects
Posted 04 April 2010 - 05:03 PM
This post has been edited by ImaSexy: 04 April 2010 - 05:03 PM
#6
Re: Storing Class Objects
Posted 04 April 2010 - 05:04 PM
Two things you could do is: change the Collection vector to a static class member, or modify the vector from outside the class.
#7
Re: Storing Class Objects
Posted 04 April 2010 - 05:05 PM
#8
#9
Re: Storing Class Objects
Posted 04 April 2010 - 05:09 PM
#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;
}
#10
Re: Storing Class Objects
Posted 04 April 2010 - 05:12 PM
Quote
Sorry for any confusion.
#11
Re: Storing Class Objects
Posted 04 April 2010 - 05:13 PM
#12
Re: Storing Class Objects
Posted 04 April 2010 - 05:16 PM
sparkart, on 04 April 2010 - 04:08 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.
ImaSexy, on 04 April 2010 - 04:13 PM, said:
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
#13
Re: Storing Class Objects
Posted 04 April 2010 - 05:25 PM
declare the vector like this in your .h file
class Rectangle2D;
vector<Rectangle2D*> Collection;
class Rectangle2D;
{
};
#14
Re: Storing Class Objects
Posted 04 April 2010 - 05:30 PM
This post has been edited by BradyT88: 04 April 2010 - 05:31 PM
#15
Re: Storing Class Objects
Posted 04 April 2010 - 05:41 PM
BradyT88, on 04 April 2010 - 04:16 PM, said:
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
|
|

New Topic/Question
Reply




MultiQuote



|