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
How is class defined?
What is oops?what is its use in software engineering?
what are the ways in which a constructors can be called?
Why it is called runtime polymorphism?
write a programe to calculate the simple intrest and compund intrest using by function overlading
What is overloading in oops?
What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?
What is object in oop?
what type of questions
What are the components of marker interface?
How does polymorphism work?
Can static class have constructor?
Can enum be null?
What is debug class?what is trace class? What differences are between them? With examples.
What is the diamond problem in inheritance?