Hello Friends,

I want to know - "What is Polymorphism in C++?" Books can not help me...please make me clear about it..in simple terms..please...!!

Recommended Answers

All 6 Replies

the way i see it is just the ability of a pure abstract parent class to be able to execute different code sections, from their children classes, that are under the same function name. This is done by using pointers and virtual functions

class A {
    public: 
        virtual void foo() = 0; // pure virtual function makes this an abstract class
}

public class B : public A {
    public:
        virtual void foo() { cout << "hi" << endl; }
}

public class C : public A {
    public:
        virtual void foo() { cout << "bye" << endl; }
}

// we crete pointers of type A and initialize them to their children classes
A* a = new B();
A* c = new C();

a.foo();  // this prints hi
c.foo();  // this prints bye

If I'm not mistaken, when function A is compile, A.foo() memory value is left blank and it is assigned at run time when A* a = new B() and A* a = new C() are called, or something like that

@uonsin do you think its important to put virtual keyword in class B and C?
I thought just the base class needs virtual.

do you think its important to put virtual keyword in class B and C?

It's not necessary, but it is good practice to do so, just to be explicit about it. Sometimes the code for the base class and the code for the derived class are far appart (different files, different folders, different libraries, etc.) and so, it's good to have this reminder in the derived class too. In fact, C++11 introduced a new keyword called override to use for this purpose (and it also triggers an error if the base-class function is not virtual, which is a very nice bonus) (read more).

What is Polymorphism in C++?

Polymorphism is when a single entity in the code (like a variable or parameter) can take any (or many) forms that lead to different behaviors. Dynamic polymorphism is achieved essentially as uonsin showed, by having virtual functions in a base class, with different implementations of them in the different derived classes. By creating an object of a derived class, and then passing it off as a base-class object (by pointer or by reference), the code that uses that base-class object can be written once for that base-class, but in actuality, it behaves differently for each derived class that is used. This is one of the fundamental benefits of polymorphism: write code once for some abstract interface, and reuse it for every derived class that exists or will ever exist.

There is also static polymorphism, which is very important in C++, but it's a bit more of an advanced topic (uses templates). The idea is the same (polymorphism) but instead of implementing the one-to-many mapping at run-time (dynamic) via a virtual function call, it is implemented at compile-time (static) via generic template parameters (and things like concepts, tag-dispatching and type-traits).

Thanks @mike_2000_17

In programming languages, polymorphism means that the behaviour of operations or objects differently in different environment. The most simple example would be the '+' sign, which can be used for different purposes in C++. For example,
the '+' can be used in addition operation of intergers, also for the addition of floating point numbers, for the string concatination operation, and the '+' can also be used as the logcal 'and' in boolean searching.

dear there is a lot site provide detailed information about that. I think you have to google it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.