Problem
How to use private members of Base class in Subclasses without exposing them
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Access private members of base class
Created Date : 19-10-2013
Last Modified :
============================================================================
*/
#include <iostream>
using namespace std;
class A
{
private:
int x;
public:
A(int x) { this->x = x;}
int GetX() const {return x; }
friend class B;
};
class B: public A
{
public:
B(int x): A(x - 1) {}
void UseBasePrivateX(){
cout << A::x << endl;
}
};
int main()
{
B b(3);
b.UseBasePrivateX();
cout << b.GetX() << endl;
return 0;
}
Output
2
2
Press any key to continue . . .