GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 177 of 203    Print  
Access to Private Base Class Data
To allow derived class functions to access private base class data, what should you do?
A. You should use a private base class function.
B. You should use a public or protected base class function.
C. You should not make the base class data private.
D. You should do nothing; the derived class has direct access.
E. You should do nothing; it is not possible for a derived class to access the functions of a base class.



  
Total Answers and Comments: 6 Last Update: October 19, 2009     Asked by: prettyfox 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: iftikharahmad
 
B. You should use a public or protected base class function.

Above answer was rated as good by the following members:
Peter_APIIT
September 13, 2008 08:56:46   #1  
JunhuiCHEN Member Since: September 2008   Contribution: 1    

RE: Access to Private Base Class Data
E. but friend should be one solution.
 
Is this answer useful? Yes | No
September 29, 2008 02:05:38   #2  
hrisheekesh.kale27 Member Since: September 2008   Contribution: 4    

RE: Access to Private Base Class Data
E. but making the base class data protected is a good solution.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
October 24, 2008 16:19:04   #3  
iftikharahmad Member Since: October 2008   Contribution: 1    

RE: Access to Private Base Class Data
B. You should use a public or protected base class function.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
December 07, 2008 09:04:22   #4  
burraganesh Member Since: December 2008   Contribution: 20    

RE: Access to Private Base Class Data
Make the derived class friend of base class
 
Is this answer useful? Yes | No
July 17, 2009 00:25:25   #5  
Peter_APIIT Member Since: April 2008   Contribution: 2    

RE: Access to Private Base Class Data
Protected Inheritance is the way to go.


 
Is this answer useful? Yes | No
October 18, 2009 22:50:41   #6  
j_l_larson Member Since: June 2008   Contribution: 4    

RE: Access to Private Base Class Data
F. None of the above are entirely the right answer

You can provide a method in the base class to allow the private data to be read or written and then provide an access specifier in the derived class which allows the methods to be called. For example:

class Base {
public:
Base() : m_nDerivable(1) m_nSecret(0) {}
~Base() {}
int ReadBasePrivateData() { return m_nSecret; }
void ModifyBasePrivateData( int n ) { m_nSecret n; }
int foo() { return m_nSecret + m_nDerivable; }
protected:
int bar() { return m_nSecret + m_nDerivable; }
int m_nDerivable;
private:
int woozle() { return m_nSecret + m_nDerivable; } // can never be called even with access specifiers
int m_nSecret;
};

class PrivDeriv : private Base {
public:
PrivDeriv() {}
~PrivDeriv() {}
Base::ReadBasePrivateData; // access specifier we are allowed to read base's private things
Base::ModifyBasePrivateData; // access specifier we are allowed to write over base's private things
Base::foo; //public member allowed
//Base::bar; //protected member can be allowed but we selectively block it here
//Base::woozle; //private can not be allowed - compiler error
//int GetBasePrivateInt() { std::cout << "private derived get base private membern"; return Base::m_nSecret; } // compiler error
int GetBaseProtectedInt() { std::cout << "private derived get base protected membern"; return Base::m_nDerivable; }
private:
};

int main( int argc char** argv ) {
PrivDeriv a;
a.foo(); // allowed
//a.bar(); // compiler error
//a.woozle(); // compile error
int nPrivateInt a.ReadBasePrivateData();
std::cout << "private int is allowed through public read method: " << nPrivateInt << "n";
a.ModifyBasePrivateData( 10 ); // inaccessible compiler error
nPrivateInt a.ReadBasePrivateData();
std::cout << "private int has been changed through public write method: " << nPrivateInt << "n";
return 0;
}


 
Is this answer useful? Yes | No


 
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