Solved

# array of pointers to Shape objects + Vectors

Posted on 2005-05-09
Medium Priority
267 Views
Hello everyone, im going to admit this out front but I'm working through my college textbook on C++ programming. Ive reached a question I cannot do, ive had a go at it with no luck.

Basically All i have to do is:

give the code statements to add an item to the collection and remove an item from the collection.  With this example,  the item is to go to the third element of the array, in all other cases the item goes to the end or top of the collection.

and what I have to work with is an array of 5 pointers to Shape objects, below is the code for the shape class etc..

#include <iostream>
using namespace std;
#include <stack>

class Shape{
public:
Shape(int,int);
Shape ();
virtual ~Shape();
virtual void print()const;
protected:
int  centreX,centreY;
};

////// Shape implementation ////////////////////
Shape::Shape(int x, int y)
{
centreX = x;
centreY = y;
}

Shape::Shape()
{
centreX=0;
centreY=0;
}

Shape::~Shape()
{ }

void Shape::print()const
{  }

////////////// Circle ///////////////////////////////////
class Circle:public Shape{

public:

Circle(int x,int y,int r);
~Circle ();
void print()const;

protected:
};

Circle::Circle (int x,int y, int r) : Shape(x,y)
{
}

Circle::~Circle(void)
{}

void Circle::print(void)const
{
cout << "Circle with radius " << radius << " at " << centreX
<< "," << centreY << endl;
}

/////////////// rectangle //////////////////////////////
class Rectangle:public Shape{
public:
Rectangle( int, int, int, int);
~Rectangle();
void print()const;

protected:
int height;
int width;
};

////////////// rectangle impelemtation ////////////////////
Rectangle::Rectangle(int x,int y,int h,int w)
:Shape(x,y)
{
height=h;
width = w;
}

Rectangle::~Rectangle()
{}

void Rectangle::print()const
{
cout <<"Rectangle is at " << centreX <<','<< centreY
<< " Height:" << height << " width:" << width <<endl;
}

int main()
{
Shape *ShapePtr[5];
stack <*ShapePtr[5]> shapez0r;

cout<< "Initially the stack has size " << shapez0r.size() << endl;

shapez0r.push(*ShapePtr[5]);
cout<< "After addtion of Test, the size of the stack is " << shapez0r.size() << endl;
shapez0r.pop();
cout<< "After popping Test, the size of the stack is " << shapez0r.size() << endl;

return 0;
}

As you can see in main i have the code and cout statements for when adding to the stack, I can make it work if i have a single shape object and push it in, but i need to pass the array.

Martin =)
0
Question by:MagicMartin
• 3
• 2

LVL 55

Expert Comment

ID: 13963933
Why to pass an array of 5 pointers (that are not initialized, BTW)
Simply have to pass pointers 5 times

Shape *ShapePtr[5];
stack <Shape> shapez0r;

cout<< "Initially the stack has size " << shapez0r.size() << endl;

for (i=0; i<5; i++)
shapez0r.push(ShapePtr[i]);

But I insist that your pointers are pointing to nothing.
0

Author Comment

ID: 13963975
I think ive just got to do it for principal. Ive implemented your code but get the following error:

error C2664: 'push' : cannot convert parameter 1 from 'class Shape *' to 'const class Shape &'
Reason: cannot convert from 'class Shape *' to 'const class Shape'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
0

LVL 55

Accepted Solution

Jaime Olivares earned 500 total points
ID: 13963990
Sorry:
stack <Shape *> shapez0r;
0

Author Comment

ID: 13964021
Excellent, got it working however i need to satisfy the part of the question:

the item is to go to the third element of the array
0

Author Comment

ID: 13964191
Question was typed wrong, it should be put into a vector, but I will accept your answer :D
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month15 days, 12 hours left to enroll

#### 850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.