Can we have a private virtual method ?
Answers were Sorted based on User's Feedback
Answer / nidhi singh
no, we cann't hav private virtual method as virtual is used
in case of inheritance but private members cannot be
inherited..
Is This Answer Correct ? | 10 Yes | 1 No |
Answer / rajendra gandhi
No, we can't have private vitual method.
Is This Answer Correct ? | 6 Yes | 2 No |
Answer / edward
yes... we can have private virtual method and will not give
any compile time/runtime error. but there is no use of it.
we could not call the method from out side of the class
Is This Answer Correct ? | 5 Yes | 1 No |
Answer / nk
We can have virtual functions as long as we dont call the
function from a base pointer pointing to the base
class/derived class.
If we call then gives error.
The same error can be tested by compiling and running above
example.
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / sriram
We can have the private virtual method. But it can be
accessed only through the derived class not through the
base class.
class A
{
private:
virtual void fun() { std::cout << "A::fun" <<
std::endl; }
};
class B : public A
{
public:
virtual void fun() { std::cout << "B::fun" <<
std::endl; }
};
int main(int argc, char* argv[])
{
A* pa = new A();
((B*)(pa))->fun();
}
Output : A::fun
Is This Answer Correct ? | 4 Yes | 3 No |
Answer / 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 |
Answer / gopinath das
Yes , It we can have a private virtual method. It has
nothing to do with inheritance. It only resolve the dynamic
binding . Only difference is that , this method should be
used inside the class defination of the Base .
The derived class may implements the method either
public,private or protected.
Exa:
class A
{
virtual void x(){cout<<"A"<<endl;};
public:
};
class B:public A
{
//virtual void x(){cout<<"B"<<endl;}; // no prob
public:
virtual void x(){cout<<"B"<<endl;};
};
int main()
{
A *a;
B *b=new B;
a=b;
a->fn();
}
Out put:
B
Is This Answer Correct ? | 4 Yes | 7 No |
what is difference between objects and function
what is polymorphism?
program in c++ that can either 2 integers or 2 floating point numbers and output the smallest number
What is constructor in oop?
What is deep and shalow copy?
#include <string.h> #include <stdio.h> #include <stdlib.h> #include<conio.h> void insert(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); insert(s, strlen(s)); printf("The sorted string is: %s.\n", s); getch(); return 0; } void insert(char *items, int count) { register int a, b; char t; for(a=1; a < count; ++a) { t = items[a]; for(b=a-1; (b >= 0) && (t < items[b]); b--) items[b+1] = items[b]; items[b+1] = t; } } design an algorithm for Insertion Sort
What is debug class?what is trace class? What differences are between them? With examples.
what is virtual function in c++
What is object and example?
You attempt to query the data base with this command: SELECT name, salary FROM employee WHERE salary=(SELECT salary FROM employee WHERE last name='Wagner' OR dept no=233) Choose most appropriate option from the following: 1)Sub-queries are not allowed in the where clause. 2)a multiple row sub-query used with a single row comparison operator. 3)a single row query is used with a multiple row comparison operator.
How is polymorphism achieved?
what is oppes