20-CS-122-001 Computer Science II Spring 2012
Visibility

Virtual functions, classes, inheritance, lists, queues, stacks, applications

visib.cc

#include <iostream.h>

class A {
   int a;
protected:
   int b;
public:
   int c;

   A (int a, int b, int c) { this->a = a; this->b = b; this->c = c; }
};

class B : public A {
public:
   B (int a, int b, int c) : A(a,b,c) {}

   void fb() { 
      //cout << a << " "; // No good, a is private
      cout << b << " ";
      cout << c << " ";
      cout << "\n";
   }
};

class I : private B {
public:
   I (int a, int b, int c) : B(a,b,c) {}

   void fi() { 
      //cout << a << " "; // No good, a is private
      cout << b << " ";
      cout << c << " ";
      cout << "\n";
   }
};

class C : protected A {
public:
   C (int a, int b, int c) : A(a,b,c) {}

   void fc() { 
      //cout << a << " "; // No good, a is private
      cout << b << " ";
      cout << c << " ";
      cout << "\n";
   }
};

class F : public C {
public:
   F (int a, int b, int c) : C(a,b,c) {}

   void ff() { 
      //cout << a << " "; // No good, a is private
      cout << b << " ";
      cout << c << " ";
      cout << "\n";
   }
};

class G : protected C {
public:
   G (int a, int b, int c) : C(a,b,c) {}

   void fg() { 
      //cout << a << " "; // No good, a is private
      cout << b << " ";
      cout << c << " ";
      cout << "\n";
   }
};

class H : private C {
public:
   H (int a, int b, int c) : C(a,b,c) {}

   void fh() { 
      //cout << a << " "; // No good, a is private
      cout << b << " ";
      cout << c << " ";
      cout << "\n";
   }
};

class D : private A {
public:
   D (int a, int b, int c) : A(a,b,c) {}

   void fd() { 
      //cout << a << " "; // No good, a is private
      cout << b << " ";
      cout << c << " ";
      cout << "\n";
   }
};

class E : public D {
public:
   E (int a, int b, int c) : D(a,b,c) {}

   void fe() { 
      //cout << a << " "; // No good, a is private
      //cout << b << " "; // No good, b is protected
      //cout << c << " "; // No good, c is inaccessible
      cout << "\n";
   }
};

int main () {
   A a(1, 2, 3);
   //cout << a.a << " "; // No good, a is private
   //cout << a.b << " "; // No good, b is protected
   cout << a.c << " ";
   cout << "\n";
   B b(1, 2, 3);
   //cout << b.a << " "; // No good, a is private
   //cout << b.b << " "; // No good, b is protected
   cout << b.c << " ";
   cout << "\n";
   b.fb();
   C c(1, 2, 3);
   //cout << c.a << " "; // No good, a is private
   //cout << c.b << " "; // No good, b is protected
   //cout << c.c << " "; // No good, c is inaccessible
   cout << "\n";
   c.fc();
}