Last active
September 28, 2018 13:11
-
-
Save drodil/70838a30d39de20537c86b15e5ff6200 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
class BaseClass { | |
public: | |
void say_hello() { | |
std::cout << "Hello"; | |
} | |
protected: | |
void say_world() { | |
std::cout << "world!"; | |
} | |
}; | |
class PublicInheritance : public BaseClass { | |
}; | |
class ProtectedInheritance : protected BaseClass { | |
}; | |
class PrivateInheritance : private BaseClass { | |
public: | |
void say_hello_world() { | |
say_hello(); | |
say_world(); | |
} | |
}; | |
class TestProtectedClass : public ProtectedInheritance { | |
public: | |
void say_hello_world() { | |
say_hello(); | |
say_world(); | |
} | |
}; | |
class TestPrivateClass : public PrivateInheritance { | |
public: | |
void say_hello_sam() { | |
// This will not be OK as also protected functions and members | |
// will become private due to private inheritance | |
// say_hello(); | |
} | |
}; | |
int main() | |
{ | |
PublicInheritance pub; | |
pub.say_hello(); // OK | |
ProtectedInheritance prot; | |
// This will not be OK as say_hello will become protected function | |
// because of protected inheritance being used. | |
// error: 'void BaseClass::say_hello()' is inaccessible within this context | |
// prot.say_hello(); | |
PrivateInheritance priv; | |
// This will not be OK as say_hello will become private function | |
// because of private inheritance being used. | |
// error: 'void BaseClass::say_hello()' is inaccessible within this context | |
// priv.say_hello(); | |
// This will be OK as the say_hello is private function due to | |
// private inheritance so inheriting class can access it | |
priv.say_hello_world(); | |
TestProtectedClass clazz; | |
// This will be OK as the say_hello is protected function due to | |
// protected inheritance so inheriting class can access it | |
clazz.say_hello_world(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment