Array of pointers to derived classes of an abstract base class

i have two class:
1
2
3
4
5
6
7
8
9
10
11
12
class Rectangle:public Shape
{
	int height;
	int width;
public:
	void CreateRect(Rectangle*);
	int GetHeight(Rectangle*);
	int GetWidth (Rectangle*);
	float Area();
	int GetDiagonal();
	void printrect(Rectangle*);
};


and:
1
2
3
4
5
6
7
8
9
10
11
class Circle:public Shape
{
	int radius;
public:
	void CreateCirc(Circle*);
	int GetRadius(Circle*);
	float Area() ;
	int GetDiameter();
	friend class Shape;
	void printcircle(Circle*);
};

and a base class:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
[code]class Shape
{
protected:
	int location;
	char * name;
	int x;
	int y;
public:
	void ChangeLocation();
	int GetLocation();
	virtual float Area()=0;
	friend class Circle;
	friend class rectangle;
};


i tried to initiate a string of pointers to the classe like this:
[/code]
1
2
3
4
5
int main()
{
Shape *picture[20];
picture[0]=new Circle;
}


but its not working and im unable to access the Circle's methods. can someone please explain to me how its done?
Is there a reason you can't just use a vector here?

1
2
std::vector<Shape*> pictures;
pictures.push_back(new Circle);


You can also use a vector of smart ptrs so you don't have to worry about freeing the memory.

1
2
std::vector<std::unique_ptr<Shape>> pictures
pictures.push_back(std::unique_ptr<Circle>(new Circle));


Just be aware that unique_ptrs can't be copied. (That's why they're unique).
pointer arithmetic is not the problem here.
note that it does have an array of pointers.

> but its not working
you could be a little more specific

> and im unable to access the Circle's methods
¿and where in the code are you trying to do that?


Also, ¿why so friendly?
Last edited on
Topic archived. No new replies allowed.