base-class-access/base-class-access-1a.cc - Class D inherits privately from B and publically from C. | Lecture 5 - slide 27 : 40 Program 1 |
// A class D with both a private and public base class. // A very simple illustration of the practical consequences. #include <iostream> #include <string> using namespace std; class B { private: int b; public: B(int b): b(b){} void Bop(){ // Bop is public in B cout << "Bop()" << endl; } }; class C { private: int c; public: C(int c): c(c){} void Cop(){ // Cop is public in C cout << "Cop()" << endl; } }; class D : private B, public C { private: int d; public: D(int b, int c, int d): B(b), C(c), d(d){} void Dop(){ // Dop is public in D Bop(); // OK! Bop used inside D. Cop(); // OK Cop is or course visible. } }; int f(D &aD){ // aD.Bop(); // Compile error: void B::Bop() is inaccessible in aD's client interface aD.Cop(); // Output: Cop() aD.Dop(); // Output: Bop() Cop() } int main(){ D d(1,2,3); f(d); }