freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

第10章創(chuàng)建功能更強的類型-資料下載頁

2025-07-20 07:10本頁面
  

【正文】 《 程序設(shè)計 》 程序設(shè)計 100 靜態(tài)成員函數(shù)使用說明 ? 靜態(tài)成員函數(shù)可定義為內(nèi)嵌的,也可在類外定義。在類外定義時,不用 static。 ? 靜態(tài)成員函數(shù)的訪問:可以通過類作用域限定符或通過對象訪問 類名 ::靜態(tài)成員函數(shù)名() 對象名 .靜態(tài)成員函數(shù)名() 《 程序設(shè)計 》 程序設(shè)計 101 靜態(tài)成員函數(shù)實例 1 class goods {int weight。 static int total_weight。 public: goods(int w)。 ~goods()。 int weight()。 static int totalweight()。 }。 int goods::total_weight = 0。 goods::goods(int w) {weight=w。 total_weight+=w。} goods::~goods() {total_weight=weight。} int goods::weight() {return weight。} int totalweight() {return total_weight。} 《 程序設(shè)計 》 程序設(shè)計 102 靜態(tài)成員函數(shù)實例 2 ? 在程序執(zhí)行的某個時刻,有時需要知道某個類已創(chuàng)建的對象個數(shù),現(xiàn)在仍存活的對象個數(shù)。 ? 類設(shè)計: ? 數(shù)據(jù)成員:為了實現(xiàn)這個功能,我們可以在類中定義兩個靜態(tài)的數(shù)據(jù)成員: obj_count和 obj_living。 ? 成員函數(shù):要實現(xiàn)計數(shù)功能,我們可以在創(chuàng)建一個對象時,對這兩個數(shù)各加 1。當(dāng)撤銷一個對象時,obj_living減 1。為了知道某一時刻對象個數(shù)的信息,可以定義一個靜態(tài)的成員函數(shù)返回這兩個值。 《 程序設(shè)計 》 程序設(shè)計 103 類定義 ifndef _StaticSample_h define _StaticSample_h include iostream using namespace std。 class StaticSample { private: static int obj_count。 static int obj_living。 public: StaticSample() {++obj_count。 ++obj_living。} ~StaticSample() {obj_living。} static void display() //靜態(tài)成員函數(shù) {cout 總對象數(shù): obj_count \t存活對象數(shù): obj_living endl。} }。 int StaticSample::obj_count = 0。 //靜態(tài)數(shù)據(jù)成員的定義及初始化 int StaticSample::obj_living = 0。 //靜態(tài)數(shù)據(jù)成員的定義及初始化 endif 《 程序設(shè)計 》 程序設(shè)計 104 StaticSample的應(yīng)用 include int main() {StaticSample::display()。 //通過類名限定調(diào)用成員函數(shù) StaticSample s1, s2。 StaticSample::display()。 StaticSample *p1 = new StaticSample, *p2 = new StaticSample。 ()。 //通過對象調(diào)用靜態(tài)成員函數(shù) delete p1。 p2display()。 //通過指向?qū)ο蟮闹羔樥{(diào)用靜態(tài)成員函數(shù) delete p2。 StaticSample::display()。 return 0。 } 《 程序設(shè)計 》 程序設(shè)計 105 執(zhí)行結(jié)果 總對象數(shù): 0 存活對象數(shù): 0 總對象數(shù): 2 存活對象數(shù): 2 總對象數(shù): 4 存活對象數(shù): 4 總對象數(shù): 4 存活對象數(shù): 3 總對象數(shù): 4 存活對象數(shù): 2 《 程序設(shè)計 》 程序設(shè)計 106 靜態(tài)的常量成員 ? 靜態(tài)的常量成員:整個類的所有對象的共享常量 ? 靜態(tài)的常量成員的聲明: static const 類型 數(shù)據(jù)成員名 = 常量表達(dá)式; ? 注意 const數(shù)據(jù)成員和 static const數(shù)據(jù)成員的區(qū)別。 《 程序設(shè)計 》 程序設(shè)計 107 靜態(tài)常量成員實例 ? 例如,需要在某個類中需要用到一個數(shù)組,而該數(shù)組的大小對所有對象都是相同的,則在類中可指定一個數(shù)組規(guī)模,并創(chuàng)建一個該規(guī)模的數(shù)組。如下所示: class sample { static const int SIZE = 10。 int storage[SIZE]。 … }。 《 程序設(shè)計 》 程序設(shè)計 108 老版本的兼容 ? 某些舊版本的 C++不支持靜態(tài)常量 ? 解決方法:用不帶實例的枚舉類型 ? 如: class sample { enum {SIZE = 10}。 int storage[SIZE]。 … }。 《 程序設(shè)計 》 程序設(shè)計 109 第 10章 創(chuàng)建功能更強的類型 ? 從面向過程到面向?qū)ο? ? 類的定義 ? 對象的使用 ? 對象的構(gòu)造與析構(gòu) ? 常量對象與 const成員函數(shù) ? 常量數(shù)據(jù)成員 ? 靜態(tài)數(shù)據(jù)成員與靜態(tài)成員函數(shù) ? 友元 《 程序設(shè)計 》 程序設(shè)計 110 友元 ? 類的私有成員只能通過它的成員函數(shù)來訪問。 ? 友元函數(shù)是一扇通往私有成員的后門。 ? 友元可以是一個一般函數(shù)(友元函數(shù)),也可以是另一個類的成員函數(shù)(友元成員),還可以是整個類(友元類)。 《 程序設(shè)計 》 程序設(shè)計 111 友元特點 ? 友元關(guān)系是授予的而不是索取的。也就是說,如果函數(shù) f 要成為類 A的友元,類 A必須顯式聲明函數(shù) f是他的友元,而不是函數(shù) f自稱是類 A的友元。 ? 友元關(guān)系不是對稱關(guān)系,如果類 A聲明了類 B是它的友元,并不意味著類 A也是類 B的友元。 ? 友元關(guān)系不是傳遞關(guān)系。如果類 A是類 B的友元,類 B是類 C的友元,并不意味著類 A是類 C的友元。 《 程序設(shè)計 》 程序設(shè)計 112 友元函數(shù)的聲明 ? 用關(guān)鍵詞 friend說明友元函數(shù) ? 說明位置:可以定義在類外部,也可定義在類內(nèi)部。可聲明在 public部分,也可聲明在 private部分。 《 程序設(shè)計 》 程序設(shè)計 113 友元函數(shù)的聲明實例 ? 定義在類內(nèi) include include class girl { char name[10]。 int age。 public:girl(char *n, int d) {strcpy(name,n)。 age=d。} friend void disp(girl amp。x) //定義 { cout― ― endl。} }。 main() {girl e(abc, 15)。 disp(e)。 } ?定義在類外 include include class girl { char name[10]。 int age。 public:girl(char *n, int d) {strcpy(name,n)。 age=d。} friend void disp(girl amp。x); }。 void disp(girl amp。x)//定義 {cout ― endl。} main() {girl e(abc, 15)。 disp(e)。 } 一般必須有參數(shù) 《 程序設(shè)計 》 程序設(shè)計 114 友元成員 ? 是其他某個類的成員函數(shù) ? 可以訪問 friend聲明語句所在類的私有成員和公有成員的成員函數(shù) ? 類 A的成員函數(shù)作為類 B的友元函數(shù)時,必須先定義類 A,再定義類 B。在定義類 A和 B前必須先聲明類 B。 ? 格式: friend 函數(shù)返回類型 類名標(biāo)識符 ::函數(shù)名(參數(shù)列表 ); 《 程序設(shè)計 》 程序設(shè)計 115 友元成員實例 class boy。 //類的聲明 class girl { char name[10]。 int age。 public:girl(char *n, int d) {strcpy(name,n)。 age=d。} void disp(boy amp。x)。 }。 class boy { char name[10]。 int age。 public:boy(char *n, int d) {strcpy(name,n)。age=d。} friend void girl::disp(boy amp。)。 }。 void girl::disp(boy amp。x) {coutname ageendl。 cout endl。} void main() {girl e(abc, 15)。 boy b(cde,20)。 (b)。 } abc 15 cde 20 《 程序設(shè)計 》 程序設(shè)計 116 友元類 ? 整個類作為另一個類的友元。 ? 當(dāng) A類被說明為類 B的友元時,類 A的所有函數(shù)都是類 B的友元函數(shù) ? 聲明方法: class Y{….}。 class X{ …. friend Y。//或 friend class Y ……}。 《 程序設(shè)計 》 程序設(shè)計 117 友元類實例 class boy。 class girl { char name[10]。 int age。 public:girl(char *n, int d) {strcpy(name,n)。 age=d。} void disp(boy amp。x)。 }。 class boy { char name[10]。 int age。 public:boy(char *n, int d) {strcpy(name,n)。age=d。} friend class girl。 }。 void girl::disp(boy amp。x) {coutname ageendl。 cout endl。} void main() {girl e(abc, 15)。 boy b(cde,20)。 (b)。 } abc 15 cde 20 《 程序設(shè)計 》 程序設(shè)計 118 友元聲明說明 ? 友元關(guān)系可以寫在類定義中的任何地方 ? 但一個較好的程序設(shè)計的習(xí)慣是將所有的友元關(guān)系的聲明放在最前面的位置,并且不要在他的前面添加任何訪問控制說明 《 程序設(shè)計 》 程序設(shè)計 119 class girl { friend void disp(girl amp。x)。 private: char name[10]。 int age。 public: girl(char *n, int d) {strcpy(name,n)。 age=d。} }。 void disp(girl amp。x)//友元函數(shù)的定義 {cout endl。} int main() { girl e(abc, 15)。 disp(e)。 return 0。 } 《 程序設(shè)計 》 程序設(shè)計 120 總結(jié) ? 本章介紹了面向?qū)ο蟮某绦蛟O(shè)計的基本思想。面向?qū)ο蟪绦蛟O(shè)計的基本思想是如何根據(jù)應(yīng)用的需求創(chuàng)造合適的類型,用該類型的對象來解決特定的問題。 ? 本章主要介紹了如何定義一個類,如何通過訪問控制實現(xiàn)信息的封裝。如何定義類的對象,如何實現(xiàn)對象的初始化,如何操作對象。
點擊復(fù)制文檔內(nèi)容
醫(yī)療健康相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1