Can we have a private virtual method ?

Answer Posted / ganesh mishra

yes... we can have private virtual method and will not give
any compile time/runtime error.but when we derive any class
from it and override the virtual function,then the compiler
will throw a compile time error.

//file name is privatever.cpp

#include <iostream>
using namespace std;

class base
{
virtual void fun()
{
cout <<"base class function"<<endl;
}

};

class derive: public base
{
public:
virtual void fun()
{
cout<<"derived class function"<<endl;
}
};

int main()
{
base *pt;
derive *der = new derive;
pt = der;
pt->fun();
return(0);
}

here is the error
privatever.cpp: In function ‘int main()’:
privatever.cpp:6: error: ‘virtual void base::fun()’ is private
privatever.cpp:27: error: within this context

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)

3558


What are the three parts of a simple empty class?

1462


What does it mean when someone says I oop?

587


What is the point of oop?

660


How is polymorphism achieved?

587






What do you mean by Encapsulation?

645


There are two base class B1,B2 and there is one class D which is derived from both classes, Explain the flow of calling constructors and destructors when an object of derived class is instantiated.

1460


What is encapsulation in oops?

541


What is the types of inheritance?

604


What is encapsulation in oop?

611


What is oops concept with example?

582


Why do we use class?

638


What polymorphism means?

624


What is the difference between procedural programming and oops?

559


How do you define social class?

604