Thread: question on polymorphism

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Question question on polymorphism

    ok..can anyone tell me more in detail what polymorphism is..all i know so far is that the base class and derive class has the same member functions name declare with virtual it gives it dynamic linkage..and kind of same with pointers....whats so good about ..please give examples..thanks
    nextus, the samurai warrior

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Ok... I will try :)

    So imagine that you have this class

    Code:
    class Base
    {
        int x;
        
    public:
        Base (int xx=0): x(xx){}
        virtual int getNum() {return x;};
        void PrintNum( cout<<x;);
    };
    
    class Derivated
    {
        int newX;
    public:
        Derivated (int xx=1, int y=5): Base(xx){newX=y;}
    
        int getNum() {return newX;}
        void PrintNum() {cout<<NewX;}
    };
    
    void main()
    {
        Base num(5);
        Derivated num2(5,10);
        Base *ptr;
    
        int y=num.getNum();   //just call the getNum from the base
        cout<<y;  //y=5
        num.PrintNum();  //print 5
    
        int w=num.getNum();  //Now it will call the getNum from the derivated classe because this function is virtual
        cout<<w;  //w=10
      
        //Now came the nice part
        ptr=new Derivated(5,20);
    
        int z=ptr.getNum(); //it will call the getNum from the derivated
        cout<<z;  //z=20
        ptr.printNum();  //it will print 5
    }
    i hope that i didn't say any stupid thing . And i hope that you understand a bit more from classes...

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    your Derivated class didnt inherit any data members from the Base class

    Code:
    class Derivated
    
    // should be
    
    class Derivated: public Base
    nextus, the samurai warrior

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    i fix your code

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Base
    {
        int x;
        
    public:
        Base (int xx=0): x(xx){}
        virtual int getNum() {return x;};
        void PrintNum() { cout<<x; }
    };
    
    class Derivated: public Base
    {
        int newX;
    public:
        Derivated (int xx=1, int y=5): Base(xx){newX=y;}
    
        int getNum() {return newX;}
        void PrintNum() {cout<<newX;}
    };
    
    void main()
    {
        Base num(5);
        Derivated num2(5,10);
        Base *ptr;
    
        int y=num.getNum();   //just call the getNum from the base
        cout<<y;  //y=5
        num.PrintNum();  //print 5
    
        int w=num.getNum();  //Now it will call the getNum from the derivated classe because this function is virtual
        cout<<w;  //w=10
      
        //Now came the nice part
        ptr=new Derivated(5,20);
    
        int z=ptr->getNum(); //it will call the getNum from the derivated
        cout<<z;  //z=20
        ptr->PrintNum();  //it will print 5
    }
    now it is syntax correct
    nextus, the samurai warrior

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    also it should be num2.getNum() // outputs 10. but thanks i get what it does now.

    if the derive class has the same member function name as the base class it will have static linkage which it will call the base class member function, but if you use virtual then it has dynamic linkage which then when you call through a pointer it will chose the object that is associated with it.
    nextus, the samurai warrior

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Talking Of course

    Sure it must be class Derivaterublic Bse. Sorry...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. Polymorphism newbie question
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:38 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. A question Polymorphism
    By omeydhomey in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2003, 11:28 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM