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



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

Answer / 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

More C++ General Interview Questions

Explain the use of this pointer?

1 Answers  


How do you define a class in c++?

1 Answers  


What is std namespace in c++?

1 Answers  


How many types of scopes are there in c++?

1 Answers  


What is the difference between containment and delegation?

1 Answers  


difference between macro and function?

3 Answers  


Difference between strdup and strcpy?

1 Answers  


What is scope operator in c++?

1 Answers  


What is virtual function? Explain with an example

1 Answers  


You have to take 2 arrays of length 10. Input the values of array 1 from the user. Then copy the values of array 1 to array 2 in ascending order For example if user enters 9, 5, 6, 8, 1, 0, 2, 7, 4, 3 then copy the smallest element i.e. 0 first followed by 1, 2 and so

1 Answers  


What is a class definition?

1 Answers  


What is near, far and huge pointers? How many bytes are occupied by them?

1 Answers  


Categories