Contents
I. The Meaning of Encapsulation
- Combine attributes and behaviors into a whole to represent real-world entities
- Control access to attributes and behaviors
1.1 When designing a class, attributes and behaviors are written together to represent an entity
Syntax: class 类名{ 访问权限: 属性/行为 };
The attributes and behaviors in a class are collectively called members. Attributes are also called member attributes or member variables, while behaviors are also called member functions or member methods.
//例1:设计一个圆类,求圆的周长
#include<iostream>
using namespace std;
const double PI = 3.1415926;
//class代表设计一个类,类后面紧跟着类名称
class Circle{
//访问权限
//公共权限
public:
//属性
int r;
//行为
double calcuPerimeter(){
return 2 * PI * r;
}
};
int main(){
//通过圆类,创建具体的圆对象
Circle c1;
//给圆对象的属性赋值
c1.r = 10;
cout << "Perimeter is " << c1.calcuPerimeter() << endl;
system("Pause");
return 0;
}
1.2 When designing a class, attributes and behaviors can be placed under different access levels for control
There are three access levels:
- public: public access; accessible from inside and outside the class
- protected: protected access; accessible from inside the class, but not from outside the class
- private: private access; accessible from inside the class, but not from outside the class
The main difference between protected and private access lies in inheritance: a subclass can access the protected members of its parent class, but not its private members.
II. The Difference Between struct and class
In C++, the only difference between struct and class is their default access level.
- struct: public access by default
- class: private access by default
Although structs can contain member functions, they rarely do. Therefore, struct declarations usually declare only member variables. Struct declarations generally do not include public or private access specifiers.
III. Setting Member Attributes to Private
- Setting all member attributes to private lets you control read and write access yourself
- For write access, you can validate the data
例:
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
//设置姓名
void setName(string name){
m_Name = name;
}
//获取姓名
string getName(){
return m_Name;
}
//设置性别
void setGender(string gender){
if(gender == "Male" or gender == "Female"){
m_Gender = gender;
}
else{
cout << "Wrong Gender!" << endl;
m_Gender = "Male";
return;
}
}
//获取性别
string getGender(){
return m_Gender;
}
//设置年龄
void setAge(int age){
m_Age = age;
}
private:
//姓名 可读可写
string m_Name;
//性别 可读可写 修改选项只能是 "Male" "Female"
string m_Gender;
//年龄 只写
int m_Age;
};
int main(){
Person p;
p.setName("Huffie");
cout << "Name:\t" << p.getName() << endl;
p.setGender("Male");
cout << "Gender:\t" << p.getGender() << endl;
p.setAge(21);
//cout << "年龄:" << p.getAge() << endl;
system("Pause");
return 0;
}
Reference: Dark Horse Programmer’s Meticulously Crafted Work | C++ Tutorial: Programming Starts at 0 and Advances to 1—Learning to Program Is No Longer Difficult Link: https://www.bilibili.com/video/BV1et411b73Z
![[C++ Notes] The Meaning of Encapsulation and the Difference Between Structs and Classes](https://img.mahaofei.com/img/20220410093451.png)
Comments