Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Questiopn on Pure Virtual Function

Posted on 2000-04-20
9
Medium Priority
?
307 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

718 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