Thread: A random doubt that came in my mind.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    A random doubt that came in my mind.

    Hello,

    I was just wondering what is difference between pure virtual function and abstract method? By defining pure virtual function the class itself becomes abstract and we cannot create instance of that class and same is concept with abstract class. Both, the pure virtual function and abstract method do not have body implementation in class in which it is declared so where the difference lies?

    A pure virtual function also makes it compulsory to define in derived class and so is abstract method(unless the derived class is too declared as abstract).

    Thanks in advance

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    C++ has no concept of abstract methods or abstract classes. There are only pure virtual methods. If a class has one or more pure virtual methods, it can't be instantiated. That's really all there is.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok! actually learning too many things at a time can lead to this. In c# we have abstract keyword so i thought what is difference between virtual and abstract but i forgot that c++ has no abstract keyword . Thanks brewbuck you're the man

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You sound like you're coming from a C# background, and carry among you a proliferation of keywords that C++ neither needs or uses.

    First, a virtual method is one in which a derived class may override and change the meaning of that method. A pure virtual method (I suppose you could call this an 'abstract method') has no body, and must be overridden in a derived class that wishes to be instantiable.

    A class is merely an abstract base class if it contains a pure virtual method. This is never stated explicitly in code -- the presence of a pure virtual function just means it can't be instantiated. 'abstract class' is merely a term used to describe a class that can't be instantiated for this reason. (Or if a class it derives from has a pure virtual method that has not been overridden by this point in the hierarchy.)

    Let your keywords be your guide. If you know what a keyword does, does it fit the definition of whatever word you're trying to apply to it? If you don't know what a keyword does, then don't 'doubt' it... ask. You don't 'doubt' something -- either you do know it, or you don't. If you don't, then ask a 'question', but if you have doubts, then you're probably not going to form a coherent question. When you ask a question, there should be no doubt: You are confident that you have no clue, and that you are asking a question (hopefully an educated one) to get a clue.
    Last edited by Cactus_Hugger; 07-13-2009 at 08:10 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by Cactus_Hugger View Post
    Let your keywords be your guide. If you know what a keyword does, does it fit the definition of whatever word you're trying to apply to it? If you don't know what a keyword does, then don't 'doubt' it... ask. You don't 'doubt' something -- either you do know it, or you don't. If you don't, then ask a 'question', but if you have doubts, then you're probably not going to form a coherent question. When you ask a question, there should be no doubt: You are confident that you have no clue, and that you are asking a question (hopefully an educated one) to get a clue.
    And you sound from a philosophical background. Aren't you? I really dont think there was any need to write such a thing in this thread. But anyways, thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> A pure virtual method (I suppose you could call this an 'abstract method') has no body

    A pure virtual method can have a body and does in some cases. The most common example is a pure virtual destructor, which must have a body. You can also provide a body for other pure virtual functions if you want to, the language does not restrict it.

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Daved
    A pure virtual method can have a body and does in some cases. The most common example is a pure virtual destructor, which must have a body. You can also provide a body for other pure virtual functions if you want to, the language does not restrict it.
    Exactly, how? Example? I would argue the language does restrict it:
    Quote Originally Posted by C++ standard, §10.4.2
    a function declaration cannot provide both a pure-specifier and a definition. For example,
    Code:
    struct C {
        virtual void f() { }=0; // ill-formed
    };
    A "pure virtual method" is a virtual method that is not defined, ie, it has no body. The two are opposites.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cactus_Hugger
    Exactly, how? Example? I would argue the language does restrict it:
    Notice that the term is "function declaration". That sentence states that you cannot declare a member function as pure virtual and define it at that point of declaration. It does not state that you cannot define a pure virtual member function. Note that that very paragraph has the sentence: "A pure virtual function need be defined only if explicitly called with the qualified-id syntax (5.1)." Also, refer to section 12.4 paragraph 7:
    A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined.
    Quote Originally Posted by Cactus_Hugger
    A "pure virtual method" is a virtual method that is not defined, ie, it has no body. The two are opposites.
    That is not correct. Read Stroustrup's definition:
    pure virtual function - virtual function that must be overridden in a derived class. Indicated by the curious =0 syntax. A pure virtual function can be defined in the class where it is declared pure, but needn't be and usually isn't. A class with at least one pure virtual function is an abstract class.

    That said, Stroustrup's definition is a little inaccurate. I would add ", otherwise the derived class is an abstract class" to "must be overridden in a derived class".
    Last edited by laserlight; 07-13-2009 at 11:49 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Exactly, how? Example?
    Pure virtual destructors are a great example. They must be defined. Hopefully laserlight's post answers your question fully, if not let me know and I'll expand my own comments further.

    >> "must be overridden in a derived class"
    That's not entirely accurate, as a derived class doesn't necessarily have to be a concrete class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. random numbers
    By ballmonkey in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2005, 02:16 PM
  5. Replies: 4
    Last Post: 11-16-2004, 07:29 AM