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