Can anyone please explain runtime polymorphism with a real
time example??at what ciscumstances we go for it??

Answer Posted / anumeet

Here is a program of run-time polymorphism.
#include <iostream.h>
#include <string.h>
class father {
char name[20];
public:
father(char *fname) // 1 argument constructor
{
strcpy(name,fname)
}
virtual void show()
{
cout<<"father's name: "<<name<<"\n";
}
};
class son : public father
{
char name[20];
public:
son(char *sname,char *fname): father(fname)
{
strcpy(name,sname);
}
void show()
{
cout<<"son name: "<<sname<<"\n";
}
};
void main()
{
father *fp;
father f1("emraan");
fp=&f1;
fp->show();
son s1("kangana","emraan");
fp=&s1;
fp->show();
}




Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which language is not a true object oriented programming language?

750


Why it is called runtime polymorphism?

677


What is the point of polymorphism?

674


Can private class be inherited?

717


can inline function declare in private part of class?

3786






What is meant by oops concept?

686


What do you mean by variable?

668


How long to learn object oriented programming?

667


What are the 3 principles of oop?

714


What type of loop is a for loop?

777


Why is it so that we can have virtual constructors but we cannot have virtual destructors?

4042


What does no cap mean?

683


What is coupling in oop?

690


Can abstract class have normal methods?

684


What is encapsulation c#?

696