Polymorphism

This is a discussion on Polymorphism within the C++ Programming forums, part of the General Programming Boards category; Hi guys I just want to clear an issue out regarding polymorphism. Lets say i have a method Display in ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    1

    Polymorphism

    Hi guys

    I just want to clear an issue out regarding polymorphism. Lets say i have a method Display in base class and in derived class, both as virtual.. how is it possible to call the Display from base class ? Would that just be like base:: Display() ? (dont mind the space .. thats simply because : shows a smiley hehe)

    anyway here is a small implementation just to give a better picture of what im asking

    Base Class
    Code:
    class Base
    {
    public:
    	virtual void Display()
    	{
    		cout << "Class Base";
    	}
    
    };

    Derived Class
    Code:
    class Derived : public Base
    {
    public:
    	virtual void Display()
    	{
    		cout << "Class Derived";
    	}
    };

    Main
    Code:
    void main()
    {
                    Base *temp = new Derived;
    	temp->Display();
    }
    lets say i want temp to show "Class Base" as output... would that mean id have to call it like i mentioned above ? i.e temp->Base:: Display(); ? And if that is the case, will that still be 'polymorphism' since it could be more like overriding and not needing to use virtual in prototype

    Thanks for your help!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> would that mean id have to call it like i mentioned above ? i.e temp->Base::Display(); ?
    Yes. (Disable smilies in advanced forum editor to remove the smilie).

    >> will that still be 'polymorphism'
    No, it isn't polymorphism. In most cases you don't really want to do that because the entire point of a virtual function is to let the derived class implement it however it wants. The most common use of Base::Display() is if a derived class wanted to call the base class version itself.

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. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 04:41 AM
  3. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  4. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  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