+ 4

What is polymorphism in the simplest form

c++ or programming in general

1st May 2017, 6:28 PM
Sello Selby Bopape
Sello Selby Bopape - avatar
5 ответов
+ 12
Well there are two types of polymorphism, compile-time and run-time. Compile-time is implemented using templates in C++ for example. Run-time could be anything like function overloading and virtual functions. It simply means that you have different implementations based on a single blueprint. (edit function overloading is compile time sorry)
1st May 2017, 7:05 PM
Karl T.
Karl T. - avatar
+ 5
One method, many implementations. That's the simplest explanation. Example: Let's make a method and let's call it "ADD" and give it at least two parameters because we want to......? ADD something! That's correct. ADD(int variable_1, int variable_2) Okay now that we have our method, we want to ADD something right? Let's say we want to add two numbers. We can do so by just calling that method and put in two numeric arguments. ADD(1,1) //Output 2 But hey? Can this method only add numbers? Yes! But I thought you said, "One method, many implementations"? Well then let's just change it's variable's data type to string instead of integer. Oh? ADD(String variable_1, String variable_2) And this method will now be able to add two strings because the code inside the method didn't change. It still adds two variables. This is the simplest explanation I can give because you must first get the idea in your head that one method can have many implementations. It gets more complicated than this but if you have this idea in your head then it should be a start to understanding.
1st May 2017, 8:14 PM
Ghauth Christians
Ghauth Christians - avatar
+ 4
that makes sense
1st May 2017, 8:19 PM
Sello Selby Bopape
Sello Selby Bopape - avatar
+ 2
I know that polymorphism is the quality that allows one interface to access a general class of actions. but I need a practical or simplest form of explanation
1st May 2017, 6:32 PM
Sello Selby Bopape
Sello Selby Bopape - avatar
+ 2
here is an example in c++: #include <iostream> class Base { public: virtual void hello () { std::cout << "Hello from Base" << std::endl; } }; class Derived : public Base { public: virtual void hello () { std::cout << "Hello from Derived" << std::endl; } }; void main () { Base* b = new Derived(); b->hello(); // outputs "Hello from Derived" - although you have a pointer to the Base class, the method of the Derived class is called }
1st May 2017, 7:16 PM
Christoph Eibel