Lab 11 - Coupling, Cohesion and Visibility

Example Code from Lecture

Access and visibility - slide 22

#include <iostream>
using namespace std;
 
class Sneaky {
    private:
        int safe;
    public:
        Sneaky() {
            safe = 10;
        };
 
        int &sorry() {
            return safe;
        }
 
        void print() {
            cout << safe << endl;
        };
};
 
int main() {
    Sneaky x;
    x.sorry() = 1;
    x.print();
 
    int y = x.sorry();
    y = 2;
    x.print();
 
    int &z = x.sorry();
    z = 3;
    x.print();
}

Exercises

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License