当前位置:网站首页>Classes and objects -- encapsulation
Classes and objects -- encapsulation
2022-06-13 09:25:00 【Hezeze】
C++ Treat everything as an object , Objects with the same properties belong to a class .
The meaning of encapsulation is : Treat attributes and behaviors as a whole
And C The middle structure is similar to .
A class is divided into three parts : Access right ; attribute ; Behavior
You need to create the corresponding object in the main function
Attributes and behaviors are also collectively referred to as members
attribute = Member attribute = Member variables Behavior = Member functions = Member method
class student {
public: // Access right
string s_name; // attribute
string s_number;
void writname(string n_name){
s_name=n_name;
}
void writnum(string n_num){
s_number=n_num; // Behavior
}
void showstu() {
cout <<" full name :"<<s_name<<endl;
cout <<" Student number :"<<s_number<<endl;
}
};
int main() {
student a1;
a1.writname(" Bloom ha ");
a1.writnum("18203154");
a1.showstu();
return 0;
}
The second meaning of encapsulation : Access right
The permissions of all members are divided into three categories
- Public authority public Member classes can access , You can access
- Protection rights protected Member classes can access , No access outside of class ( Then inheritance will learn ) The son can visit
- Private rights private Member classes can access , No access outside of class ( The son can't visit )
class And struct
struct The default permission for is public
class The default permission for is private
Privatizing member properties is a bit :
- You can control the read and write permissions by yourself
- Write permission can detect the validity of data
This is a struct Structural variables are incomparable ,struct Anyone can modify all the variables at will , And the operation on the structure needs to rewrite a function , Complicate the code .
边栏推荐
- C language: Address Book
- JUC 原子累加器
- SQL ROW_ The number() function uses
- Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
- A static variable is associated with a class and can be used as long as the class is in memory (the variable does not exist as long as your application terminates). (heap body, stack reference)
- 攻防世界PWN play 条件竞争漏洞的利用
- Online debugging tool Arthas Foundation
- LeetCode 剑指 Offer 30. 包含min函数的栈
- LeetCode 1143. 最长公共子序列
- Lecture par lots de tous les fichiers vocaux sous le dossier
猜你喜欢
1-2 24:00 (20 points) [CSP certification true question]
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
JUC atomic reference and ABA problem
(dp+ memory) acwing 901 skiing
Final principle
LeetCode 1. Sum of two numbers
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
acwing 790. The third root of a number (dichotomy)
C language: dynamic memory management
Jenkins接入Openldap用户认证
随机推荐
I have summarized the knowledge points of JS [intermediate and advanced] for you
BGP Federation +community
20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
C language: five custom types
JUC Unsafe
C language time difference calculation
Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
攻防世界-PWN-shell
1-4 message passing interface [CSP authentication]
Sort() sort function
Amadahl's Law (a little thought)
批量讀取文件夾下的全部語音文件
Solov2 source code analysis
Opencv face recognition of ros2 foxy~galactic~humble
20211108 det(AB)=det(A)det(B)
JUC Unsafe
(dp+ memory) acwing 901 skiing
LeetCode 343. 整数拆分
Collection of articles on virtualization and cloud computing
Instruction level parallelism (?)