Solved

Questiopn on Pure Virtual Function

Posted on 2000-04-20
9
301 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

861 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