Hello Friends,


Generally, we have pure virtual function without body, but C++ does allow to write pure virtual function with body. Can anyone suggest any use-case where we need to write pure-virtual function with body?

Regards,
Vivek

Recommended Answers

All 17 Replies

Hello Friends,


Generally, we have pure virtual function without body, but C++ does allow to write pure virtual function with body. Can anyone suggest any use-case where we need to write pure-virtual function with body?

Regards,
Vivek

This is news to me, but it has been a while since I used CPP and maybe it has evolved. My understanding is that any class that implements a "pure" virtual function is an abstract class that forces you to define the function or it also becomes an abstract class. I am not aware that you can define a body for the function in your class and still have a "pure" virtual function.

how about a pure virtual destructor.

Also gotW

This is news to me, but it has been a while since I used CPP and maybe it has evolved. My understanding is that any class that implements a "pure" virtual function is an abstract class that forces you to define the function or it also becomes an abstract class. I am not aware that you can define a body for the function in your class and still have a "pure" virtual function.

Yes, it is possible.

#include <iostream>
using namespace std;

class animal
{
public:
    animal() {  }
    virtual void speak() = 0
    {
        cout << "meow\n";
    }
    void SayHello1() { animal::speak(); }
    void SayHello2() { speak(); }
};

class dog : public animal
{
public:
    dog() {}
    virtual void speak()
    {
        cout << "Woof\n";
    }
};


int main()
{
    dog d;
    d.speak();
    d.SayHello1();
    d.SayHello2();
   return 0;
}

results:

Woof
meow
Woof
Press any key to continue . . .

commented: can write like this [code]virtual void speak() =0 { //code } [/code] cool +1

Cool--didn't know that. Thanks for the info.

whats that supposed to mean?

Are you asking me to explain the article to you?

Hi DdoubleD,

Thanks.
I agree with your code snippet, but i have below questions:

1) Why does SayHello2() invoke speak() of dog instead of animal.
Although SayHello2() is a member function of animal. Is this due to the object being used of dog? In general, when i call a function from a member function, it calls from its own class, if the function is not available, it checks for base class. But in this case, it's checking for derived class, I am little confused. Can you share more details?

2) My original question is more about design, what would be a real time use-case where i need to write pure-virtual function with body?
In general, i make pure-virtual functions for abstract classes, and i want every derived class must override the function. Have you ever defined body for a pure virtual function, where you actually needed it?

Regards,
Vivek

Hi DdoubleD,

Thanks.
I agree with your code snippet, but i have below questions:

1) Why does SayHello2() invoke speak() of dog instead of animal.
Although SayHello2() is a member function of animal. Is this due to the object being used of dog? In general, when i call a function from a member function, it calls from its own class, if the function is not available, it checks for base class. But in this case, it's checking for derived class, I am little confused. Can you share more details?
*
2) My original question is more about design, what would be a real time use-case where i need to write pure-virtual function with body?
In general, i make pure-virtual functions for abstract classes, and i want every derived class must override the function. Have you ever defined body for a pure virtual function, where you actually needed it?

Regards,
Vivek

Firstly, that was not my code snippit.

1) because dog speak overrode animal...
2) yes. With respect to item 1, suppose you want to now define a particular dog with a particular barking sound. For each particular dog, you could do something like attach a sound unique to that breed of dog. Otherwise, the default sound for a common dog would be utilized. Does that make sense?
2 cont.) Yes. I have overridden many virtual functions, but until today, I did not know that you could define a body in a "pure" virtual function as defined in CPP. So, I've learned something new today--hoorah!

2 cont.) Yes. I have overridden many virtual functions, but until today, I did not know that you could define a body in a "pure" virtual function as defined in CPP. So, I've learned something new today--hoorah!

I am still perplexed by this new identification of the definition of "pure", which was always treated as wholly abstract by myself in the past. This new definition (to me anyway) of a "pure" virtual function sort of depletes reason of the meaning of the word "pure".

Anyway, best of luck!

Hi,

1) Over riding should come into picture when i am using pointer to object, but here i am using the object directly. Do you think that overriding comes in picture?
2) Sorry, but i don't agree much, since in that case i will have a usual virtual function, not a pure virtual function. Anyways, let's leave this point, since you also happen to know this recently
like me:) Thanks.

Regards,
Vivek

Firstly, that was not my code snippit.

1) because dog speak overrode animal...
2) yes. With respect to item 1, suppose you want to now define a particular dog with a particular barking sound. For each particular dog, you could do something like attach a sound unique to that breed of dog. Otherwise, the default sound for a common dog would be utilized. Does that make sense?
2 cont.) Yes. I have overridden many virtual functions, but until today, I did not know that you could define a body in a "pure" virtual function as defined in CPP. So, I've learned something new today--hoorah!

Hi,

1) Over riding should come into picture when i am using pointer to object, but here i am using the object directly. Do you think that overriding comes in picture?
2) Sorry, but i don't agree much, since in that case i will have a usual virtual function, not a pure virtual function. Anyways, let's leave this point, since you also happen to know this recently
like me:) Thanks.

Regards,
Vivek

You are not making sense to me:
1) overriding a function has nothing to do with "using a pointer to an object", or "using object directly".
2) OK--we will leave that point out--LOL.

What are you asking now?

Look, you make a function virtual anticipating it will, or in fact needs to, be overridden. Therefore, the derived class can, or even should or must (1), implement an override of the function to alter characteristics/behavior and can also call the inherited function if need be. I assume that a "pure" virtual function (1) still requires the derived class to implement this functionality.

Are you asking me to explain the article to you?

No, I mean what was with the quote to my response?

how about a pure virtual destructor.

Also gotW

See: http://www.codeguru.com/cpp/tic/tic0163.shtml

No, I mean what was with the quote to my response?

I thought you had asked how this subject pertained to pure virtual destructors, so I provided that link which explained that aspect. I only now noticed you provided a link in the first quote above--sorry.

I'm pulling an excerpt from the article I prescribed for the benefit of people trying to read this thread:

"For a time, pure virtual destructors were legal and worked if you combined them with a function body, but in the final C++ standard function bodies combined with pure virtual functions were outlawed. This means that a virtual destructor cannot be pure, and must have a function body because (unlike ordinary functions) all destructors in a class hierarchy are always called."

Cheers!

"This means that a virtual destructor cannot be pure"

In c++ a destructor can be a pure virtual destructor.

You are not making sense to me:
1) overriding a function has nothing to do with "using a pointer to an object", or "using object directly".
2) OK--we will leave that point out--LOL.

What are you asking now?

1) May be I could not explain what i intended do.
As I understand, if I am using objects to classes, Early Binding is done by Compiler, and there is no virtual table in picture. And always the function of the class whose object is created gets called(if available). But in this case, speak() of dog class is getting called. Anyways, I got it. Reason is implicit "this" pointer added by compiler while calling speak() (or this->speak()). And since "this" points to dog class, speak() of dog is invoked.
Thanks alot:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.