Encapsulation in C++ - C++ 中的封装

Encapsulation in C++ - C++ 中的封装

1. Encapsulation in C++

Encapsulation is nothing new to what we have read. It is the method of combining the data and functions inside a class. Thus the data gets hidden from being accessed directly from outside the class. This is similar to a capsule where several medicines are kept inside the capsule thus hiding them from being directly consumed from outside. All the members of a class are private by default, thus preventing them from being accessed from outside the class.
封装对于我们阅读的内容而言并不是什么新鲜事物。它是将数据和函数合并到类中的方法。这样就隐藏了数据,使其无法从类外部直接访问。这类似于胶囊,其中几种药物被保存在胶囊内,从而使它们不被直接从外部消耗。默认情况下,类的所有成员都是私有的,因此可以防止从类外部访问它们。

encapsulation [ɪnˌkæpsjuˈleɪʃn]:n. 封装,包装
capsule [ˈkæpsjuːl]:n. 胶囊,蒴果,太空舱,小容器 adj. 压缩的,概要的 vt. 压缩,简述

2. Encapsulation

Encapsulation is necessary to keep the details about an object hidden from the users of that object. Details of an object are stored in its data members. This is the reason we make all the member variables of a class private and most of the member functions public. Member variables are made private so that these cannot be directly accessed from outside the class (to hide the details of any object of that class like how the data about the object is implemented) and so most member functions are made public to allow the users to access the data members through those functions.
必须进行封装以使有关对象的详细信息对用户隐藏。对象的详细信息存储在其数据成员中。这就是为什么我们将一个类的所有成员变量设为私有,而大多数成员函数设为公共的原因。将成员变量设为私有,这样就无法从类外部直接访问这些成员变量 (以隐藏该类的任何对象的详细信息,例如有关该对象的数据的实现方式),因此大多数成员函数都被公开以允许用户使用通过这些功能访问数据成员。

For example, we operate a washing machine through its power button. We switch on the power button, the machine starts and when we switch it off, the machine stops. We don’t know what mechanism is going on inside it. That is encapsulation.
例如,我们通过电源按钮操作洗衣机。我们打开电源按钮,机器启动,当我们关闭电源时,机器停止。我们不知道其中发生了什么机制。那就是封装。

3. Benefits of Encapsulation

There are various benefits of encapsulated classes.
封装类有很多好处。

  • Encapsulated classes reduce complexity.
    封装的类降低了复杂性。

  • Help protect our data. A client cannot change an Account’s balance if we encapsulate it.
    帮助保护我们的数据。如果我们封装它,则客户无法更改其余额。

  • Encapsulated classes are easier to change. We can change the privacy of the data according to the requirement without changing the whole program by using access modifiers (public, private, protected). For example, if a data member is declared private and we wish to make it directly accessible from anywhere outside the class, we just need to replace the specifier private by public.
    封装的类更容易更改。通过使用访问修饰符 (public, private, protected),我们可以根据需要更改数据的隐私,而无需更改整个程序。例如,如果数据成员被声明为私有,而我们希望使其可以从类外部的任何地方直接访问,则只需将说明符替换为公共。

Let’s see an example of Encapsulation.
让我们看一个封装的例子。

//============================================================================
// Name        : std::class
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

class Rectangle
{
	int length;
	int breadth;
public:
	void setDimension(int l, int b)
	{
		length = l;
		breadth = b;
	}
	int getArea()
	{
		return length * breadth;
	}
};

int main()
{
	Rectangle rt;
	rt.setDimension(7, 4);
	cout << rt.getArea() << endl;

	return 0;
}

Output

28

The member variables length and breadth are encapsulated inside the class Rectangle. Since we declared these private, so these variables cannot be accessed directly from outside the class. Therefore , we used the functions setDimension and getArea to access them.
成员变量 lengthbreadth 封装在 Rectangle 类中。由于我们声明了这些私有,因此无法从类外部直接访问这些变量。因此,我们使用了 setDimensiongetArea 函数来访问它们。

inheritance [ɪnˈherɪtəns]:n. 继承,遗传,遗产
class:类
derived class:继承类,派生类
subclass:子类
base class:基类
superclass:超类,父类
passion [ˈpæʃn]:n. 激情,热情,酷爱,盛怒
muscle [ˈmʌsl]:n. 肌肉,力量 vt. 加强,使劲搬动,使劲挤出 vi. 使劲行进

References

https://www.codesdope.com/cpp-initialization-list/

发布了473 篇原创文章 · 获赞 1762 · 访问量 104万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104475649