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

any one please tell me the purpose of operator overloading

2157


Can you inherit a private class?

835


What are the 4 main oop principles?

935


What is oops with example?

757


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

4324


Get me an image implementation program.

1729


What do you mean by abstraction?

810


write knight tour problem which is present in datastructure

2369


class CTest { public: void someMethod() { int nCount = 0; cout << "This is some method --> " << nCount; } }; int main() { CTest *pctest; pctest->someMethod(); return 0; } It will executes the someMethod() and displays the value too. how is it possible with our creating memory for the class . i think iam not creating object for the class. Thanks in Advance... Prakash

1964


What is class and example?

814


2. Give the different notations for the class.\

1798


What is meant by multiple inheritance?

928


What is constructor overloading in oop?

847


Can you explain polymorphism?

818


What is inheritance write a program to show use of inheritance?

859