封装
封装(英语:Encapsulation)是指,一种将[抽象] “抽象化 (计算机科学)”)性函数接口的实现细节部分包装、隐藏起来的方法。
封装的优点:
- 确保用户代码不会无意间破坏封装对象的状态
- 被封装的类的具体实现细节可以随时改变,无需调整用户级别的代码。
摘自《C++primer》
友元
采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口。但是,有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员,这时可以将这些函数定义为该函数的友元函数。除了友元函数外,还有友元类,两者统称为友元。友元的作用是提高了程序的运行效率(即减少了类型检查和安全性检查等都需要时间开销),但它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私有成员。
友元函数的定义方法:
在类里声明一个普通函数,在前面加上friend修饰,那么这个函数就成了该类的友元,可以访问该类的一切成员。
当一个函数需要访问两个不同类型对象的私有数据成员的时候,可以使用友元函数。有两种使用的方式:
- 该函数作为全域函数,在两个类中被宣告为友谊函数
- 作为一个类中的成员函数,在另一个类中被宣告为友谊函数
此外:
友元的声明只能在类定义的内部,但是具体位置不限。(一般,最好在类定义开始或者结束前的位置集中声明友元。)
举例说明:
#include <iostream>
using namespace std;
class Bezaa; // Forward declaration of class Bezaa in order for example to compile.
class Aazaa
{
private:
int a;
public:
Aazaa() { a = 0; }
void show(Aazaa& x, Bezaa& y);
friend void ::show(Aazaa& x, Bezaa& y); // declaration of global friend
};
class Bezaa
{
private:
int b;
public:
Bezaa() { b = 6; }
friend void ::show(Aazaa& x, Bezaa& y); // declaration of global friend
friend void Aazaa::show(Aazaa& x, Bezaa& y); // declaration of friend from other class
};
// Definition of a member function of Aazaa; this member is a friend of Bezaa
void Aazaa::show(Aazaa& x, Bezaa& y)
{
cout << "Show via function member of Aazaa" << endl;
cout << "Aazaa::a = " << x.a << endl;
cout << "Bezaa::b = " << y.b << endl;
}
// Friend for Aazaa and Bezaa, definition of global function
void show(Aazaa& x, Bezaa& y)
{
cout << "Show via global function" << endl;
cout << "Aazaa::a = " << x.a << endl;
cout << "Bezaa::b = " << y.b << endl;
}
int main()
{
Aazaa a;
Bezaa b;
show(a,b);
a.show(a,b);
}
则输出为:
Show via global function
Aazaa::a = 0
Bezaa::b = 6
Show via function member of Aazaa
Aazaa::a = 0
Bezaa::b = 6
友元类的定义方法:
举例:
class B {
friend class A; // A is a friend of B
private:
int i;
};
class A {
public:
A(B b) {
b.i = 0; // legal access due to friendship
}
};
友元类的性质:
- 不相似
- 不传递
- 不继承
- Friendships are not [symmetric]– if class
A
is a friend of classB
, classB
is not automatically a friend of classA
. - Friendships are not [transitive] – if class
A
is a friend of classB
, and classB
is a friend of classC
, classA
is not automatically a friend of classC
. - Friendships are not [inherited]– if class
Base
is a friend of classX
, subclassDerived
is not automatically a friend of classX
; and if classX
is a friend of classBase
, classX
is not automatically a friend of subclassDerived
.