Programming help

Started by
3 comments, last by Log059 18 years, 10 months ago
I want to create a simple array of various types of geometric entities. The purpose is to define something in terms or arcs and lines while holding the data in a single array, rather than an array of lines and an array of arcs for example class arc { ...arc class } class line { ..line class } then in the program entity[0]=arc or line class; entity[1]=arc or line class (can be different type from previous spot in array; etc. these classes are not derived from each other at this time although I would not rule out doing so. Can anyone steer me in the right direction on how to achieve a really simple container of sorts.
Advertisement
maybe something like this:

class Shape{   virtual void Draw() = 0;};class Arc : public Shape{   Draw();}class Line : public Shape{   Draw();}Shape **shapes = new Shapes[2];shapes[0] = new Line();shapes[1] = new Arc();
will that work?

each class has different members
ex

class line
{

private:
float x1, x2, y1, y2;

...
member functions that deal with these private members for line
}

class arc{

private:
float centerx, centery, theta1, theta2 , radius;

...
member functions that deal with these private members for line
}
Yup, it should work as long as all of the derived classes implement teh virtual Draw function. When you call draw on a shape, it will actually call the derived class's draw function, which should do what you want, regardless of the different member variables.
say i have a function:

float ArcAngle()
{
return theta1-theta2;
}

in my arc but this function would not be needed in the line class

would I need a virtual function in my base class:

virtual float ArcAngle()=0;

Thanks for your help

This topic is closed to new replies.

Advertisement