2

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?

Narender Parmar
  • 201
  • 2
  • 10
  • 4
    Easy: make every method private, unless it has to form part of the public API of that class to make that class useful. – David Arno Aug 03 '17 at 10:56
  • 1
    Take a look here https://stackoverflow.com/q/2620699/5934037 – Laiv Aug 03 '17 at 11:04
  • @David Arno- so does it mean I have to make every member private in the initial draft level of design, I m doing so wrong in my design from a long time and make every member public without knowing what to make private inside class. Only variables I assign under private tag, Thanks man!! :) – Narender Parmar Aug 03 '17 at 11:09
  • Possible duplicate of [Should a method do one thing and be good at it?](https://softwareengineering.stackexchange.com/questions/137941/should-a-method-do-one-thing-and-be-good-at-it) – gnat Aug 03 '17 at 11:10
  • @gnat - think the above question more or less defines the SRP in context of method example method add will only add and does not print, it does only one thing ie add, Correct me if i m wrong – Narender Parmar Aug 03 '17 at 11:19
  • this question is somewhat poorly worded - now after re-reading it one more time it looks like a closer duplicate would be [Why I need to make method of my class private, If I need to access it throughout my application?](https://softwareengineering.stackexchange.com/q/283716/31260) – gnat Aug 03 '17 at 11:28
  • 1
    Does this answer your question? [Why I need to make method of my class private, If I need to access it throughout my application?](https://softwareengineering.stackexchange.com/questions/283716/why-i-need-to-make-method-of-my-class-private-if-i-need-to-access-it-throughout) – BobDalgleish Jan 13 '20 at 13:35

4 Answers4

4

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.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179
  • 1
    The other use for private methods is to provide an abstraction. isEmpty() rather than size==0 – Nick Keighley Aug 03 '17 at 11:25
  • I have updated the question with code, can you update me the methods I keep in public are really public? like show_calculatorobv need GUI class to interact, so we put show_calculator in public? – Narender Parmar Aug 03 '17 at 11:31
  • @NarenderParmar, As your class is designed, it is hard to tell if all methods are correctly declared public, because your class seems to have multiple responsibilities (handling the display, performing calculations, showing about screens). – Bart van Ingen Schenau Aug 03 '17 at 11:40
  • @Bart van Ingen Schenau - okay cool!! so I need to split my class responsibility, thanks for updating – Narender Parmar Aug 03 '17 at 11:47
  • 1
    `A method should be made private if other classes would have no reason to know that the method exists.` and no reason to change it. Private methods also protects sensible code. If altering a method, the class miss its reason to change (SRP) , the access to such method should be as constrained as possible. – Laiv Aug 03 '17 at 14:21
2

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.

amon
  • 132,749
  • 27
  • 279
  • 375
0

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.

0

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.

gnasher729
  • 42,090
  • 4
  • 59
  • 119