Created
July 29, 2011 06:09
-
-
Save browny/1113253 to your computer and use it in GitHub Desktop.
[C++] Make protected/private inherited members public
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Base { | |
private: | |
int a; | |
int privMethod() { return a; } | |
public: | |
Base() { a = 0; b = 0;} | |
int b; | |
int pubMethod() { return a; } | |
}; | |
class Derived1 : protected Base { | |
public: | |
Derived1() : Base() {} | |
}; | |
class Derived2 : protected Base { | |
public: | |
Derived2() : Base() {} | |
//Base::a // Error | |
//Base::privMethod; // Error | |
Base::b; | |
Base::pubMethod; | |
}; | |
class Test { | |
public: | |
Derived1 derived1; | |
Derived2 derived2; | |
void test() { | |
//int b1 = derived1.b; // Error | |
//int b2 = derived1.pubMethod(); // Error | |
cout << derived2.b << endl; | |
cout << derived2.pubMethod() << endl; | |
} | |
}; | |
int main() { | |
Test test; | |
test.test(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment