C Programming 1 What does the following code do void drawSqu
C++ Programming
1) What does the following code do?
void drawSquares(Shape **p, int size)
{
Square *sp;
for ( int i = 0; i < size; i++ )
{
sp = dynamic_cast<Square*>(p[i]);
if ( sp )
sp->draw();
}
}
2) Explain why a factory member function should be a static member function.
Solution
Solution:
1) The above code is an example of downcasting, the code initially trying to obtain information at run time, so there are using pointers which can be used for testing dynamic_cast.
Using a cast from a base class type to a derived type is called downcasting.
The above function implements by taking an array of pointers to shapes as an argument and draws squares only.
2) Factory member function should be a static member function because functions can be used for:
