Thread: function = 0 ;

  1. #1
    Unregistered
    Guest

    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

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    It's a abstract virtual function. A derived object of that class cannot be defined until that function is defined.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    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.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    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.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM