Data encapsulation, data abstraction

Data encapsulation is a mechanism to function data and operation data bundled together, data abstraction is an interface to a user and is exposed only to hide the implementation details of the mechanism. The class is defined as a friend of another class, the implementation details will be exposed, thereby reducing the encapsulation. Ideally Foreign hide implementation details of each class as much as possible.

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  class Student
 . 4  {
 . 5  public :
 . 6      Student ( Double cmath, Double cChiness, Double cEnglish)      // parameter argument and not, as otherwise it will not cause initialization 
. 7      {
 . 8          Math = cmath;
 . 9          chiness = cChiness;
 10          Dictionary Dictionary English = cEnglish;
 . 11          SUM = 0.0 ;
 12 is      }
 13 is      DoubleThe GetInfo ( String name)      // external interface, can function only needs to know the effect, you do not need to know how to achieve 
14      {
 15          GetAchievement (name);
 16          return SUM;
 . 17      }
 18 is      Double the GetInfo ( String name, String PV)       / / overloaded function 
. 19      {
 20 is          GetAchievement (name);
 21 is          return SUM / . 3 ;
 22 is      }
 23 is  Private :
 24      Double Math = 0 ;
 25      Double= chiness 0 ;
 26 is      Double Dictionary Dictionary English = 0 ;
 27      Double SUM = 0.0 ;
 28      void GetAchievement ( String name)     // function invisible to the user, data abstraction 
29      {
 30          IF (name == " Tim " )
 31 is          {
 32              SUM = Math chiness + + Dictionary Dictionary English;
 33 is          } the else {
 34 is              SUM = - . 1 ;
 35          }
 36      }
37 };
38 
39 int main()
40 {
41     Student std(97, 86, 85);
42     cout << std.GetInfo("Tim") << endl;
43     cout << std.GetInfo("Tim", "pv") << endl;;
44     system("pause");
45     return 0;
46 }

 Results:  268 89.3333 

Reproduced in: https: //www.cnblogs.com/xiaodangxiansheng/p/10999886.html

Guess you like

Origin blog.csdn.net/weixin_34217773/article/details/93210240