Inheritance downcast query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jayesah@gmail.com

    #1

    Inheritance downcast query

    Hi All,

    I have following code. Anyone can please tell me how
    object of type class A is converted to Class B ?

    #include <iostream.h>
    class A
    {
    public:
    void operator=(int i){a=i;}

    A operator+(A& obj)
    {
    A nn;
    nn.a = a + obj.a;
    cout<<"A: operator + is called\n";
    return nn;

    }

    int a;
    };

    class B : public A
    {
    using A::operator=;

    public:
    B()
    { cout<<"b's simple called\n"; };
    int b;
    };

    int main(void)
    {
    B bb1,bb2;
    bb1=3;
    bb2=4;

    B bb3;
    bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */

    cout << bb3.a;

    return 0;
    }

  • Alf P. Steinbach

    #2
    Re: Inheritance downcast query

    * jayesah@gmail.c om:
    >
    I have following code. Anyone can please tell me how
    object of type class A is converted to Class B ?
    Yes, but first, about the code.

    #include <iostream.h>
    This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
    have this header. Use standard <iostreamplus <ostream(and perhaps
    <istreamif input is required).

    class A
    {
    public:
    void operator=(int i){a=i;}
    >
    A operator+(A& obj)
    That argument should be "A const& obj" unless you're intent on changing
    the actual argument object.

    As it is you can't call "+" with a constant or temporary right hand side.

    {
    A nn;
    nn.a = a + obj.a;
    cout<<"A: operator + is called\n";
    return nn;
    >
    }
    >
    int a;
    };
    >
    class B : public A
    {
    using A::operator=;
    Are you sure you want this assignment operator private?

    public:
    B()
    { cout<<"b's simple called\n"; };
    You mean, "default constructor".

    int b;
    };
    >
    int main(void)
    {
    B bb1,bb2;
    bb1=3;
    bb2=4;
    >
    B bb3;
    bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */
    First, there's no conversion from B to A, and this should not compile
    (have you actually tried the code?).

    Second, if you make the carte blanche "using" statement public it should
    compile, because the auto-generated A::operator=( A const& ) is then
    made available in B and invoked. To avoid that you just need to be a
    bit more specific.

    E.g., in class B, "void operator=( int i ){ A::operator=( i ); }"; or
    better, use a conventional member function instead of an operator.

    cout << bb3.a;
    >
    return 0;
    }
    >

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Naresh

      #3
      Re: Inheritance downcast query


      Alf P. Steinbach wrote:
      * jayesah@gmail.c om:

      I have following code. Anyone can please tell me how
      object of type class A is converted to Class B ?
      >
      Yes, but first, about the code.
      >
      >
      #include <iostream.h>
      >
      This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
      have this header. Use standard <iostreamplus <ostream(and perhaps
      <istreamif input is required).
      >
      >
      class A
      {
      public:
      void operator=(int i){a=i;}

      A operator+(A& obj)
      >
      That argument should be "A const& obj" unless you're intent on changing
      the actual argument object.
      >
      As it is you can't call "+" with a constant or temporary right hand side.
      >
      >
      {
      A nn;
      nn.a = a + obj.a;
      cout<<"A: operator + is called\n";
      return nn;

      }

      int a;
      };

      class B : public A
      {
      using A::operator=;
      >
      Are you sure you want this assignment operator private?
      >
      >
      public:
      B()
      { cout<<"b's simple called\n"; };
      >
      You mean, "default constructor".
      >
      >
      int b;
      };

      int main(void)
      {
      B bb1,bb2;
      bb1=3;
      bb2=4;

      B bb3;
      bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */
      >
      First, there's no conversion from B to A, and this should not compile
      (have you actually tried the code?).
      >
      Second, if you make the carte blanche "using" statement public it should
      compile, because the auto-generated A::operator=( A const& ) is then
      made available in B and invoked. To avoid that you just need to be a
      bit more specific.
      >
      E.g., in class B, "void operator=( int i ){ A::operator=( i ); }"; or
      better, use a conventional member function instead of an operator.
      >
      >
      cout << bb3.a;

      return 0;
      }
      >
      >
      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      its a well compiled code with gcc 3.3.3 on linux.
      It also gives expected output i..e 7

      Comment

      • Naresh

        #4
        Re: Inheritance downcast query


        Alf P. Steinbach wrote:
        * jayesah@gmail.c om:

        I have following code. Anyone can please tell me how
        object of type class A is converted to Class B ?
        >
        Yes, but first, about the code.
        >
        >
        #include <iostream.h>
        >
        This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
        have this header. Use standard <iostreamplus <ostream(and perhaps
        <istreamif input is required).
        >
        >
        class A
        {
        public:
        void operator=(int i){a=i;}

        A operator+(A& obj)
        >
        That argument should be "A const& obj" unless you're intent on changing
        the actual argument object.
        >
        As it is you can't call "+" with a constant or temporary right hand side.
        >
        >
        {
        A nn;
        nn.a = a + obj.a;
        cout<<"A: operator + is called\n";
        return nn;

        }

        int a;
        };

        class B : public A
        {
        using A::operator=;
        >
        Are you sure you want this assignment operator private?
        >
        >
        public:
        B()
        { cout<<"b's simple called\n"; };
        >
        You mean, "default constructor".
        >
        >
        int b;
        };

        int main(void)
        {
        B bb1,bb2;
        bb1=3;
        bb2=4;

        B bb3;
        bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */
        >
        First, there's no conversion from B to A, and this should not compile
        (have you actually tried the code?).
        >
        Second, if you make the carte blanche "using" statement public it should
        compile, because the auto-generated A::operator=( A const& ) is then
        made available in B and invoked. To avoid that you just need to be a
        bit more specific.
        >
        E.g., in class B, "void operator=( int i ){ A::operator=( i ); }"; or
        better, use a conventional member function instead of an operator.
        >
        >
        cout << bb3.a;

        return 0;
        }
        >
        >
        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        its a well compiled code with gcc 3.3.3 on linux.
        It also gives expected output i..e 7

        Comment

        • Alf P. Steinbach

          #5
          Re: Inheritance downcast query

          * Naresh:
          >
          its a well compiled code with gcc 3.3.3 on linux.
          It also gives expected output i..e 7
          It shouldn't compile, so if you actually copied and pasted the code that
          compiled for you, then get a better compiler.

          Btw., please quote what you're responding to but /no more/. Like
          Einstein's "as simple as possible, but no simpler". Also, please don't
          quote signatures, and please don't post the same message two or more times.

          HTH. & TIA.


          --
          A: Because it messes up the order in which people normally read text.
          Q: Why is it such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          Working...