Encapsulation in c++ language

Encapsulation in c++ language

When developing complex systems, allowing indiscriminate access to an object’s internals can be disastrous. A malicious programmer may intentionally tweak one or more objects to sabotage the system.

C++ provides several ways to protect the internals of an object from the outside world, but the simplest strategy is the one we have been using: We can qualify fields and methods, generically referred to as class members, as either public or private.

incapsulation in cpp language
incapsulation in cpp language

Accessibility rules, also called visibility rules or permissions, determine what parts of a class and/or object are accessible to the outside world. C++ provides a great deal of flexibility in assigning access permissions, but some general principles exist that, if followed, foster programs that are easier to build and extend.

In general, fields should be private. Clients should not be able to arbitrarily change the state of an object. Allowing such might allow client code to put an object into an undefined state (for example, changing the denominator of a fraction to zero). An object’s state should only change as a result of calling the object’s methods.

Methods that provide a service to client code should be part of the public section of the class. The author of the class must ensure that the public methods cannot place the object into an illegal state. For example, consider the following method:

void set_denominator(int d) {
      denominator = d;
}

The class interface—the visible part. Clients see and can use the public parts of an object. The public methods and public variables of a class constitute the interface of the class. A class’s interface specifies what it does.

The class implementation—the hidden part. Clients cannot see any private methods or private variables. Since this private information is invisible to clients, class developers are free to do whatever they want with the private parts of the class. A class’s implementation specifies how it accomplishes what it needs to do.

#include <iostream>

class IntPoint {
    public:
        int x;
    int y;
    ntPoint(int x, int y): x(x), y(y) {}
};
class Rectangle {
    IntPoint corner; // Location of the rectangle's lower-left corner
    int width; // The rectangle's width
    int height; // The rectangle's width
    public:
        Rectangle(IntPoint pt, int w, int h):
        corner((pt.x < -100) ? -100 : (pt.x > 100 ? 100 : pt.x),
            (pt.y < -100) ? -100 : (pt.y > 100 ? 100 : pt.y)),
        width((w < 0) ? 0 : w), height((h < 0) ? 0 : h) {}
    int perimeter() {
        return 2 * width + 2 * height;
    }
    int area() {
        return width * height;
    }
    int get_width() {
        return width;
    }
    int get_height() {
        return height;
    }
    // Returns true if rectangle r overlaps this
    // rectangle object.
    bool intersect(Rectangle r) {
        // Details omitted
    }
    // Returns the length of a diagonal rounded to the nearest
    // integer.
    int diagonal() {
        // Details omitted
    }
    // Returns the geometric center of the rectangle with
    // the (x,y) coordinates rounded to the nearest integer.
    IntPoint center() {
        // Details omitted
    }
    bool is_inside(IntPoint pt) {
        // Details omitted
    }
};
int main() {
    Rectangle rect1(IntPoint(2, 3), 5, 7),
        rect2(IntPoint(2, 3), 1, 3),
        rect3(IntPoint(2, 3), 15, 3),
        rect4(IntPoint(2, 3), 5, 3);
    std::cout << rect1.get_width() << '\n';
    std::cout << rect1.get_height() << '\n';
    std::cout << rect2.get_width() << '\n';
    std::cout << rect2.get_height() << '\n';
    std::cout << rect3.get_width() << '\n';
    std::cout << rect3.get_height() << '\n';
    std::cout << rect4.get_width() << '\n';
    std::cout << rect4.get_height() << '\n';
    std::cout << rect1.get_perimeter() << '\n';
    std::cout << rect1.get_area() << '\n';
    std::cout << rect2.get_perimeter() << '\n';
    std::cout << rect2.get_area() << '\n';
    std::cout << rect3.get_perimeter() << '\n';
    std::cout << rect3.get_area() << '\n';
    std::cout << rect4.get_perimeter() << '\n';
    std::cout << rect4.get_area() << '\n';
}
ass Widget {
    int value;
    public:
        Widget();
    Widget(int v);
    int get();
    void bump();
};
Widget::Widget() {
    value = 40;
}
Widget::Widget(int v) {
    if (v >= 40)
        value = v;
    else
        value = 0;
}
int Widget::get() const {
    return value;
}
void Widget::bump() {
    if (value < 50)
        value++;
}
int main() {
    Widget w1, w2(5);
    std::cout << w1.get() << '\n';
    std::cout << w2.get() << '\n';
    w1.bump();
    w2.bump();
    std::cout << w1.get() << '\n';
    std::cout << w2.get() << '\n';
    for (int i = 0; i < 20; i++) {
        w1.bump();
        w2.bump();
    }
    std::cout << w1.get() << '\n';
    std::cout << w2.get() << '\n';
}