Results 1 to 7 of 7

Thread: inherited funcitons

  1. #1

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking inherited funcitons

    Ok, having a few inheritence problems.

    I have a class CMatrix;
    It has a void Print() function and a constructor that takes 3 parameters (rows, cols, fill)

    I also have a class CPoint, which uses CMatrix as a base.
    When constructed, it takes size and fill, and calls the CMatrix constructor (1, size, fill)

    No Problem yet, but how can i call a CPoint object to .Print(), and get it to use the CMatrix Print() Function without doing this:

    void CPoint::Print()
    {
    CMatrix::Print();
    }

    Is there any better way?
    Thanks
    sql_lall

  2. #2
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    So, you want to use CMatrix's print() in CPoint's print() function?

    If your going to use inheritance, and print() is common to both classes, you can just use CMatrix's print() function (it carries over to CPoint after all). So, if you wanted to use the print() function in a driver program:

    CPoint declaration:
    Code:
    class CPoint: public CMatrix;
    print() usage in a driver program:
    Code:
    CPoint pt(1,2,3);
    pt.print(); //print() carries over from CMatrix
    Does that help any?
    Last edited by Comreak; Oct 10th, 2003 at 04:10 AM.
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    To clarify: if you don't actually do anything in the CPoint version of print other than calling the base version, simply omit the method completly from CPoint.

    How is a point a matrix btw?
    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 sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    ok...nice

    So, if i just leave out the function entirely from the Point class, then when i call
    myPoint.Print()
    then this will default to the Matrix Print() funciton??

    Oh, and an n-dimensional point can be stored as a 1xn or nx1 matrix. Then you can do tranformations by using matrix multiplication, or do scaling, subtraction etc easily.
    sql_lall

  5. #5
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319

    Re: ok...nice

    Originally posted by sql_lall
    So, if i just leave out the function entirely from the Point class, then when i call
    myPoint.Print()
    then this will default to the Matrix Print() funciton??
    Yes, that's correct. CPoint inherits all of it's qualities from CMatrix, including print().
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  6. #6

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking thanks

    Thanks for that... i'll try it out soon

    Oh, and just one more thing (sorry...)

    I have some funcitons that take const parameters (i.e)
    CMatrix& CMatrix:perator +(const CMatrix& InMat);
    now, I also have some void functions, like:
    void CMatrix::Print();

    The only problem is if i try to call the Print function on one of the const parameters (i.e. i can't call InMat.Print() from before)

    I am guessing this is because the compiler isn't guarenteed that InMat won't change when i call Print(), so the 'const'-ness isn't guarenteed, so there's an error.

    What i am wondering is how to make the Print() funciton to be able to be called from constant Matrices..
    something like:
    const void CMatrix::Print() ???
    Thanks
    sql_lall

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    class CMatrix
    {
    public:
      void Print() const;
    };
    
    void CMatrix::Print() const
    {
    }
    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