?
Solved

array of pointers to Shape objects + Vectors

Posted on 2005-05-09
5
Medium Priority
?
267 Views
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{
    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:
    int radius;
};

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

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.

Can anyone please helpe me?

Martin =)
0
Comment
Question by:MagicMartin
  • 3
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
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

by:MagicMartin
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

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

Author Comment

by:MagicMartin
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

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Article by: SunnyDark
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

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.

Join & Ask a Question