Solved

Questiopn on Pure Virtual Function

Posted on 2000-04-20
9
306 Views
Last Modified: 2010-04-02
Can I implement a pure virtual function?
How?
0
Comment
Question by:TSENTHILKLUMAR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

615 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