Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 654829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:32:13+00:00 2026-05-13T22:32:13+00:00

I’m working on a graphics application that is using virtual classes fairly extensively. It

  • 0

I’m working on a graphics application that is using virtual classes fairly extensively. It has:

  • A picture class, which is essentially a collection of shapes.

  • A shapes class, which is purely virtual and has a few classes that inherit from it:

    • Circle
    • Polygon
    • Rectangle
  • A Figure shape, which is any graphical figure (also virtual), shape inherits from this.

Essentially, my problem comes down to implementing the picture class, which is basically being used to store a collection of shapes. I’m currently using a Vector to store shapes, however, it’s apparent that this is the wrong decision since Vector instantiates these shapes, which is not good as they are purely virtual.

Below is my current code base (summarized a bit):

class Figure {
public:
...
  virtual ~Figure();
...
};

class Shape: public Figure
{
public:
...
  virtual ~Shape() {}
  virtual Shape* clone() const = 0;
...
};

class Polygon : public Shape
{
public:
...
virtual Shape* clone() const {return new Polygon(*this);}
... 
private:
std::vector<Point> points;

};

class Picture: public Figure {
public:
...
  Picture(Graphics& gd);
  Picture (const Picture&);
  ~Picture();
  void clear();
  void add (const Shape&);
...
private:
std::vector<Shape> shapes;
Graphics* gfx;
};

//Picture implementation:
...
Picture::Picture(Graphics& gd)
{
gfx = &gd;
}


Picture::Picture(const Picture& a)
{
shapes = a.shapes;
}

Picture::~Picture() 
{
clear();
}

void Picture::clear()
{
shapes.clear();
}

void Picture::add (const Shape& shp)
{
Shape* nshp = shp.clone();
shapes.push_back(*nshp);
}
...

The error messages I’m getting are just a bunch of these:

picture.cpp:33: instantiated from
here
/opt/local/bin/../lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/ext/new_allocator.h:105:
error: cannot allocate an object of
abstract type ‘Shape’ shape.h:12:
note: because the following virtual
functions are pure within ‘Shape’:
shape.h:58: note: virtual void
Shape::get(std::istream&) shape.h:31:
note: virtual void
Shape::put(std::ostream&) const
shape.h:36: note: virtual void
Shape::scale(const Point&, double)
shape.h:40: note: virtual void
Shape::translate(double, double)
shape.h:45: note: virtual void
Shape::reflectHorizontally(double)
shape.h:49: note: virtual void
Shape::reflectVertically(double)
shape.h:52: note: virtual
RectangularArea Shape::boundingBox()
const shape.h:21: note: virtual
Shape* Shape::clone() const
shape.h:55: note: virtual void
Shape::draw(Graphics&) const

So what is the ideal way to store these shapes. What kind of collection should I be using to store these things?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:32:14+00:00Added an answer on May 13, 2026 at 10:32 pm

    When you need polymorphism, you need to use either pointers or references. Since containers (or arrays) can’t store references, you have to use pointers.

    Essentially change your picture class’s vector to:

    std::vector<Shape*>
    

    and appropriately modify the other member functions.

    The reason why you can’t/shouldn’t store them as value types is because vector is a homogenous container i.e. it only stores data of one type (and only one type — subclasses are not allowed!). The reason for this is because the vector stores its data in an array, which needs to know the size of the objects it’s storing. If the sizes of these objects are different (which they might be for different shapes) then it can’t store them in an array.

    If you store them as pointers then they all have the same size (sizeof(Shape*)) and also have access to the shape’s vtable, which is what allows polymorphic behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer We just ran into this ourselves. P4 appears to have… May 14, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer Yes you can. The standard warning applies -- regular expressions… May 14, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer First things first, you need a handler on your admin… May 14, 2026 at 10:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.