Skip to content

Wiki

Packaging

About 5 min read

Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.Encapsulation

1.1.The Significance of Encapsulation

Encapsulation is one of the three major features of C++ object-oriented programming.

Significance of encapsulation:

  • Take attributes and behaviors as a whole to represent things in life
  • Apply access control to attributes and behaviors

Encapsulation significance one:

​ When designing a class, attributes and behaviors are written together to represent things.

Syntax: class 类名{ 访问权限: 属性 / 行为 };

**Example 1:** Design a circle class to calculate the circumference of a circle

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;
}

Operation result:

圆的周长为: 62.8

**Example 2:** Design a student class with attributes for name and student ID; it should be possible to assign values to the name and student ID, and to display 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;
}

Operation result:

name:德玛西亚 ID:250

Encapsulation significance two:

When designing a class, attributes and behaviors can be placed under different access levels to control them.

There are three types of access permissions:

  1. public Public permission
  2. protected Protected permission
  3. private Private 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 result: The program ended normally, with no output in the terminal.

1.2.Differences between struct and class

In C++, the onlydifferencebetween struct and class is that the default access permissions are different

Difference:

  • Default permission for struct is public
  • Default permission for class is private
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;
}

运行结果: 编译失败:代码在类外访问了私有成员 m_A,编译器会报告访问权限错误。

1.3.Set member attributes to private

**Advantage 1:** All member attributes are set to private, allowing you to control read and write permissions yourself

**Advantage 2:** For write permissions, we can detect the validity of 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;
}

Operation result:

姓名: 张三
年龄: 50

Practice Case 1: Design a Cube Class

Design a cube class (Cube)

Find the surface area and volume of the cube

Use a global function and a member function respectively to determine whether two cubes are equal.

1545533548532

Practice Case 2: Relationship Between a Point and a Circle

Design a Circle class (Circle) and a Point class (Point) to calculate the relationship between a point and a circle.

1545533829184