Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

The public members are the interface through which the outside world interacts with this object.

The private members are implementation details that describe how a class fulfils its purpose. Since they are private they do not affect how other code interacts with our class and are therefore not part of a larger design. The private members are used to implement the public methods. So basically: any member you create should be private if it is not needed for the external interface of that class.

Sometimes private methods implement some functionality that could be modelled as a responsibility in its own right, could be re-used in another context, or is so complex that you want to unit-test it directly. It can then make sense to extract these methods into free functions or into a class of its own. You often end up with a number of utility classes that have nothing to do directly with the core business logic of your software, but are needed by that business logic to get the job done. For example: methods for loading data from files, abstract algorithms, or adapters to other systems. Quite often, you can use existing libraries for these tasks.

If you want either inherit these methods in derived class or reuse by other classes then declare them as Public. And if you protect it from being used by any other class then declare them as Private. Because methods that are private can only be called by methods within the same class or within the same "module". Methods are not commonly made private; usually they're made protected so that children can call them, or public so that other code can call them.

If someone else needs to call a method, it must be public. If nobody else needs to call it, you make it private. And the opposite of course: Only code inside your class can call private methods; anyone can call public methods.

Imagine your boss tells you what your class needs to be able to do, and how and with which interface this functionality is called. That gives you your public methods. Everything else is private.

Licensed under: CC-BY-SA with attribution
scroll top