Category: Classes in C++

1: Polymorphism : 'Polymorphism' is an object oriented term. Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate actions.

 

In other words, polymorphism means taking more than one form. Polymorphism leads to two important aspects in Object Oriented terminology – Function Overloading and Function Overriding. Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called. Overriding refers to the modifications made in the sub class to the inherited methods from the base class to change their behavior.

 

2: 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();

}

 

3: 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.

 



Dinesh ThakurDinesh Thakur is a Columinist and designer with strong passion and founder of Computer Notes. if you have any ideas or any request please get @me on
linkedin FaceBook Twitter Google Plus