Solved

Questiopn on Pure Virtual Function

Posted on 2000-04-20
9
295 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to clear a vector as well as how to detect empty vectors in C++.

947 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

22 Experts available now in Live!

Get 1:1 Help Now