Hi ...
I have 3 classes : CBase(char *name)->C2DPoint(float x, float y)->C3DPoint(x, y, z coordinates). I have predefined all operators(+, -, *, /, +=, -=, *=, /=, <<, >>, =, copy constructor - all working properly).
In C3DPoint class I have :
int main()
{
C2DPoint a;
C3DPoint b;
cout << b + a; // working because of the method above
cout << a + b; // also working but i have no such method in
//C2DPoint class and the result is object
// of C2DPoint...it should be C3DPoint object
// in fact , it should not work at all I think
}
I hope that I've posted the problem clearly enough ... if it isn't I can attach the files ....
Thanks in advance
So C3DPoint inherits C2DPoint publically, and C2DPoint has a + operator defined? In that case, it's not really that strange. Remember that public inheritance means IS-A, and because of that C3DPoint IS-A C2DPoint. This makes the operator + defined for C2DPoint fit perfectly when you add a C2DPoint and a C3DPoint.
To tell you the truth, I don't really think public inheritance is the right relation between the two classes here. A C3DPoint is not a C2DPoint in my opinion. It only looks very similar. At best you could say it HAS-A C2DPoint, in which case you would use a member to model the relationship.
Moreover, I don't see how you can naturally add a 2D point to a 3D point...
You just move over x and y coordinates .. z coordinate stays the same.
A C3DPoint is not a C2DPoint in my opinion.
C3DPoint just add a z coordinate ... if I create 2D point it is 2D .. if I create 3D point it first create 2D and then add the z coordinate and become 3D....
I attach the source code if you have time to look at it ... I need to know why this happens and how can i prevent that ...
Thanks again
I have to agree with wien: a 3D point is not a 2D point. This example is similar to the circle/ellipse example of bad inheritance. Just because it looks that way mathematically, doesn't mean that it's a good idea to use inheritance in this way.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
Also, aside from the fact that I find it strange having C3DPoint inherit C2DPoint , since these classes will most often be used as values you set yourself up for slicing problems by publically inheriting C2DPoint.
If I have a number of fixed dimensions I would prefer a point (vector) to be a template class with a numerical template parameter that decides the dimension of the point (vector).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.