CS253: Software Development with C++

Fall 2019

Access

Show Lecture.Access as a slide show.

CS253 Access

Member access

public member access

class Foo {
  public:
    void za() { cout << "🍕"; }
    int n = 42;
};

int main() {
    Foo f;
    f.za();
    cout << f.n << '\n';
}
🍕42

A public data member is rarely the right thing to do, but it can be justified for a class that’s really just a struct in fancy clothing.

private member access

class Foo {
  private:
    void za() { cout << "🍕"; }
    int n = 42;
};

int main() {
    Foo f;
    f.za();
    cout << f.n << '\n';
}
c.cc:9: error: 'void Foo::za()' is private within this context
c.cc:3: note: declared private here
c.cc:10: error: 'int Foo::n' is private within this context
c.cc:4: note: declared private here

Only Foo can use private members.

protected member access

class Foo {
  protected:
    void za() { cout << "🍕"; }
    int n = 42;
};

int main() {
    Foo f;
    f.za();
    cout << f.n << '\n';
}
c.cc:9: error: 'void Foo::za()' is protected within this context
c.cc:3: note: declared protected here
c.cc:10: error: 'int Foo::n' is protected within this context
c.cc:4: note: declared protected here

Only Foo and its immediately derived classes can use protected members.

Inheritance access

public inheritance

class B {
  public:    void pub() {}
  private:   void priv() {}
  protected: void prot() {}
};
class D : public B {
  public:
    D() {
        pub();
        priv();
        prot();
    }
};
int main() {
    D d;
    d.pub();
    d.priv();
    d.prot();
}
c.cc: In constructor 'D::D()':
c.cc:10: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:10: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:17: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:18: error: 'void B::prot()' is protected within this context
c.cc:4: note: declared protected here

private inheritance

class B {
  public:    void pub() {}
  private:   void priv() {}
  protected: void prot() {}
};
class D : private B {
  public:
    D() {
        pub();
        priv();
        prot();
    }
};
int main() {
    D d;
    d.pub();
    d.priv();
    d.prot();
}
c.cc: In constructor 'D::D()':
c.cc:10: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:10: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:16: error: 'void B::pub()' is inaccessible within this context
c.cc:2: note: declared here
c.cc:16: error: 'B' is not an accessible base of 'D'
c.cc:17: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:17: error: 'B' is not an accessible base of 'D'
c.cc:18: error: 'void B::prot()' is protected within this context
c.cc:4: note: declared protected here
c.cc:18: error: 'B' is not an accessible base of 'D'

protected inheritance

class B {
  public:    void pub() {}
  private:   void priv() {}
  protected: void prot() {}
};
class D : protected B {
  public:
    D() {
        pub();
        priv();
        prot();
    }
};
int main() {
    D d;
    d.pub();
    d.priv();
    d.prot();
}
c.cc: In constructor 'D::D()':
c.cc:10: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:10: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:16: error: 'void B::pub()' is inaccessible within this context
c.cc:2: note: declared here
c.cc:16: error: 'B' is not an accessible base of 'D'
c.cc:17: error: 'void B::priv()' is private within this context
c.cc:3: note: declared private here
c.cc:17: error: 'B' is not an accessible base of 'D'
c.cc:18: error: 'void B::prot()' is protected within this context
c.cc:4: note: declared protected here
c.cc:18: error: 'B' is not an accessible base of 'D'

Summary

public inheritance
Used for is-a relationships. The derived class gets the base class methods.
private inheritance
A sort of secret has-a relationship. The derived class gets the base class methods, but they’re private. It might be better to declare the base class as a private data member, instead.
protected inheritance
Used only by the confused, the desperate, and the wicked.