i am writing a program on 2-D transformation which handles transformations for three objects, namely, point, line and a triangle.
i have created a base class Object and derived three classes - Point, Line, Triangle - from it.
Here i will discuss just the Object and Point class.
The code snippet for that is as under :
// base class Object
class Object
{
public :
virtual void translate() = 0;
// virtual void rotate() = 0;
// virtual void scale() = 0;
// virtual void reflect() = 0;
// virtual void shear() = 0;
virtual void draw(void) const = 0;
virtual void setCoords() = 0;
};
// Point class
class Point : public Object
{
private :
int* coords_;
public :
Point()
{
coords_ = new int[2];
}
~Point()
{
delete coords_;
}
void setCoords (int*& coords)
{
coords_[0] = coords[0];
coords_[1] = coords[1];
}
void translate (const int tx, const int ty)
{
coords_[0] = coords_[0] + tx;
coords_[1] = coords_[1] + ty;
}
void draw(void) const
{
putpixel (coords_[0], coords_[1], GREEN);
}
};
however, the compile complains me when i compile this code saying that i cant create an object of the abstract class Point, which i think is correct because i think i have actually performed overloading rather than overriding..hence the compiler throws me this error..
Now my actual problem is how to overcome this problem. I require to keep the methods like translate(), draw() as purely virtual in the base class Object.
One way i have thought of is (for the base class Object) :
virtual void translate (const int tx,const int ty) = 0;
virtual void setCoords (int*& coords) = 0;
however i am confused because the parameters passed are rather useless here..the pure virtual functions having to be declared in abstract base class, and the functions having no code to work on..
however this seems to be working..as the compiler now accepts this code..
But i would like to welcome any other solution.. :)
So please help me out.