hi,

I have doubt about virtual function behaviour in below code snippet. As per my understanding class derive2 didn't override the virtual function fun() , so it is obvious that its object can't be assigned to the Base class pointer *p and even individually we can't instanciate the object of derive2 (please see the commented section) unless and untill we override the fun function in derive2 class due to having abstract base class.

#include<iostream.h>
using namespace std;
class Base
{
public:      
virtual void fun()=0;      

};

class derive:public Base
{

public:      

    void fun()
    {
    cout<<"fun:derive";
     }  

};

class derive1:public Base
{

public:      
   void fun()
   {

        cout<<"fun:derive1";

        }


};



class derive2 : public Base
{


 public:
  void show()
  {

       cout<<"show:derive2";

       }     


};


int main()
{
 Base * p=new derive;
 p->fun();
 p= new derive1;
 p->fun();
 //derive2 *a = new derive2;
 //a->show();
 //derive2 a;
 //a.show();
 derive2 *a;
 a->show();
 return 1;    
}

but my question is ,why still i am able to instanciate derive2 object as per below code.

derive2 *a;
a->show();
derive2 *a;
a->show();

This has not created an object of type derive2. This has created a pointer to some random piece of memory, and then tried to use that random piece of memory as if it were an object of type derive2. At no point has an object of type derive2 been created.

This is actually a dangerous situtation. The pointer a is expecting to point to an existing object of class derive2 or one of its derived classes, but you have it pointing to essentially a random point in memeory, because you have not initiallized the pointer. You have actually not created an object there. When this runs, a->show(); will be executed, but because it does not actually access the object, just a string literal and an object instantiated elsewere (cout), you might luck out and the program will actually execute without producing a segmentation violation and aborting the program. In fact, depending on your compiler/optimizer, it might treat show() like a static member function, which does not look at any particular instance of the class.

The problem with what you commented out was not happening at the assignment to the pointer, but at new, when it tried to create an object of the class, which you know is not allowed. A pointer to a pure abstract class is valid because it is expected that it will be pointing to an object of one of its properly defined derived classes instead of a object of the abstract class itself.

Guys,
It is true so if you create object like below code , it is going to flash the error which is expected.

derive2 a;

 derive2 *a1 =&a ;

Thanks for your input, may be in case of complex code i could have expected segmentation violation error :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.