Polymorphism

What is Polymorphism ? Give me a simple example of it.
No offense, but do your own web search. The question is big enough that you are going to be better off reading a well written static web page with this info than us trying to recreate the info here in a short post. If you have a deeper question after reading that, then we can try to help.
i have googled it . But i didnt understand that very well , thats why i asked you guys to describe it with a simple example that i can understand.
Show an example that you've studied but do not understand. Ask a specific question about it.
I don't think I can say it any better than: https://www.geeksforgeeks.org/polymorphism-in-c/

The idea, as simply as I can express it, is that you sometimes want two classes to behave the same way or 'be the same thing' for a moment in your code.
1
2
3
4
#include <iosfwd>
class Foo;

std::ostream& operator<< ( std::ostream&, const Foo& );


1
2
3
4
5
6
7
8
9
int main() {
  Foo bar;
  std::ofstream ofs( "snafu.txt" );
  std::ostringstream gaz;

  cout << bar;
  ofs  << bar;
  gaz  << bar;
}

What happens on lines 6--8?

All three output the bar. All three use the same operator.
All three: cout, ofs and gaz are all ostream objects.
Looks like a duck, sounds like a duck, ...

However, they are slightly different streams. The output goes to different destinations: standard output, file and string buffer, respectively.
Thank you guys.. I understand this right now..
closed account (E0p9LyTq)
http://www.cplusplus.com/doc/tutorial/polymorphism/
Topic archived. No new replies allowed.