adspace


Give an example of run-time polymorphism/virtual functions.

Answer Posted / Satish Singh

Run-time polymorphism, also known as dynamic polymorphism, in C++ is achieved using virtual functions. Here's an example:nn```cppnclass Shape {npublic:n virtual void draw() = 0;n};nnclass Circle : public Shape {npublic:n void draw() {n cout << "Drawing a circle" << endl;n}n};nnclass Square : public Shape {npublic:n void draw() {n cout << "Drawing a square" << endl;n}n};nnint main() {n Shape *shapes[2];n shapes[0] = new Circle();n shapes[1] = new Square();n for (int i = 0; i < 2; i++) {n shapes[i]->draw();n }n return 0;n}n```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How c functions prevents rework and therefore saves the programers time as wel as length of the code ?

1164


daily Routine of father

1483


What is the latest version on c++?

1206


Can union be self referenced?

1266


What character terminates all character array strings a) b) . c) END

1394