Advertisement

How to use C++ Properly

Started by April 12, 2000 06:44 AM
15 comments, last by Kosh 24 years, 6 months ago
hi everyone, I would like to know, I have this little problem and I am not too sure on how to get around it. if I have a class, say Point, defined like this class Point2D{ private: int x, y; public: Point2D(){ x=0; y=0} ~Point2D(){} }; and then I want to extend it, so that I can have a 3D point, now, I want to make a Point3D class yeah, so how would I extend this class to do so ? like this perhaps ? class Point3D: public Point2D{ private: int z; public Point3D(){ x=0; y=0; z=0;} ~Point3D(); } if this is correct, I now have one problem with this. its that x and y are now public, they are not private, so now, functions or methods external to the class have access to x and y, but not z, but I dont want that, so you might say to put private Point2D where I define the point3d class, but then I would get everything in private, but what about public stuff thats in Point2D ? it''s not been made private, so now anything outside the class cannot access the extended Point2D stuff, cause of this rule. my question therefore, at last, is how can I do this, keeping public things public and private things private ? is there a way to do this is this even a problem, if not, explain what should I do in this situation cause what I wanna do is better done another way thanks for your help kosh
if understanding it made and born with time, then time, is understandings greatest enemy.....
Hi,
If the base class has members defined as private then they are only ever accessible from the base class, regardless of the access level specified for the inheritance. So x & y will not be public but also will not be accessible through Point3D.

Use the ''protected'' access specifier for x & y and derive Point3D from Point2D using ''public'', these will then be accessible through a Point2D or Point3D object. If you are not going to derive from Point3D then z can remain private.

ie.

class Point2D{
protected:
int x, y;
public:
Point2D(){ x=0; y=0}
~Point2D(){}
};

class Point3D: public Point2D{
private:
int z;
public
Point3D(){ x=0; y=0; z=0;}
~Point3D();
}

Hope this helps,

Niv

Advertisement
If you are going to inherit from a base class and you wish to keep data hiding. Use protected members they act in the same way as a private member, but the derived class can access them. While maintaing data hiding. So in your 2D point class,

int x, y; would be accesable by your 3D point class, but would not be accesable by any extenal methods or functions.
quote: Original post by Kosh

like this perhaps ?

class Point3D: public Point2D{


I think your confusion comes from the "public Point2D" part. Public inheritance doesn't mean that all your private variables become public, thankfully It just means that your public variables stay public in the derived class, rather than defaulting to private.

Also, if you want maximum efficiency, you should probably use initialisation lists rather than assignments for setting up the x,y,z values in the constructor. Eg:
Point2D:: Point2D : x(0), y(0) {}
You shouldn't need to initialise x and y in Point3D either, as they will take Point2D's constructor I believe.

Edited by - Kylotan on 4/12/00 9:29:16 AM
Hi !!

Well, OOP is great, but I think you shouldn''t really put it to it''s limits.

I mean, create two classes that are independant:
C2DPoint and C3DPoint.

There is really no need to derive a 3D Point from a 2D Point.

That''s my opinion.

Phillip
Phillip Schuster
I really agree with Philip on this one. Oop is so much more then inheritance. I see people use inheritance for way too much things where there are better solutions. I tend to avoid implementation inheritance all together and stick to interface inheritance only.

But i''m sorry. That wasn''t the topic .

On a side note:

can anybody explain initialisation lists to me?

thanks,

Jaap Suter



____________________________
Mmmm, I''ll have to think of one.
____________________________Mmmm, I''ll have to think of one.
Advertisement
Hi Phillip,

I was thinking the same thing. I would probably even make C2DPoint and C3DPoint each derived from a CPoint class, with the coordinates declared in the derived classes (or setting up a coordinate array pointer in the base class, which the derived classes allocate and free, so I could make 4D or 17D points if I wanted, but that''s probably an unneccessary complexity unless you''re doing some abstract advanced physics or something).

Deriving C3DPoint from C2DPoint doesn''t seem like the best idea from the organizational point of view, ''cause it''s kind of like saying a 3D point is a kind of 2D point - which can be explained but it''s not the most natural way of thinking about it.

Brian
aig
I would agree with s9801758 here. Using inheritance in this case is a classic example of how _not_ to use C++. You would in effect just be using C++ just because you could. In terms of low-level (or even mid-level) graphics stuff, I''ve found that hardcore C++ just has no place. You''ll end up wasting huge amounts of time. Why not just use

struct 2d_point {
int x, y;
};

struct 3d_point {
float x, y, z;
};

Its _way_ simpler. You might consider using a vector (x, y, z) class simply because you can do some nice operator overloading for addition/dot-product/etc. But using inheritance is massively overkill for something like this.
Volition, Inc.
I "agree" with s9801758.
I would also like to know what an
init list is =)
Hello Kosh

Well, I don''t know alot about c++ but read this anyway. May help you decide what to do.
One of the problems with new c++ developers is to use inheratence to much, to over use it. Some times it make the solution harder to get to.
So the question is why are you going with inheratence, are you taking functions from one to use with the other. If you have a 2d point with x and y, and a 3d point with x,y,z then for the 3d point you are not using any functions from the 3d point and may be better left apart.
If you want to use inheratence then put x,y in 2d with all the methods you need, and put just the z in the 3d point. Then any of the methods that need z, write them to be virtual so if you are using a 3d point it will know to use the methods for the 3d point class.
Later Ben
Your best bet is to create them seperate. Each class would be easier to maintain that way.

This topic is closed to new replies.

Advertisement