encapsulation
encapsulation
The Significance of Encapsulation
Encapsulation is one of the three major features of object-oriented programming in C++.
The meaning of encapsulation:
- Encapsulate attributes and behaviors as a whole to represent things in life.
- Implement permission control over attributes and behaviors.
Encapsulation Significance One:
When designing classes, attributes and behaviors are written together to represent things.
Syntax: class 类名{ 访问权限: 属性 / 行为 };
Example 1: Design a Circle class and calculate its circumference.
Example code:
//圆周率
const double PI = 3.14;
//1、封装的意义
//将属性和行为作为一个整体,用来表现生活中的事物
//封装一个圆类,求圆的周长
//class代表设计一个类,后面跟着的是类名
class Circle
{
public: //访问权限 公共的权限
//属性
int m_r;//半径
//行为
//获取到圆的周长
double calculateZC()
{
//2 * pi * r
//获取圆的周长
return 2 * PI * m_r;
}
};
int main() {
//通过圆类,创建圆的对象
// c1就是一个具体的圆
Circle c1;
c1.m_r = 10; //给圆对象的半径 进行赋值操作
//2 * pi * 10 = = 62.8
cout << "圆的周长为: " << c1.calculateZC() << endl;
return 0;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Example 2: Design a Student class with attributes for name and student ID, allowing the name and student ID to be set, and enabling the display of the student's name and student ID.
Example 2 Code:
//学生类
class Student {
public:
void setName(string name) {
m_name = name;
}
void setID(int id) {
m_id = id;
}
void showStudent() {
cout << "name:" << m_name << " ID:" << m_id << endl;
}
public:
string m_name;
int m_id;
};
int main() {
Student stu;
stu.setName("德玛西亚");
stu.setID(250);
stu.showStudent();
return 0;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Significance of Encapsulation Two:
When designing classes, attributes and behaviors can be placed under different levels of access for better control.
There are three types of access permissions:
- public access
- Protected protection permission
- private access permission
Example:
//三种权限
//公共权限 public 类内可以访问 类外可以访问
//保护权限 protected 类内可以访问 类外不可以访问
//私有权限 private 类内可以访问 类外不可以访问
class Person
{
//姓名 公共权限
public:
string m_Name;
//汽车 保护权限
protected:
string m_Car;
//银行卡密码 私有权限
private:
int m_Password;
public:
void func()
{
m_Name = "张三";
m_Car = "拖拉机";
m_Password = 123456;
}
};
int main() {
Person p;
p.m_Name = "李四";
//p.m_Car = "奔驰"; //保护权限类外访问不到
//p.m_Password = 123; //私有权限类外访问不到
return 0;
}
Run/Observation Results: This section is syntax definition-focused, typically requiring compilation together with the calling code. Pay attention to the definition method and usage location.
The key difference between 'struct' and 'class' in C++ lies in default access control and inheritance behavior.
In a struct, members and inheritance are public by default. In a class, they are private by default. This is the only fundamental language-level distinction.
For example:
struct Point {
int x; // public by default
int y;
};
class Circle {
int radius; // private by default
public:
void draw();
};
In practice, developers often use:
- struct for simple data containers or when all members are intended to be public.
- class for more complex objects that encapsulate state and provide methods, often with private data.
Both support inheritance, constructors, destructors, and other OOP features. The choice is largely conventional and stylistic.
In C++, the only difference between struct and class lies in different default access specifiers.
Difference:
- struct defaults to public access.
- class defaults to private access
class C1
{
int m_A; //默认是私有权限
};
struct C2
{
int m_A; //默认是公共权限
};
int main() {
C1 c1;
c1.m_A = 10; //错误,访问权限是私有
C2 c2;
c2.m_A = 10; //正确,访问权限是公共
return 0;
}
Run/Observation Results: This section is syntax definition-focused, typically requiring compilation together with the calling code. Pay attention to the definition method and usage location.
Members are set to private.
Advantage 1: Setting all member attributes as private allows you to control access!
Advantage 2: regarding write permissions, we can validate the data.
Example:
class Person {
public:
//姓名设置可读可写
void setName(string name) {
m_Name = name;
}
string getName()
{
return m_Name;
}
//获取年龄
int getAge() {
return m_Age;
}
//设置年龄
void setAge(int age) {
if (age < 0 || age > 150) {
cout << "你个老妖精!" << endl;
return;
}
m_Age = age;
}
//情人设置为只写
void setLover(string lover) {
m_Lover = lover;
}
private:
string m_Name; //可读可写 姓名
int m_Age; //只读 年龄
string m_Lover; //只写 情人
};
int main() {
Person p;
//姓名设置
p.setName("张三");
cout << "姓名: " << p.getName() << endl;
//年龄设置
p.setAge(50);
cout << "年龄: " << p.getAge() << endl;
//情人设置
p.setLover("苍井");
//cout << "情人: " << p.m_Lover << endl; //只写属性,不可以读取
return 0;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Exercise 1: Design a Cube Class
Design the Cube class
Find the surface area and volume of a cube.
Determine whether two cubes are equal using global functions and member functions, respectively.

Exercise Case 2: Relationship Between a Point and a Circle
Design a Circle class and a Point class to calculate the relationship between a point and a circle.
