Solved

Questiopn on Pure Virtual Function

Posted on 2000-04-20
9
292 Views
Last Modified: 2010-04-02
Can I implement a pure virtual function?
How?
0
Comment
Question by:TSENTHILKLUMAR
9 Comments
 

Expert Comment

by:daknight2000
ID: 2735760
yes u can
somehting like
virtual myfunction()=0
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2735781
class A
{
     virtual void f();  // regular virtual
     virtual void v() = 0; // pure virtual
};

void A::f()
{
    cout << "A::f()" << endl;
}

void A::v()
{
    cout << "A::v() will never be executed" << endl;
}
0
 

Author Comment

by:TSENTHILKLUMAR
ID: 2736270
what i would like to know is in multilevel inheritance if i call a pure virtualfunction from a derivedclass function whichis not virtual it is getting executed which should not be, as a pure virtual function can not have a body
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2736593
once a function is declared as virtual, it is virtual for every class that derives from that base class, whether or not the keywork virtual is actually used in the derived class. And since you can't instantiate an object of a class that has any pure virtual functions, you can never run the risk of calling a virtual function that contains no body
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:Wyn
ID: 2736923
How can you call a pure vitual function ?
Base:Vitual()??
It's not legal and you'd never do that...
0
 

Expert Comment

by:kishore_joshi
ID: 2736985
class Base
{
      int i;
public:
      virtual void f() = 0;      // Pure Virtual Function
};


void Base::f()             // Implimentation of Pure Virt Function
{
      cout<<" \n I cannot be called.... ";
}


class Derv1:public Base
{
      int j;
public:
      void f()
      {
            cout<< " I am in Derv1 \n ";
      }

      void f_d1()
      {
       cout<<" in f_d1 function of the d1 class.. going to call base PVF";
       Base::f();   // Calling the pure virtual fuction of the Base class
      }
};


class Derv2:public Derv1
{
      int k;
public:
      void f()
      {
            cout<< " I am in Derv2 \n ";
      }

};



void main()
{

      Base *bPtr;

      Derv1 d1; bPtr = &d1;
      bPtr->f();
      d1.f_d1();


      Derv2 d2; bPtr = &d2;
      bPtr->f();
      d2.f_d1();

}

0
 
LVL 2

Expert Comment

by:alcindor
ID: 2745412
If the original question still stands, then I would say that you wil NEVER need to implement a pure virtual function. If you wish to provide some default base function then don't make it pure virtual.
The idea of pure virtual functions are to define interface specifications for derived classes.

Roger
0
 
LVL 2

Accepted Solution

by:
abesoft earned 50 total points
ID: 2748570
You can indeed implement a pure virtual function.  A PV function is just like any other virtual function, with one exception: no class containing (or inheriting without overriding) a pure virtual function cannot be instantiated.

So, for example:
class PVBase{
public:
    virtual void Print() = 0
    {   cout << "Base printing support";}
};
class Derived: public PVBase{
    virtual void Print()
    {   cout << "Derived printing support";
        // Now, print the base portion!
        PVBase::Print();
    }
};

If you choose to implement your pure virtual function, then it can still be called, although it won't be called "virtually".  That is, you will never end up calling it by:
    myPointer->Print()
because myPointer will (by definition) be an instance of a class that has over-ridden the Print function.

I think that this is a rather obscure part of C++, but it can be used effectively in some cases.  Basically, this mechanism allows the base class to provide some "standard" operations for a function, but still require derived classes to specialize the behaviour.  Printing is one place that I could imagine it being useful.

To alcindor: Yes, you are right in saying that you will never _need_ to implement a PV.  But it is nevertheless useful, in that it can be called.
0
 
LVL 2

Expert Comment

by:abesoft
ID: 2748577
Oh, the other part of your question was "how do I implement a PV function"?  The syntax is the same as any virtual function.  You can define it inline (as in my example) or out-of-line, as in kishore_joshi's example.  The only difference is the "=0" item in the class header.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now