封裝不單純隻是提供一個封皮,封裝的是資料抽象出的屬性與行為,封裝提供了通路控制,可以實作資料隐藏與通路接口,封裝提供了作用域與命名空間的界定。封裝讓資料的顆粒度變得更大,有利于程式的積木式搭建。當然,封裝也是繼承與多态的基石。
class ClassName // member encapsulation, data hiding through access level
// a base address, name expanding, namespace, scope
{ // {}形成封裝,有資料隐藏,有接口
private: // private與{}一起形成資料隐藏,{}外面的部分(包括其子類)無法通路{}内的pvivate部分
// (可以被内部的部分通路)
// but not private for friends of functions or classes
// Declarations of private
// members appear here.
protected: // 本類、子類成員函數、友元可以通路,此外,外部不能被通路
public: // public部分形成封裝的接口,與外界互動
// Declarations of public
// members appear here.
// static members // 由類名即可以通路,類名相當于命名空間或辨別符擴充(name expanding)
// virtual functions // 被繼承重載時可擴充
// abstract class(virtual and "=0" notation) forms interface
// friend ostream &operator << (ostream &, const className &);
}; // 盡量暴露方法,不要暴露資料
// static data member must be defined outside the class declaration
// Separating a class into specification and implementation files
// 在外定義成員需使用作用域限定操作符(scope resolution operator)::限定成員名稱
// ReturnType ClassName :: functionName ( ParameterList ){} // member name extending
示意圖:
一個簡單執行個體:
class Rectangle
{
private:
double width;
double length;
static int counting;
public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
friend ostream &operator << (ostream &, const Rectangle &);
};
int Rectangle::counting = 0;
不同繼承方式形成的通路權限:
執行個體:
#include <stdio.h>
class A{
private:
int x;
protected:
int y;
public:
int z;
A(int a,int b,int c){
x=a;
y=b;
z=c;
}
};
class B:private A{
private:
int p;
public:
B(int a,int b,int c,int d):A(a,b,c){p=d;}
int sum(){
return y+z;
}
};
main()
{
A a(1,2,3);
printf("%d\n",a.z); // 3
B b(1,2,3,4);
printf("%d\n",b.sum());// 5
//printf("%d\n",b.z);
getchar();
}
類封裝形成的資料隐藏可以保證資料的安全性、完整性。在保證接口的穩定性後, 當外部隻通路接口時,接口實作的改變以及private部分的變更,都無須變更已使用該類接口的代碼。
-End-