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 |
Explain the use of this pointer?
How do you define a class in c++?
What is std namespace in c++?
How many types of scopes are there in c++?
What is the difference between containment and delegation?
difference between macro and function?
Difference between strdup and strcpy?
What is scope operator in c++?
What is virtual function? Explain with an example
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
What is a class definition?
What is near, far and huge pointers? How many bytes are occupied by them?