Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW meth
Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE
✍: Guest
Answer1
POLYMORPHISM : A phenomenon which enables an object to react differently to the same function call.
in C++ it is attained by using a keyword virtual
Example
public class SHAPE
{
public virtual void SHAPE::DRAW()=0;
}
Note here the function DRAW() is pure virtual which means the sub classes must implement the DRAW() method and SHAPE cannot be instatiated
public class CIRCLE::public SHAPE
{
public void CIRCLE::DRAW()
{
// TODO drawing circle
}
}
public class SQUARE::public SHAPE
{
public void SQUARE::DRAW()
{
// TODO drawing square
}
}
now from the user class the calls would be like
globally
SHAPE *newShape;
When user action is to draw
public void MENU::OnClickDrawCircle(){
newShape = new CIRCLE();
}
public void MENU::OnClickDrawCircle(){
newShape = new SQUARE();
}
the when user actually draws
public void CANVAS::OnMouseOperations(){
newShape->DRAW();
}
Answer2
class SHAPE{
public virtual Draw() = 0; //abstract class with a pure virtual method
};
class CIRCLE{
public int r;
public virtual Draw() { this->drawCircle(0,0,r); }
};
class SQURE
public int a;
public virtual Draw() { this->drawRectangular(0,0,a,a); }
};
Each object is driven down from SHAPE implementing Draw() function in its own way.
2012-02-08, 2896👍, 0💬
Popular Posts:
What is the difference between mysql_fetch_object() and mysql_fetch_array() functions in PHP? mysql_...
Which bit wise operator is suitable for turning off a particular bit in a number? The bitwise AND op...
What is the method to customize columns in DataGrid? Use the template column.
What metrics will you look at in order to see the project is moving successfully? Most metric sets d...
How can I use tables to structure forms Small forms are sometimes placed within a TD element within ...