CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2004
    Posts
    21

    Inheriting question...

    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 :
    Code:
    C3DPoint::C3DPoint(const C2DPoint &ob) : C2DPoint(ob)
    Here an example of the main() function :
    Code:
    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

  2. #2
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Inheriting question...

    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.
    Insert entertaining phrase here

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Inheriting question...

    Moreover, I don't see how you can naturally add a 2D point to a 3D point...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Feb 2004
    Posts
    21

    Re: Inheriting question...

    Quote Originally Posted by cilu
    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
    Attached Files Attached Files

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Inheriting question...

    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


  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Inheriting question...

    You just move over x and y coordinates .. z coordinate stays the same.
    Why not move the y and z coordinates and keep the x unchanged?

    Anyway, please use zip to compress files, not rar. Zip is a cross-platform format. For instance, I don't have WinRar, I can't extract your files.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Sep 2004
    Posts
    519

    Re: Inheriting question...

    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).

    Code:
    template<std::size_t n>
    struct vector
    {
        // ... methods
        double m_vector[n];
    };
    Last edited by marten_range; April 25th, 2005 at 02:16 AM. Reason: Stupid error

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Inheriting question...

    Minor niggle: I wouldn't call it vector, though.
    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


  9. #9
    Join Date
    Feb 2004
    Posts
    21

    Re: Inheriting question...

    Well,
    thanks for the answers ... i figured out what i needed ...

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

Featured