OOP – Responsibilities of Private Methods in Class Design

language-agnosticobject-orientedobject-oriented-designooad

In different design books I have read that

  1. First identify the purpose of class (abstraction).
  2. Class should only do one thing (SRP).
  3. methods are defined as responsibility of the class.

As per my understanding public methods handle the responsibilities of the class which require other class [ie interacting classes]

My question is what responsibilites private members of the class do? and how to decide which method to be made private?

Have a look at my code

class calculator
 {
    private:
    long double operand_1;
    long double operand_2;
    long double result;

    int optr;
    int multiplier;

    Button One;
    Button Two;
    Button Three;
    Button Four;
public:
    calculator();

    const long double get_number_operator(const int=0);
    const long double calculate_number(const int [],const int);

    void show_calculator( );
    void calculations( );
    void clear_screen( );
    void show_back(const int);
    void show_about( );
    void show_time( );

The method in public are really public or we can put those in private?

Best Answer

Private members come in two flavours:

  • Private data members are used to store the internal state of the object
  • Private methods can be seen as named blocks of code that can be used by other methods of the class to fulfill their responsibility.
    Private methods are typically used when several methods need to do the exact same work as part of their responsibility (like notifying external observers that the object has changed), or when a method is split in smaller steps for readability.

A method should be made private if other classes would have no reason to know that the method exists.

Related Topic