Results 1 to 11 of 11

Thread: Polymorphism

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Polymorphism

    How does C++ support Polymorphism? (Or which features of C++ implement polymorphism and how?)

    For example Overloading and virtual functions help in implementing polymorphism.

    Last edited by kalayama; 12-19-2006 at 02:06 AM.
    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  2. #2
    Expert Member
    Join Date
    Oct 2005
    Answers
    383

    Re: Polymorphism

    I think Abstract classes support polymorphis.

    Also function overloading,since the name of functions can be same but either their retun type, no: of paramerts etc can be different.So which funtion is calle d depend on the run time.

    Polymorphism simply means single interface multiple methods, means many forms.
    Bye

    Last edited by vmshenoy; 01-05-2007 at 02:43 AM. Reason: got something wrong!typing mistake
    :)
    NEVER SAY DIE.

  3. #3
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: Polymorphism

    Totally two types of polymorphism are there....

    1.compile time polymorphism
    2.run time polymorphism

    Using method overloading u can acheive the compile time polymorphism. It means Compiler knows at the compile time which object assigned to which class.

    Using method overridding u can acheive the run time polymorphism. It means Compiler didn't know the compile time and it knows only at the run time.

    i explained the above things using only my java knowledge...I think the concept are same...only the words are different...

    ------------------
    suresh


  4. #4
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: Polymorphism

    Yeps. Concepts remain the same. These languages just "impliment" OOPS concepts.

    -Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  5. #5
    Contributing Member
    Join Date
    Mar 2007
    Answers
    34

    Re: Polymorphism

    polymorphism means one instance multiple methods :
    method over riding is run time poly morphism & method overloading is run time poly morphism


  6. #6
    Junior Member
    Join Date
    Oct 2008
    Answers
    4

    Re: Polymorphism

    Quote Originally Posted by rohit dwivedi9450 View Post
    polymorphism means one instance multiple methods :
    method over riding is run time poly morphism & method overloading is run time poly morphism
    no i think method overloading is compile time poly morphism rather than run time


  7. #7

    Polymorphism

    Polymorphism
    See also: Polymorphism in object-oriented programming
    Polymorphism enables one common interface for many implementations, and for objects to act differently under different circumstances.

    C++ supports several kinds of static (compile-time) and dynamic (run-time) polymorphism. Compile-time polymorphism does not allow for certain run-time decisions, while run-time polymorphism typically incurs a performance penalty.

    Static polymorphism
    Function overloading allows programs to declare multiple functions having the same name (but with different arguments). The functions are distinguished by the number and/or types of their formal parameters. Thus, the same function name can refer to different functions depending on the context in which it is used. The type returned by the function is not used to distinguish overloaded functions.

    Similarly, operator overloading allows programs to define certain operators (such as +, !=, <, or &) to result in a function call that depends on the types of the operands they are used on. Overloading an operator does not change the precedence of calculations involving the operator, nor does it change the number of operands that the operator uses (any operand may however be ignored). The . :: .* ? operators can not be overloaded.

    Default arguments are used when defining a different function is not needed when supplying a default value for an argument will suffice. Care should be taken when using default arguments in conjunction with overloaded functions to not cause a conflict over which function to use. For instance, the following code:

    // function with default argument but also an overloaded function
    int strcpy(char *str1, char *str2, short unsigned n=65535);
    // second overloaded function
    int strcpy(char *str1, char *str2);
    will compile correctly when strcpy is used with an argument for n but not when no argument is specified. This is because the compiler has no means of knowing if the intended function is the first form with a default value of 65535 for n or the second form with no n argument.

    Templates in C++ provide a sophisticated mechanism for writing generic, polymorphic code. In particular, through the Curiously Recurring Template Pattern it's possible to implement a form of static polymorphism that closely mimics the syntax for overriding virtual methods (a dynamic polymorphism technique described below). Since C++ templates are type-aware and Turing-complete they can also be used to let the compiler resolve recursive conditionals and generate substantial programs through template metaprogramming.

    Dynamic polymorphism
    Variable pointers (and references) of a base class type in C++ can refer to objects of any derived classes of that type in addition to objects exactly matching the variable type. This allows arrays and other kinds of containers to hold pointers to objects of differing types. Because assignment of values to variables usually occurs at run-time, this is necessarily a run-time phenomenon.

    C++ also provides a dynamic_cast operator, which allows the program to safely attempt conversion of an object into an object of a more specific object type (as opposed to conversion to a more general type, which is always allowed). This feature relies on run-time type information (RTTI). Objects known to be of a certain specific type can also be cast to that type with static_cast, a purely compile-time construct which is faster and does not require RTTI.

    Through virtual member functions, different objects that share a common base class may all support an operation in different ways. The member functions implemented by the derived class are said to override the same member functions of the base class. In contrast with function overloading, the parameters for a given member function are always exactly the same in number and type. Only the type of the object for which this method is called varies. In addition to standard member functions, operator overloads and destructors can also be virtual.

    By virtue of inherited objects being polymorphic, it may not be possible for the compiler to determine the type of the object at compile time. The decision is therefore put off until runtime, and is called dynamic dispatch. In this way, the most specific implementation of the function is called, according to the actual run-time type of the object. In C++, this is commonly done using virtual function tables. This may sometimes be bypassed by prepending a fully qualified class name before the function call, but calls to virtual functions are in general always resolved at run time.

    An example:

    #include <iostream>

    class Bird // the "generic" base class
    {
    public:
    virtual void OutputName() {std::cout << "a bird";}
    virtual ~Bird() {}
    };

    class Swan : public Bird // Swan derives from Bird
    {
    public:
    void OutputName() {std::cout << "a swan";} // overrides virtual function
    };

    int main()
    {
    Swan mySwan ; // Creates a swan.

    Bird* myBird = &mySwan; // Declares a pointer to a generic Bird,
    // and sets it pointing to a newly created Swan.

    myBird->OutputName(); // This will output "a swan", not "a bird".

    return 0;
    }

    Guys let me know if I am wrong?


  8. #8
    Junior Member
    Join Date
    Oct 2008
    Answers
    4

    Re: Polymorphism

    Quote Originally Posted by animesh.chatterjee View Post
    Polymorphism
    See also: Polymorphism in object-oriented programming
    Polymorphism enables one common interface for many implementations, and for objects to act differently under different circumstances.

    C++ supports several kinds of static (compile-time) and dynamic (run-time) polymorphism. Compile-time polymorphism does not allow for certain run-time decisions, while run-time polymorphism typically incurs a performance penalty.

    Static polymorphism
    Function overloading allows programs to declare multiple functions having the same name (but with different arguments). The functions are distinguished by the number and/or types of their formal parameters. Thus, the same function name can refer to different functions depending on the context in which it is used. The type returned by the function is not used to distinguish overloaded functions.

    Similarly, operator overloading allows programs to define certain operators (such as +, !=, <, or &) to result in a function call that depends on the types of the operands they are used on. Overloading an operator does not change the precedence of calculations involving the operator, nor does it change the number of operands that the operator uses (any operand may however be ignored). The . :: .* ? operators can not be overloaded.

    Default arguments are used when defining a different function is not needed when supplying a default value for an argument will suffice. Care should be taken when using default arguments in conjunction with overloaded functions to not cause a conflict over which function to use. For instance, the following code:

    // function with default argument but also an overloaded function
    int strcpy(char *str1, char *str2, short unsigned n=65535);
    // second overloaded function
    int strcpy(char *str1, char *str2);
    will compile correctly when strcpy is used with an argument for n but not when no argument is specified. This is because the compiler has no means of knowing if the intended function is the first form with a default value of 65535 for n or the second form with no n argument.

    Templates in C++ provide a sophisticated mechanism for writing generic, polymorphic code. In particular, through the Curiously Recurring Template Pattern it's possible to implement a form of static polymorphism that closely mimics the syntax for overriding virtual methods (a dynamic polymorphism technique described below). Since C++ templates are type-aware and Turing-complete they can also be used to let the compiler resolve recursive conditionals and generate substantial programs through template metaprogramming.

    Dynamic polymorphism
    Variable pointers (and references) of a base class type in C++ can refer to objects of any derived classes of that type in addition to objects exactly matching the variable type. This allows arrays and other kinds of containers to hold pointers to objects of differing types. Because assignment of values to variables usually occurs at run-time, this is necessarily a run-time phenomenon.

    i have a que here...
    can u give any example which emphazize use of using base class (pointer or reference) necessary to have containers holding different types? ......................................


    C++ also provides a dynamic_cast operator, which allows the program to safely attempt conversion of an object into an object of a more specific object type (as opposed to conversion to a more general type, which is always allowed). This feature relies on run-time type information (RTTI). Objects known to be of a certain specific type can also be cast to that type with static_cast, a purely compile-time construct which is faster and does not require RTTI.

    Through virtual member functions, different objects that share a common base class may all support an operation in different ways. The member functions implemented by the derived class are said to override the same member functions of the base class. In contrast with function overloading, the parameters for a given member function are always exactly the same in number and type. Only the type of the object for which this method is called varies. In addition to standard member functions, operator overloads and destructors can also be virtual.

    By virtue of inherited objects being polymorphic, it may not be possible for the compiler to determine the type of the object at compile time. The decision is therefore put off until runtime, and is called dynamic dispatch. In this way, the most specific implementation of the function is called, according to the actual run-time type of the object. In C++, this is commonly done using virtual function tables. This may sometimes be bypassed by prepending a fully qualified class name before the function call, but calls to virtual functions are in general always resolved at run time.

    An example:

    #include <iostream>

    class Bird // the "generic" base class
    {
    public:
    virtual void OutputName() {std::cout << "a bird";}
    virtual ~Bird() {}
    };

    class Swan : public Bird // Swan derives from Bird
    {
    public:
    void OutputName() {std::cout << "a swan";} // overrides virtual function
    };

    int main()
    {
    Swan mySwan ; // Creates a swan.

    Bird* myBird = &mySwan; // Declares a pointer to a generic Bird,
    // and sets it pointing to a newly created Swan.
    i donot understand what is the need of using base class pointer here to refer to derived class object.... why don't we directly use derived class pointer..like Swan * mynewSwan=&mySwan;
    and then mynewSwan->OutputName();
    can anyone educate me on this ???

    myBird->OutputName(); // This will output "a swan", not "a bird".

    return 0;
    }

    Guys let me know if I am wrong?
    ..........................................................????


  9. #9
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: Polymorphism

    Some of the threads are merged for better management of the site.

    MODERATOR


  10. #10
    Junior Member
    Join Date
    Sep 2008
    Answers
    3

    Re: Polymorphism

    Hi, Can you tell me what polymorphism is and an example of it please as it is all new to me.
    Thanks


  11. #11

    Re: Polymorphism

    Polymorphism usually functions to retain variety of form


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact