生活随笔
收集整理的這篇文章主要介紹了
Part5 数据的共享与保护 5.4类的友元5.5共享数据的保护
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
友元是C++提供的一種破壞數據封裝和數據隱藏的機制。 通過將一個模塊聲明為另一個模塊的友元,一個模塊能夠引用到另一個模塊中本是被隱藏的信息。 可以使用友元函數和友元類。 為了確保數據的完整性,及數據封裝與隱藏的原則,建議盡量不使用或少使用友元。
?
友元函數: 1 友元函數是在類聲明中由關鍵字friend修飾說明的非成員函數,在它的函數體中能夠通過對象名訪問 private 和 protected成員 2 作用:增加靈活性,使程序員可以在封裝和快速性方面做合理選擇。 3 訪問對象中的成員必須通過對象名。
// 5-6友元函數
#include<iostream>
#include <cmath>
using namespace std;
class Point{
public :Point( int x =
0 ,
int y =
0 ):x(x), y(y){} int getX(){
return x;} int getY(){
return y;}friend float dist(Point &a, Point &
b);
private : int x,y;
};
float dist(Point &a, Point &
b){ double x = a.x -
b.x; double y = a.y -
b.y; return static_cast<
float >(sqrt(x*x+y*
y));
}
int main(){Point p1( 1 ,
1 ), p2(
3 ,
4 );cout <<
" The distance is: " ;cout << dist(p1,p2) <<
endl; return 0 ;
} ?
友元類: 1 若一個類為另一個類的友元,則此類的所有成員都能訪問對方類的私有成員。 2 聲明語法:將友元類名在另一個類中使用friend修飾說明。
class A{friend class B;
public : void display(){cout << x <<
endl;}
private : int x;
}
class B{
public : void set (
int i); void display();
private :A a; // A是B的組件
}
void B::
set (
int i){a.x =
i;
}
void B::display(){a.display();
} 類的友元關系是單向的: 如果聲明B類是A類的友元,B類的成員函數就可以訪問A類的私有和保護數據,但A類的成員函數卻不能訪問B類的私有、保護數據。
?
5.5共享數據的保護 對于既需要共享、又需要防止改變的數據應該聲明為常類型(用const進行修飾)。 對于不改變對象狀態的成員函數應該聲明為常函數。
常類型: 1 常對象:必須進行初始化,不能被更新。 const 類名 對象名 2 常成員:用const進行修飾的類成員:常數據成員和常函數成員 3 常引用:被引用的對象不能被更新。 const 類型說明符 &引用名 4 常數組:數組元素不能被更新 類型說明符 const 數組名[大小] 5 常指針:指向常量的指針
常對象例:
class A{
public :A( int i,
int j){x = i; y =
j;}
private : int x,y;
};
A const a(
3 ,
4 );
// a是常對象,不能被更新 ?
常成員:用const修飾的對象成員 1 常成員函數:使用const關鍵字說明的函數。 常成員函數不更新對象的數據成員。 const關鍵字可以被用于參與對重載函數的區分 通過常對象只能調用它的常成員函數。 2 常數據成員:使用const說明的數據成員。
// 5-7 常成員函數舉例
#include<iostream>
using namespace std;
class R{
public :R( int r1,
int r2):r1(r1),r2(r2){} void print(); void print()
const ;
private : int r1,r2;
};
void R::print(){cout << r1 <<
" : " << r2 <<
endl;
}
void R::print()
const {
// 編譯器會審查內部有沒有改變數據 cout << r1 <<
" : " << r2 <<
endl;
}
int main(){R a( 5 ,
4 );a.print(); // 調用void print() const R b (
20 ,
39 );b.print(); // 調用void print() const return 0 ;
} // 5-8 常數據成員舉例
#include<iostream>
using namespace std;
class A{
public : A( int i); void print();
private : const int a; static const int b;
// 靜態常數據成員
};
const int A::b =
10 ;
A::A( int i):a(i){}
// 常成員a只能在初始化列表中初始化,不能再函數體中賦值
void A::print(){cout << a <<
" : " << b <<
endl;
}
int main(){ // 建立對象a和b,并以100和0作為初值,分別調用構造函數, // 通過構造函數的初始化例表給對象的場數據成員賦初值 A a1(
100 ), a2(
0 );a1.print();a2.print(); return 0 ;
} ?
常引用: 如果在聲明引用時用const修飾,被聲明的引用就是常引用。 常引用所引用的對象不能被更新。 如果用常引用做形參,便不會意外地發生對實參的更改。
// 5-9常引用作形參
#include<iostream>
#include <cmath>
using namespace std;
class Point{
public :Point( int x =
0 ,
int y =
0 ):x(x),y(y){} int getX(){
return x;} int getY(){
return y;}friend float dist(
const Point &p1,
const Point &
p2);
private : int x,y;
};
float dist(
const Point &p1,
const Point &
p2){ double x = p1.x -
p2.x; double y = p1.y -
p2.y; return static_cast<
float >(sqrt(x*x+y*
y));
}
int main(){ const Point myp1(
1 ,
1 ),myp2(
4 ,
5 );cout <<
" The distance is: " ;cout << dist(myp1,myp2) <<
endl; return 0 ;
} ?
轉載于:https://www.cnblogs.com/leosirius/p/7967908.html
總結
以上是生活随笔 為你收集整理的Part5 数据的共享与保护 5.4类的友元5.5共享数据的保护 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。