Class Data Encapsulation(private data) in operator overloading

前端 未结 5 1388
无人及你
无人及你 2021-01-23 12:19

Below is the code

The Code:

#include 
using namespace std;

class Rational {
  int num;  // numerator
  int den;  // den         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-23 12:28

    While the other posters have explained how the C++ access specifier work, no one has explained why they work in this way.

    Increasing encapsulation is all about minimizing the amount of code which has access to your object internals (i.e. your data members), not the number of objects. If the access specifiers limited access to the internals of other objects within the same class, this would not increase encapsulation.

    Encapsulation is important because it means that changing implementation details will affect a minimal amount of code. Increasing encapsulation increases the maintainability of code. It is a build-time, not a runtime concept.

提交回复
热议问题