implementing a polymorphic function in C++

This is a discussion on implementing a polymorphic function in C++ within the C++ Programming forums, part of the General Programming Boards category; Which of the following must be ensured in order to implement a polymorphic function in C++? A. There has to ...

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37

    implementing a polymorphic function in C++

    Which of the following must be ensured in order to implement a polymorphic function in C++?

    A. There has to be a pointer of the derived class that has implemented the polymorphic function that holds the address of the derived class object.
    B. The function must be declared as virtual in both the base class and in the derived class that overrides the function.
    C. The function must be declared as pure virtual.
    D. There has to be a base class pointer holding the address of a base or derived class object that has implemented the polymorphic function.
    E. The function must be declared as virtual in the base class.

    Ok so for this I feel:
    * A is incorrect - but I really don't understand the answer given.
    * B is incorrect, because virtual keyword is not need in the derived class.
    * C is incorrect. because to be polymorphic it doesn't have to be pur virtual.
    * D is incorrect - but I really don't understand the answer given.
    * E is correct,

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    A - C is incorrect. D & E is correct.
    Basically, what A is asking is this:
    Code:
    Derived* p = new Derived;
    Is this a requirement for polymorphism?

    D asks the same, but using a Base class pointer.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,778
    Quote Originally Posted by coletek
    * A is incorrect - but I really don't understand the answer given.
    The sentence might be ambiguous, but my interpretation is that it is asking if you need a derived class pointer to point to an object of the corresponding derived class in order to implement a relevant polymorphic function. The answer would be no because this is not relevant to the question, since this involves using the polymorphic function via a pointer instead of implementing it. Also, such a function call would be statically bound, not dynamically bound, and by "polymorphic" we presumably mean runtime polymorphism achieved by dynamic binding.

    Quote Originally Posted by coletek
    * B is incorrect, because virtual keyword is not need in the derived class.
    * C is incorrect. because to be polymorphic it doesn't have to be pur virtual.
    I agree.

    Quote Originally Posted by coletek
    * D is incorrect - but I really don't understand the answer given.
    Refer to my explanation for A. This time, the statement talks about using a base class pointer to point to an object of a relevant derived class. Now, in this case dynamic binding would be applicable, and there might be actual use of runtime polymorphism. But like in A, this is about using polymorphism, not about implementing a function, so the answer should be no.

    Quote Originally Posted by coletek
    * E is correct
    I agree.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    Also for D, polymorphism can also be done with references.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    We'll all have law degrees soon.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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