Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Can we have a private virtual method ?

Answers were Sorted based on User's Feedback



Can we have a private virtual method ?..

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

Can we have a private virtual method ?..

Answer / rajendra gandhi

No, we can't have private vitual method.

Is This Answer Correct ?    6 Yes 2 No

Can we have a private virtual method ?..

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

Can we have a private virtual method ?..

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

Can we have a private virtual method ?..

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

Can we have a private virtual method ?..

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

Can we have a private virtual method ?..

Answer / narasimulu

No we can't have private virtual method

Is This Answer Correct ?    3 Yes 4 No

Can we have a private virtual method ?..

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

Post New Answer

More OOPS Interview Questions

what is virtual function in c++

6 Answers  


What is destructor example?

0 Answers  


create a c++ program that will accepts 9 inputs using 3 by 3 array.

1 Answers  


1.what are two type of classe members called? 2.what is data hiding and data encapsulation? 3.how do you make a class member visible aouside its class? 4.what is the default visibility of a class data member? 5.what are the advantages of oop over the structured programing?

6 Answers  


WHY FUCTION OVERLOADING DOSENOT RETURN A RETEN TYPE

2 Answers  


difference between class and object

10 Answers   Chandan, IBM, Magic Soft,


Write an operator overloading program to Overload ‘>’ operator so as to find greater among two instances of the class.

1 Answers  


What is advantage of inheritance?

0 Answers  


Why multiple inheritance is not allowed?

0 Answers  


Write a c++ program to display pass and fail for three student using static member function

0 Answers  


What is difference between polymorphism and inheritance?

0 Answers  


What is friend function?

12 Answers   Wipro,


Categories