-
function = 0 ;
Hello everyone :
Can anyone solve my questions ?
What is the meaning of functions declarations as below :-
virtual void PutLine(void) = 0 ;
It is defined at the mother class and many sub-class have same name PutLine(void) functions. It seems that the declaration is useless in mother class but for the sub-class. But how a void function equal to zero ?
Thank You for your attention.
Lau
-
It's a abstract virtual function. A derived object of that class cannot be defined until that function is defined.
-
Basically, it is saying that it will never be used, and it will only be there so that a derived class can use its own implementation of it.
-
Basically, it is saying that it will never be used
That's completely wrong.
It's saying that it requires an interface with that function name, with those parameters, but it requires derived objects to create their own implementation of that function. Eg. a class called Shape should have a function that would output it's area... but it doesnt know what type of shape it is so it declares a pure virtual function called Area() that the derived objects must implement themselves.. (since each shape will have it's own way of calculating the area).
This doesn't mean that the Shape object can't call the Area() function.
A class such as this, that contains a pure virtual function (ie function = 0), can not be instantiated.
U.
-
Sorry about my bad explanation, I meant that it will never be used by itself and that it can only be used from a derived class.
-
Sorry about my bad explanation, I meant that it will never be used by itself and that it can only be used from a derived class
this is still wrong mate. Just because a class has a pure virtual function, it doesnt mean that it can't call that function itself!
Here is an example of a class that calls one of it's own pure virtual functions.. and it works fine (BTW, i havent' compiled this... but i know it works)
Code:
class Shape
{
public:
void ShowArea(void);
virtual int Area(void) = 0;
};
class Square : public Shape
{
public:
virtual int Area(void);
};
void Shape::ShowArea(void)
{
cout << "My area is " << Area() << endl;
}
int Square::Area(void)
{
return(side * side);
}
i hope this clears it up.
U.