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.

Recommended Answers

All 4 Replies

No compiler error when you compile THIS code with Object and Pont declarations only. Of course, you can't create this class Point object because of it does not implement all Object interaface and therefore it is an abstract class too.
Let's start from the class Object redesign:
1. The setCoords() pure virtual function is absolutely senseless one: what coords to set? It has no parameters. Don't forget: if you want to use all kinds of Objects via Object interface (via pointer or reference to Object) you must declare common setCoords virtual function suitable for all Objects.
2. Always add virtual dectructor to an abstract class, for example:

class Object
{
public:
    virtual ~Object() {}
    ...

You can't call derived class destructors if the base class destructor is not virtual.

Just thought I'd throw in that little bit I do know...
In your Point destructor, you're freeing the memory allocated by the coords_array. But what you're actually doing is only deleting the first entry of the Array. All the rest will be stored.
To delete all of it's elements you would need to add [] before it, as displayed below.

~Point()
	{
		delete [] coords_;
	}

If you wouldn't do this you'd be creating memory leaks. Which makes the memory unusable untill the application terminates.

@ArKM :

@ArkM :
okies got your point...

@beyond :
yupp..you are right...it was just that i hadnt checked the code yet...so didnt find out that thing.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.