Still trying to understand Polymorphism

This is a discussion on Still trying to understand Polymorphism within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class A { protected: int a ; int b; public: A() { a=10; b=100; ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Still trying to understand Polymorphism

    Code:
    #include <iostream>
    using namespace std; 
    class A
    {
        protected:
            int a ;
            int b;
        public: 
            A()
            {
                a=10;
                b=100;
            }
            virtual int multiply()=0 ;
    };
    class B:public A
    {
        public: 
            int multiply(); 
            int Divide() ;
    };
    int B::Divide()
    {
        return b/a ;
    }
    int B::multiply()
    {
        return a*b ;
    }
    void main()
    {
        
       A *act= new B ;
    
          cout << act->multiply() ;
         cout << act->Divide() ;//this didn't work
    
        cout << act  -> B::Divide() ;//also that doesn't work   
    
    }
    Polymorphism basic definition: allows dynamic binding, the function type to change during run time..

    i wanted A to be able to use the divide function from the B class.
    but I get a compiler error saying B is not a member object of a class, something like unambiguous access..
    I don't get it... if act points to the class B or object of class B. should I not be able to use member functions of B?
    Why does it not work and is there a way to solve this?
    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    i wanted A to be able to use the divide function from the B class.
    A doesn't have a divide function.

    Polymorphism means that a call like

    Code:
    act->multiply(x, y);
    may call B::multiply or C::multiply etc, depending on what the dynamic type of *act is.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    so you are saying unless 2 objects from different classes have a specific function i can apply for the 2 objects.. i can use polymorphism..
    What if I need to use polymorphism and also be able to use the other functions in that child class?

    would I have to do this
    Code:
    A *a = new B ; 
    a->multiply(a,b) ;
    B b ; 
    
    b.Divide() ; //assuming that the another Class C would does not need a Divide function
    //so no reason to include in Base class A
    that seems redundant to me..

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Then you'll need to cast the pointer to the type that has the method.

    Code:
    static_cast<B*>(a)->multiply(x, y); //if you are sure that a has to be a B
    if (B* b = dynamic_cast<B*>(a)) { //if a may or may not be a B
        b->multiply(x, y);
    }
    Polymorphism doesn't mean that the code won't be statically typed.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

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