GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 164 of 203    Print  
virtual key word
can access any private member of the base class through the derived class FUNCTION IF IT IS VIRTUAL?


  
Total Answers and Comments: 4 Last Update: December 07, 2008     Asked by: saquib_225 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
June 25, 2008 09:31:17   #1  
omkar1010 Member Since: June 2008   Contribution: 1    

RE: virtual key word
yes we can access it as public function
 
Is this answer useful? Yes | No
July 07, 2008 16:16:04   #2  
jezpops Member Since: July 2008   Contribution: 2    

RE: virtual key word
I dont understand what you want to do by this question. Usually the virtual keyword would be used in the base class and the method overridden in the derived class.

The virtual keyword ensures that the correct overridden function in the derived class is called rather than the function in the base class. It ensures only one instance of the object exists at runtime.

Since it only usually used in inheritance then methods of the base class would be accessible anyway (dependent on the access specifiers)

 
Is this answer useful? Yes | No
July 15, 2008 02:09:58   #3  
arun_goel3 Member Since: July 2008   Contribution: 2    

RE: virtual key word
using typecasting and virtual we can access the private member of base class

#include <iostream>

using namespace std;

class base

{

int i;virtual void update(int j)

{

cout << "in base update" << endl;

i j;

}

public :

base ( int ii 10) : i(ii) {}

void print()

{ cout << "value of i " << i << endl; }

virtual ~base() {}

};

class derived : public base

{

public :

int i;derived (

int i1 20 int i2 21) : base(i1) i(i2) {}virtual void update(int j)

{

cout << "in derived update" << endl;

i j;

}

};

void main()

{

base *b1 new base();

b1->print();

derived *d (derived *)b1;

d->update(30);

b1->print();

delete b1;

}


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
December 07, 2008 09:29:12   #4  
burraganesh Member Since: December 2008   Contribution: 20    

RE: virtual key word
No way
Ex:
#include<iostream>
using namespace std;
class base{
private:
int a;
public:
virtual void show()
{
cout<<a<<endl;
}
};
class derived:public base{
public:
void display()
{
cout<<a<<endl;//we must call show() here instead
}
};
int main()
{
derived ob;
ob.display();
return 0;
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape