Results 1 to 9 of 9

Thread: I don't know what to name this topic

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163

    I don't know what to name this topic

    class X
    {
    public:
    double A(double x)
    {
    return x*x;
    }
    double B(double x)
    {
    return A(x)/2;
    }
    };

    class Y ublic X
    {
    public:
    double A(double x)
    {
    return x*x*x;
    }
    };
    void main()
    {
    Y y;
    cout<<y.B(3);
    }

    this gives 4.5

    but in java


    public class X
    {
    public:
    double A(double x)
    {
    return x*x;
    }
    double B(double x)
    {
    return A(x)/2;
    }

    }
    public class Y extends X
    {
    public:
    double A(double x)
    {
    return x*x*x;
    }
    }
    public class App
    {
    public static void main(String s[])
    {
    Y y=new Y();
    System.out.println(y.B(3));
    }
    }

    this gives 13.5

    Why

    Then what is the use of interface in java?
    Purushottam

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Horribly wrong forum.

    Z.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Try this in the C++ part:
    Code:
    class X
    {
    public:
    virtual double A(double x)
    {
    return x*x;
    }
    double B(double x)
    {
    return A(x)/2;
    }
    };
    
    class Y : public X
    {
    public:
    virtual double A(double x)
    {
    return x*x*x;
    }
    };
    void main()
    {
    Y y;
    cout<<y.B(3);
    }
    It now should output 13.5 too.
    Methods in Java are virtual by default. In C++ they are not because it would often produce unnecessary overhead. In Java this isn't important as in interpreted (byte-)code virtual functions don't create overhead.

    If you don't know what virtual means look up polymorphism in your favourite C++ tutorial.


    Z: It is the right forum. He's more likely to get an answer here than in the Java forums (why the hell should any Java programmer know about virtual functions or that there is something else unless he/she comes from a C++ background).
    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
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Hehe, I was about to say "virtual" was missing but I guess you beat me at it

    [EDIT]
    Doesnt it just overload the function if there is no virtual and override it when there is? So, unless you say something like Y::A(x)/2 in the base function (and this wont work inline because Y is unknonwn at the time), it will always resolve to using X::A(x)/2!!
    Last edited by MoMad; Sep 30th, 2002 at 02:49 PM.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by CornedBee
    Z: It is the right forum. He's more likely to get an answer here than in the Java forums (why the hell should any Java programmer know about virtual functions or that there is something else unless he/she comes from a C++ background). [/B]
    True, forgive, it was too early in the morning =).

    Z.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Mo: Y:: won't ever work in the base class because it's not guaranteed that it is really a Y (might be a X or even a Z derived from X too). It will resolve to X:: except if it's virtual, then it will resolve to ActualType::
    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.

  7. #7
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    isnt it something like typeof(*this):: or yea something to do with the object and not necesarily the class itself. And the Y:: was just for example. I meant it has to be able to do that kind of forward declaration or something.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    I know it works with virtual function.
    But why it works with java.I think only using interface it should work.
    Purushottam

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, extends also uses only virtual functions.

    But why exactly would better be asked in the Java forum.
    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