pure virtual function
Wednesday, January 25, 2006 7:57:18 PM
Virtual function is used to ease polymorphism implementation, which allows the c++ compiler to differentiate the actual "type" of the objects created which derived from a base class.
Pure virtual function is also virtual function, but it do not have a body. It looks just like a function declaration, and it forced the derived class to implement the function defination. (compiler gives error if the derived class do not define the pure virtual function). The base class that contains pure virtual function is call abstract class. It defines generic interfaces without specified the functionalities.
How to declare virtual function and pure virtual function?
That is simple, just add the magic keyword infront of the function you wanna turn it to virtual function:
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area () //THIS MAKES THE FUNCTION VIRTUAL
{ return (0); }
};
How about Pure Virtual Function?
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area ()=0; //PURE, NO INLINE DEFINATION
};
I remember when I first learn about pure virtual function, it was 6 years ago. Never used and exam also didn't covers. Its been awhile I didn't really touch Object-Oriented C++.













