Polymorphism and Composition

This is a discussion on Polymorphism and Composition within the C++ Programming forums, part of the General Programming Boards category; Hi all, the code below doesn't do what I would expect and I'm having some problems understanding why. I was ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    Polymorphism and Composition

    Hi all, the code below doesn't do what I would expect and I'm having some problems understanding why. I was expecting to see "Bar method" as output however it gives "Foo method", also if I use a pointer to Foo in Testing class it works as expected.

    Code:
    #include <iostream>
    
    class Foo
    {
      public:
        Foo() {}
        virtual ~Foo() {}
    
        virtual void method() {std::cout << "Foo method\n";}
    };
    
    class Bar: public Foo
    {
      public:
        Bar() {}
       ~Bar() {}
    
        virtual void method() { std::cout << "Bar method\n";}
    };
    
    class Testing
    {
      public:
        Testing() {}
        ~Testing() {}
    
        void setFoo(Foo &f) {f_ = f;}
    
        void start() {f_.method();}
    
      private:
        Foo f_;
    };
    
    
    int
    main()
    {
      Foo f0;
      Bar b0;
    
      Testing t0;
    
      t0.setFoo(b0);
      t0.start();
      return 0;
    }
    Thanks!
    Last edited by sugarfree; 07-20-2010 at 08:17 AM. Reason: Changing tags from quote to code

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by sugarfree View Post
    Hi all, the code below doesn't do what I would expect and I'm having some problems understanding why. I was expecting to see "Bar method" as output however it gives "Foo method", also if I use a pointer to Foo in Testing class it works as expected.

    Code:
    #include <iostream>
    
    class Foo
    {
      public:
        Foo() {}
        virtual ~Foo() {}
    
        virtual void method() {std::cout << "Foo method\n";}
    };
    
    class Bar: public Foo
    {
      public:
        Bar() {}
       ~Bar() {}
    
        virtual void method() { std::cout << "Bar method\n";}
    };
    
    class Testing
    {
      public:
        Testing() {}
        ~Testing() {}
    
        void setFoo(Foo &f) {f_ = f;}
    
        void start() {f_.method();}
    
      private:
        Foo f_;
    };
    
    
    int
    main()
    {
      Foo f0;
      Bar b0;
    
      Testing t0;
    
      t0.setFoo(b0);
      t0.start();
      return 0;
    }
    Thanks!
    Because "f_" is a "Foo", not a "Bar". Change that to a "Foo&" and everything works just fine.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Because f_ is a Foo, you are copying the "Foo" subset of Bar (if such an instance is passed) into the f_ member. Hence it is a Foo, and Foo::method is called.
    Can be solved with a reference as Sebastiani pointed out.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Thanks! I got it now.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21