array of pointers to Shape objects + Vectors

Posted on 2005-05-09
Last Modified: 2010-04-01
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{
      Shape ();
      virtual ~Shape();
      virtual void print()const;         
     int  centreX,centreY;

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


 { }

void Shape::print()const
{  }

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

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

Circle::Circle (int x,int y, int r) : Shape(x,y)
   radius = r;


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

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

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


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;

       cout<< "After addtion of Test, the size of the stack is " << shapez0r.size() << endl;
      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.

Can anyone please helpe me?

Martin =)
Question by:MagicMartin
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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++)
    But I insist that your pointers are pointing to nothing.

    Author Comment

    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.
    LVL 55

    Accepted Solution

         stack <Shape *> shapez0r;

    Author Comment

    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

    Author Comment

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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

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

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

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now