Cplusplus 简明教程
Data Encapsulation in C++
所有 C++ 程序都由以下两个基本元素组成:
All C++ programs are composed of the following two fundamental elements −
-
Program statements (code) − This is the part of a program that performs actions and they are called functions.
-
Program data − The data is the information of the program which gets affected by the program functions.
封装是一个面向对象编程概念,它将数据和操作该数据的函数绑定在一起,并使这两者免受外部干扰和误用。数据封装导致了 data hiding 的重要 OOP 概念。
Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.
Data encapsulation 是捆绑数据及其使用函数的机制,而 data abstraction 是仅公开接口并向用户隐藏实现细节的一种机制。
Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
C++ 通过创建用户自定义类型(称为 classes )来支持封装和数据隐藏的属性。我们已经学习过一个类可以包含 private, protected * and *public 成员。默认情况下,在类中定义的所有项都是私有的。例如:
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected * and *public members. By default, all items defined in a class are private. For example −
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
变量 length, breadth 和 height 属于 private 。这意味着只有 Box 类中的其他成员才能访问它们,而不能通过程序中的任何其他部分访问。这是实现封装的一种方法。
The variables length, breadth, and height are private. This means that they can be accessed only by other members of the Box class, and not by any other part of your program. This is one way encapsulation is achieved.
要使类的某些部分变为 public (即,可由程序中的其他部分访问),则必须在 public 关键字之后声明它们。在 public 说明符之后定义的所有变量或函数均可通过程序中的所有其他函数访问。
To make parts of a class public (i.e., accessible to other parts of your program), you must declare them after the public keyword. All variables or functions defined after the public specifier are accessible by all other functions in your program.
将一个类设为另一个类的友元会公开实现细节并减少封装。理想情况下,应尽可能使一个类中的尽可能多的细节对其他所有类隐藏。
Making one class a friend of another exposes the implementation details and reduces encapsulation. The ideal is to keep as many of the details of each class hidden from all other classes as possible.
Data Encapsulation Example
任何实现具有公共和私有成员的类的 C++ 程序都是数据封装和数据抽象的示例。考虑以下示例 −
Any C++ program where you implement a class with public and private members is an example of data encapsulation and data abstraction. Consider the following example −
#include <iostream>
using namespace std;
class Adder {
public:
// constructor
Adder(int i = 0) {
total = i;
}
// interface to outside world
void addNum(int number) {
total += number;
}
// interface to outside world
int getTotal() {
return total;
};
private:
// hidden data from outside world
int total;
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() <<endl;
return 0;
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Total 60
上述类将数字相加并返回总和。公共成员 addNum 和 getTotal * are the interfaces to the outside world and a user needs to know them to use the class. The private member *total 是对外界隐藏的,但类需要它们才能正常运行。
Above class adds numbers together, and returns the sum. The public members addNum and getTotal * are the interfaces to the outside world and a user needs to know them to use the class. The private member *total is something that is hidden from the outside world, but is needed for the class to operate properly.
Designing Strategy
大多数人都学会了默认情况下使类成员私有,除非我们确实需要公开它们。这只是一个很好的 encapsulation 。
Most of us have learnt to make class members private by default unless we really need to expose them. That’s just good encapsulation.
这最常应用于数据成员,但它同样适用于所有成员,包括虚拟函数。
This is applied most frequently to data members, but it applies equally to all members, including virtual functions.