Results 1 to 24 of 24

Thread: Oop [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004

    Oop [RESOLVED]

    Say that i had two classes, A and B. Now B is structurly the same to A and a lot but not all of A's functionality is the same for B.

    This would make B very conducive to be publically inherited from A. The problem is that not all of A's functionality (a few members but none of the operators) should be included in B. Should B be pubically inherited from A?

    Furthermore, if we should inherit, would it be a good strategy to make all members that should be inherited into A declared as private in B?
    Last edited by Darkwraith; Jun 16th, 2004 at 03:32 PM.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Maybe it would be better to make a class C, and then make A and B inherit from that one. The functions that is not going to be the exact same way should oyu make viritual, so you can "over ride" them.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Inheritance is a matter of relation, not of common functionality. What is the relation between A and B? Can you say "B is a A"? Does it make sense? If so, inherit. If you can't, don't.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    I could say that B is-a A, however, some of the functionality of B does not apply to A.

    If you would like, I could explain what A and B are. I just generalized it so that I could just cut to the chase.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  5. #5
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by Darkwraith
    If you would like, I could explain what A and B are.
    Go for it!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    These are both components for a physics engine that I am creating (one part is actually posted, by the way.)

    A actually represents a Point class and B represents a Vector class. Now in the world of math, a vector is a point that has magnitude and direction, so conceptually, a "Point isa Vector." Furthermore, representation of a Point and a Vector are the same, and therefore, structurally, a "Point isa Vector." Lastly, in terms of mathematical operations, the operators of a Point and Vector are the same, so the basic functionality would indicate that a "Point isa Vector." If this was all there was to it, it would be really easy.

    What starts getting sticky is the higher mathematical operations that we do. For example, we cannot find the distance between two vectors, but we could find the distance between two points. We cannot do matrix multiplication with a Point. Etc.

    So what I end up having is a Vector class that should publically inherit Point by convention, but it should not include all the functionality of the base class, Point. What would be a good method to do this?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Vector has, interesting enough, two meanings when it comes to implementing it.

    Most of the time it's one set of coordinates, representing either a point or a direction+magnitude.
    Sometimes it's the strict mathematical view of both together.

    Now, in the first case you can say "Vector is a Point". That's not important, though, as there usually is no point class in that model.
    In the second case, however, you can't say "Vector is a Point". You can say "Vector contains a Point" or "Vector has a Point" and give the vector class a member of type point.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Right now, I am using the first model.

    The problem is that there is a need for a Point class because a Point specificially needs to be returned. There is functionality of the Point class that should not be included in the Vector, so I can't make them the same.

    If I say "Vector hasa Point" (thereby making Vector a container class for Point,) I am now defeating one of the purposes of inheriting Vector from Point, to not repeat code.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The vector class in the Direct3D utilities has all operations that apply to a point...

    You see, under the first model, the definition of vector you gave doesn't apply:
    Now in the world of math, a vector is a point that has magnitude and direction
    That's two pairs (or triples) of coordinates. One for the point, for for magnitude+direction.
    Under the first model, however, the vector has only one tuple. Thus it cannot fulfill the definition you gave.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Ah, but this is for console and OpenGL applications, not DirectX. Basically, the core components (Matrix, Vector, and Point) should act as strict mathematical components. Later, a layer of OpenGL components will be built on top of them as optimizations specifically geared to OpenGL applications.

    The operators are all the same but the methods are not.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The same applies for OpenGL.

    Besides, 3d programming vectors usually have 4 dimensions. XYZW. The W indicating whether it's a point or a direction. Which also ensures correct application of translation matrices for instance.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    I thought that the W was for scalability among other things.

    The components are not restricted to any number of dimensions. Right now, I am dealing with the strict mathematical behavior of these components.

    Sorry, I forgot to say this. A vector could be defined in two ways. (1) A vector is a point that has magnitude and direction. (2) A vector is a 1 x n matrix. Therefore, all of the operations that a Point could do, a Vector could do, but all the operations that a Vector could do, a Point cannot do.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    In that case, inherit Point privately from Vector and use the using directive to only make those operations visible you want.
    Private inheritance means "is implemented in terms of", and to say that the point is implemented in terms of the vector would be appropriate in your situation.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    So you mean:

    class Point;
    class Vector: virtual private Point;

    ?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Nope, I mean
    class Vector;
    class Point : private Vector;
    Since the Point is, after all, a specialization of the Vector in this scheme.

    Where did the virtual come from, anyway?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Sorry if my inheritance is a little off. I am trying to learn object oriented design on the fly . I thought that base classes need to be inherited virtually. Also, how is Point a specialization of Vector?

    A vector could also be defined by two points (however, it could be also defined by one point if we assume that the second is (0, 0, ...) ) If we define it where Point inherits from Vector, we lose this.

    Sorry, I missed a chunk of one of your posts (did not come up on my browser ):

    You see, under the first model, the definition of vector you gave doesn't apply:

    quote:
    --------------------------------------------------------------------------------
    Now in the world of math, a vector is a point that has magnitude and direction
    --------------------------------------------------------------------------------


    That's two pairs (or triples) of coordinates. One for the point, for for magnitude+direction.
    Under the first model, however, the vector has only one tuple. Thus it cannot fulfill the definition you gave.
    Wouldn't the definition be fulfilled if the tuple was the component representation of the vector?

    I see the data structures of the two classes as:

    class Point {
    float* coordinate;
    unsigned dimension;
    };

    class Vector {
    float* component;
    unsigned dimension;
    };
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Since when does a Point have a dimension?

    We're still dealing with the fundamental problem of the definition of Vector here. A vector like you describe it has (in a 2d system for brevity) these components:
    Code:
    class Vector
    {
      float x, y;  // Origin
      float cx, cy;  // Direction & Magnitude
    };
    Point has these components:
    Code:
    class Point
    {
      float x, y;  // Position
    };
    However, under the model typical for computer graphics the Vector has these components:
    Code:
    class Vector
    {
      float x, y;  // Position or Direction&Magnitude
      float w;  // Scaling (or whatever)
    };
    And a transformation matrix looks like this:
    Code:
    class Matrix
    {
      float f11, f12, f13;
      float f21, f22, f23;
      float f31, f32, f33;
    };
    Ok, I probably don't really need to tell you all this stuff because you already know, but it serves to make my point.
    Now, a scale transformation has a matrix like this:
    Code:
    sx 0 0
    0 sy 0
    0 0  1
    This scales the vector by sx and sy, if they're the same then you have a uniform scale.
    Rotation looks like this (easy in 2d as there's only one axis to rotate around):
    Code:
    cos P  -sin P  0
    sin P   cos P  0
      0      0    0
    with P being the angle to rotate.
    Now it gets interesting, we also need the translation.
    Code:
    0 0 x
    0 0 y
    0 0 1
    The only real reason the vector has three coordinates is that it makes it possible to multiply it with such matrices. But how do we get rid of the additional coordinate?
    Well, if it is 0 to start with, that's not a problem. It will never have any other value gives the three matrix types we have here.
    If it is 1 to start with, it will have a different value, which is why we need to w-normalize the vector after transforming it. That means that we divide all coordinates by w, resulting in w being 1 again and the other coordinates getting proper values too.
    Interestingly enough, the 0 in the w coordinate means that the other components are not affected by a translation. Which only makes sense for directional vectors. Those that have w=1 are points, those that have w=0 are directions.
    That's the way it works in graphics. That's the way 3d hardware expects it, too.

    In this system, a point is a specialization of a vector: it's a vector with w=1.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Ok, so you are saying that as these structures are going down in dimension, they are becoming more specialized?

    The problem I have is the fact that there are relations among Point and Vector that cannot be expressed (or can they?) in the linking of Point and Vector that way.

    I would like to include a constructor in Vector that would need Point:

    Code:
    class Vector {
    public:
    Vector(const Point& begin, const Point& end);
    private:
    float* component;   // component form of Vector
    unsigned dimension;
    };
    How could this be done if Point inherits from Vector?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Ok. I think I got this figured out now.

    Thanks everybody for all your help.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There is no need for the dimension in the vector, the components take care of it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Which components?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The float components.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004


    How is that possible when C-style arrays do not have a length attached to them?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  24. #24
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Huh?
    Oh, the dimension is the count...

    That's something completely different of course. Never mind me then. Except that I don't think a runtime-dimensioned system a good idea. It's considerably slower than a compile-time-dimensioned, harder to implement and rarely needed.

    I mean, how often don't you know at compile time how many dimensions your vector has?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width