Derived classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfred

    Derived classes

    Hi ti all!
    I'd like o ask some clarification about this code.
    I'd like to undestand why in main function pD->f() return D even if pE
    and pD have the same value.
    Do you know where I can find more information about?

    Thank you very much!!!
    Regards
    Alfred

    _______________ ____________

    #include <cstdlib>
    #include <conio.h>
    #include <iostream>
    #include <windows.h>

    using namespace std;
    class B {
    public:
    void f() { cout << "B" << endl; };
    };

    class D : public B{
    public: void f() { cout << "D" << endl; };
    };

    class E : public D {
    public: void f() { cout << "E" << endl; };
    };

    int main()
    {

    E *pE = new E;
    pE->f();
    cout <<"pE= " << pE << endl;

    D *pD = pE;
    cout <<"pD= " << pD << endl;
    pD->f();

    system ("pause");
    }

    _______________ ____________
  • Zara

    #2
    Re: Derived classes

    On Fri, 19 Sep 2008 00:10:13 -0700 (PDT), Alfred <galiayer@yahoo .com>
    wrote:
    >Hi ti all!
    >I'd like o ask some clarification about this code.
    >I'd like to undestand why in main function pD->f() return D even if pE
    >and pD have the same value.
    >Do you know where I can find more information about?
    >

    You must look for information about *virtual functions* and how to use
    them

    Best regards,

    zara

    Comment

    • anon

      #3
      Re: Derived classes

      Alfred wrote:
      Hi ti all!
      I'd like o ask some clarification about this code.
      I'd like to undestand why in main function pD->f() return D even if pE
      and pD have the same value.
      Because you did this:


      Maybe you wanted to make the method f() virtual?
      Do you know where I can find more information about?
      >
      books, online, etc

      Thank you very much!!!
      Regards
      Alfred
      >
      _______________ ____________
      >
      #include <conio.h>
      #include <windows.h>
      btw what are these two headers for??

      Comment

      Working...