polymorphism

This is a discussion on polymorphism within the C++ Programming forums, part of the General Programming Boards category; Hi, what is polymorphism? Any chance of a quick guide line please! maybe an example? or a web page if ...

  1. #1
    Unregistered
    Guest

    Question polymorphism

    Hi, what is polymorphism?

    Any chance of a quick guide line please! maybe an example?

    or a web page if you know one, but a guide line would be great...

    Thank you so much for reading.

    --------------------
    from: desperate for help

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    oh sigh, it is easier to point you a good tutorial that write here n pages of text.
    So here you go: http://firstpod.tripod.com/cpp21/ and chapter 12 and 13. It's really a good tutorial, nothing too long.
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    That's the same reason I did not reply. Kudos to you for giving him a link. I'm just lazy.

  4. #4
    rm3
    Guest

    heh

    Polymorphism is a number of things, I think, but what I got from it (sort of) is a bit like this:

    int function(int a, int b)

    and

    int function(int c, double a)

    are different

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Polymorphism

    Try this website i found it very interesting
    http://zeus.eed.usv.ro/misc/doc/prog/c/msvc12/vcl22.htm

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    I'm sure these are all great links, and I'm also sure polymorphism is in the faq, but here's my 2 cents worth.

    Polymorphism is used when you want to call a function of the same name that accepts different kinds of inputs.

    For example if I have a that accepts integers like this:

    void afunction(int a, int b)
    {
    printf("a %d, b %d",a,b);
    }

    but I also want my function to accepts floating points, I just continue to declare the function as usual

    void afunction(float a, float b)
    {
    printf("a %f, b%f",a,b);
    }

    so now, if I have integers or floating point numbers, I can call the same function... please note however, that for every different input you have, you have to declare another function... polymorphism allows you to use the same function to accept different or multiple inputs, or even process different inputs in different ways.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    Maybe I'm totally outa whack here.. but I was under the impression that your examples above are simply overloaded functions? I always though polymorphism requires the use of a classes.

    for ex:

    Code:
    /*  Header file
    #ifndef MAIN_H
    #define MAIN_H
    
    class CParent
    {
    public:
    	virtual int foo() const { return 1; };
    };
    
    class CChild : public CParent
    {
    public:
    	int foo() const { return 2; };
    };
    
    #endif
    Code:
    /* cpp file
    #include <iostream>
    #include "main.h"
    
    using namespace std;
    
    void polymorphic_func(CParent &);
    void non_polymorphic(CParent);
    
    int main()
    {
    	CParent p;
    	CChild c;
    
    	non_polymorphic(p);
    	non_polymorphic(c);
    	polymorphic_func(p);
    	polymorphic_func(c);
    
    	return 0;
    }
    
    void polymorphic_func(CParent &p)
    {
    	cout << p.foo() << endl;
    }
    
    void non_polymorphic(CParent p)
    {
    	cout << p.foo() << endl;
    }

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Polymorphism is calling virtual function from a class, but keep in mind you must use a pointer to call the function.

  9. #9
    Unregistered
    Guest
    CuriousJay & SilasP are correct. Polymorphism is an OOP approach whereby code calls base class virtual functions with a base class pointer, which then calls the appropriate derived class function of the same name. E.g., code can call base class Shape virtual function Draw with a pointer, and the Draw function in the correct derived class, say Square or Circle, will be called.
    This means that you don't need to know what derived function is called at compile time. A user might pick which derived Shape to draw at run time, it might be done randomly in a game, etc. New derived classes can be added more easily as well, since new class specific code doesn't need to be added to the program (at least as far as the new class working).
    This type of thing could be done with switch statements or multiple if checks, but that is usually slower and much more code writing.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    Yeah, they're right. That's what I get for trying to think C during class :P Thanks for the correction guys.

    -Max

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Poly - many
    morph - shape

    eg:

    Class A {}
    Class B: public A{}
    Class C: public A{}
    Class D: public C{}

    void main(void)
    {
    A a;
    B b;
    C c;
    D d;

    A* ap;

    ap = &a;
    ap = &b; // valid because B is derived from A
    ap = &c; // valid because C is derived from A
    ap = &d; // valid because D is derived from C, which is derived from A
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

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. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  3. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  4. change sorting method using polymorphism
    By Forever82 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 01:21 PM
  5. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM

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