C++ class encapsulation | class encapsulation

The separation of C++ public interface and private implementation

C++ implements encapsulation through classes. Data and operations related to these data are encapsulated in a class. In other words, the role of a class is to encapsulate data and algorithms in an abstract data type declared by the user. After a class is declared , The user mainly implements the functions provided by the class by calling public member functions

C++ public member functions are the public interface of the user-used class or the external interface of the class. Of course, it is not necessary to specify all member functions as public, but these member functions are not public interfaces.

Although private data members cannot be directly accessed outside the C++ class, private data members can be referenced or even modified by calling public member functions. Users can call public member functions to achieve certain functions, and these functions are specified when the class is declared. Users can use them and should not change them.

In fact, users often don't care about the details of how these functions are implemented, but only need to know what results will be obtained by calling which function, and what functions can be achieved.

C++ operating data members through member functions is called class implementation. In order to prevent users from arbitrarily modifying public member functions and changing operations on data, users are often prevented from seeing the source code of public member functions, and obviously cannot modify it. Users can only access the object code of public member functions.

The data manipulated in the class is private, and the details of the implementation are hidden from the user. This realization is called a private realization. The separation of the public interface and the private realization of the class forms information concealment.

If you want to modify or expand the functions of a class, you only need to modify the related data members of this class and the member functions related to it, and the parts outside the class in the program do not need to be modified.
If you find errors in reading and writing data in the class at compile time, you don't need to check the entire program, just check a few member functions in this class that access these data.

The member functions of C++ classes are called methods in object-oriented programming theory. Methods refer to operations on data. A method corresponds to an operation. Only when a method is declared as a public method can it be activated by the outside world of the object. Command to call related methods.

Case: C++ uses classes.

#include<iostream>//预处理
using namespace std;//命名空间 
class Time
{
    
    
  public: //声明以下是公用的
    int day;
    int hour;
    int minute; 
};
int main()//主函数 
{
    
    
  Time time1;//定义time1为Time类对象
  cout<<"输入天:"<<endl; 
  cin>>time1.day;
  cout<<"输入小时:"<<endl;
  cin>>time1.hour;
  cout<<"输入分钟:"<<endl;
  cin>>time1.minute;
  cout<<"写这篇文章是"<<time1.day<<"号,";
  cout<<time1.hour<<"点:";
  cout<<time1.minute<<"分"<<endl;
  return 0; //函数返回值为0; 
}

Compile and run results:

输入天:
21
输入小时:
20
输入分钟:
38
写这篇文章是21号,20点:38--------------------------------
Process exited after 9.871 seconds with return value 0
请按任意键继续. . .

Above, if you read and think it is helpful to you, please give Xiaolin a compliment, so Xiaolin also has the motivation to update, thank you fathers and villagers~ More cases of
C++ encapsulation
can go to the public account: C language Entry to master

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/113064118