#include <iostream>
using namespace std;
// Base class: demonstrating three access levels
class Base {
public:
int pubVar;
void pubFunc() {
cout << "Base::pubFunc() called" << endl;
}
protected:
int protVar;
void protFunc() {
cout << "Base::protFunc() called" << endl;
}
private:
int privVar;
void privFunc() {
cout << "Base::privFunc() called" << endl;
}
public:
// Constructor for initializing members
Base() : pubVar(100), protVar(200), privVar(300) {}
};
// 1. Public inheritance
class PublicDerived : public Base {
public:
void testAccess() {
// Inside derived class: can access base class public and protected members
cout << "Inside PublicDerived::testAccess()" << endl;
cout << "pubVar = " << pubVar << endl; // OK: public member
pubFunc(); // OK: public function
cout << "protVar = " << protVar << endl; // OK: protected member
protFunc(); // OK: protected function
// Illegal: base class private members are not accessible in derived class
// cout << privVar << endl; // Compilation error
// privFunc(); // Compilation error
}
// Provide additional public interface (already naturally inherits base class public members)
void callBasePublic() {
pubFunc(); // OK
}
};
// 2. Protected inheritance
class ProtectedDerived : protected Base {
public:
void testAccess() {
// Inside derived class: base class public and protected members become protected, still accessible
cout << "Inside ProtectedDerived::testAccess()" << endl;
cout << "pubVar = " << pubVar << endl; // OK: now equivalent to protected
pubFunc(); // OK: now equivalent to protected
cout << "protVar = " << protVar << endl; // OK: protected
protFunc(); // OK: protected
// Illegal: base class private members still not accessible
// cout << privVar << endl; // Compilation error
// privFunc(); // Compilation error
}
// Access base class protected members indirectly through public member function (exposed to outside)
void accessBasePublic() {
pubFunc(); // OK, outside can call through this public function
}
};
// 3. Private inheritance
class PrivateDerived : private Base {
public:
void testAccess() {
// Inside derived class: base class public and protected members become private, still accessible
cout << "Inside PrivateDerived::testAccess()" << endl;
cout << "pubVar = " << pubVar << endl; // OK: now equivalent to private
pubFunc(); // OK: now equivalent to private
cout << "protVar = " << protVar << endl; // OK: now equivalent to private
protFunc(); // OK: now equivalent to private
// Illegal: base class private members still not accessible
// cout << privVar << endl; // Compilation error
// privFunc(); // Compilation error
}
// Access base class public members indirectly through public member function (exposed to outside)
void callBasePublic() {
pubFunc(); // OK
}
};
// Additional demonstration: effect of multi-level inheritance on access rights
// Publicly inherit from a class that already uses public inheritance
class FromPublic : public PublicDerived {
public:
void testFurther() {
// PublicDerived publicly inherits from Base, so Base's public and protected remain unchanged
cout << "Inside FromPublic::testFurther()" << endl;
cout << "pubVar = " << pubVar << endl; // OK: public
pubFunc(); // OK: public
cout << "protVar = " << protVar << endl; // OK: protected
protFunc(); // OK: protected
// privVar still not accessible
}
};
// Publicly inherit from a class that uses protected inheritance (demonstrates persistence of permission reduction)
class FromProtected : public ProtectedDerived {
public:
void testFurther() {
// ProtectedDerived uses protected inheritance from Base, Base's public and protected become protected in ProtectedDerived
// Publicly inherit to FromProtected, these members remain protected
cout << "Inside FromProtected::testFurther()" << endl;
cout << "pubVar = " << pubVar << endl; // OK: because pubVar is protected in FromProtected
pubFunc(); // OK: protected
cout << "protVar = " << protVar << endl; // OK: protected
protFunc(); // OK: protected
}
};
// Main function: demonstrate member access under different inheritance modes
int main() {
cout << "========== Demonstration of Different Inheritance Permissions and Access Control ==========\n\n";
// ----- Public inheritance -----
cout << "--- Public inheritance ---" << endl;
PublicDerived pubObj;
// Access base class members through derived class object
cout << "Access public inheritance public member through object: pubVar = " << pubObj.pubVar << endl;
pubObj.pubFunc(); // OK: public member function
// pubObj.protVar; // Error: protected members cannot be accessed outside class
// pubObj.protFunc(); // Error
// pubObj.privVar; // Error: private members can never be accessed outside class
pubObj.testAccess(); // Call internal test function (legally access internal protected members)
cout << endl;
// ----- Protected inheritance -----
cout << "--- Protected inheritance ---" << endl;
ProtectedDerived protObj;
// After protected inheritance, all base class public/protected members become protected in derived class,
// therefore cannot be accessed directly through object
// protObj.pubVar; // Error: now pubVar is protected
// protObj.pubFunc(); // Error: pubFunc is protected
// protObj.protVar; // Error
// protObj.protFunc(); // Error
protObj.testAccess(); // OK: can access internally, output base class member values
protObj.accessBasePublic(); // OK: indirectly call base class public function through derived class public member function
cout << endl;
// ----- Private inheritance -----
cout << "--- Private inheritance ---" << endl;
PrivateDerived privObj;
// After private inheritance, all base class public/protected members become private in derived class,
// cannot directly access any base class members through object
// privObj.pubVar; // Error: private
// privObj.pubFunc(); // Error: private
privObj.testAccess(); // OK: can access internally
privObj.callBasePublic(); // OK: indirectly call base class public function through public member function
cout << endl;
// ----- Multi-level inheritance demonstration -----
cout << "--- Multi-level inheritance demonstration (public inheritance chain) ---" << endl;
FromPublic fromPubObj;
fromPubObj.testFurther(); // Can access public and protected members internally
fromPubObj.pubFunc(); // Still can call directly through object (public)
cout << endl;
cout << "--- Multi-level inheritance demonstration (public inheritance after protected inheritance) ---" << endl;
FromProtected fromProtObj;
fromProtObj.testFurther(); // Can access internally, but these members remain protected outside class
// fromProtObj.pubFunc(); // Error: pubFunc is protected in FromProtected, cannot access outside object
cout << endl;
cout << "========== Summary ==========" << endl;
cout << "1. Public inheritance: base class public members remain public, protected members remain protected.\n";
cout << "2. Protected inheritance: base class public and protected members become protected in derived class.\n";
cout << "3. Private inheritance: base class public and protected members become private in derived class.\n";
cout << "4. Regardless of inheritance type, base class private members can never be directly accessed in derived class.\n";
cout << "5. Inheritance type affects member visibility in subsequent derived classes (permissions may further reduce or remain).\n";
return 0;
}