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

正文內(nèi)容

計算機(jī)二級c考試復(fù)習(xí)資料全資料-資料下載頁

2025-04-24 22:46本頁面
  

【正文】 的地址賦值給下一個表項的鏈接指針,從而構(gòu)造了一個鏈表。它并沒有改變調(diào)用它的listEntry對象的內(nèi)容,只是把該對象的地址賦給函數(shù)的參數(shù)所引用的那個ListEntry對象的preventry指針,盡管該函數(shù)不會修改對象的數(shù)據(jù),但它并不是常量型。這是因為,它拷貝對象的地址this指針的內(nèi)容給一個非長常量對象,而編譯器回認(rèn)為這個非常量對象就有可能通過拷貝得到的地址去修改當(dāng)前對象的數(shù)據(jù),因此AddEntry()函數(shù)在聲明時不需要用const.二級C++輔導(dǎo)筆記:類對象數(shù)組和靜態(tài)成員一、類對象數(shù)組  類的對象和C++其他數(shù)據(jù)類型一樣,也可以為其建立數(shù)組,數(shù)組的表示方法和結(jié)構(gòu)一樣?! nclude   class Date  {  int mo,da,yr?! ublic:  Date(int m=0,int d=0, int y=0) { mo=m。 da=d。 yr=y。}  void display() const { cout }?! nt main()  {  Date dates[2]?! ate today(12,31,2003)?! ates[0]=today?! ates[0].display()?! ates[1].display()?! eturn 0。  }    在前面已經(jīng)說過,不帶參數(shù)或者所有參數(shù)都有默認(rèn)值的構(gòu)造函數(shù)叫做默認(rèn)構(gòu)造函數(shù)。如果類中沒有構(gòu)造函數(shù),編譯器會自動提供一個什么都不做的公共默認(rèn)構(gòu)造函數(shù) 。如果類當(dāng)中至少有一個構(gòu)造函數(shù),編譯器就不會提供默認(rèn)構(gòu)造函數(shù)?! ∪绻惍?dāng)中不含默認(rèn)構(gòu)造函數(shù),則無法實例化其對象數(shù)組。因為實例花類對象數(shù)組的格式不允許用初始化值來匹配某個構(gòu)造函數(shù)的參數(shù)表。  上面的程序中,main()函數(shù)聲明了一個長度為2的Date對象數(shù)組,還有一個包含初始化值的單個Date對象。接著把這個初始化的Date對象賦值給數(shù)組中第一個對象,然后顯示兩個數(shù)組元素中包含的日期。從輸出中可以看到,第一個日期是有效日期,而第二個顯示的都是0。  當(dāng)聲明了某個類的對象數(shù)組時,編譯器會為每個元素都調(diào)用默認(rèn)構(gòu)造函數(shù)。  下面的程序去掉了構(gòu)造函數(shù)的默認(rèn)參數(shù)值,并且增加了一個默認(rèn)構(gòu)造函數(shù)?! nclude   class Date  {  int mo, da, yr。  public:  Date()?! ate(int m,int d,int y) { mo=m。 da=d。 yr=y。}  void display() const { cout }?! ate::Date()  {  cout mo=0。 da=0。 yr=0?!   int main()  {  Date dates[2]?! ate today(12,31,2003)?! ates[0]=today?! ates[0].display()。  dates[1].display()。  return 0?!   運行程序,輸出為:  Date constructor running  Date constructor running  12/31/2003  0/0/0  從輸出中可以看出,Date()這個默認(rèn)構(gòu)造函數(shù)被調(diào)用了兩次。    當(dāng)類對象離開作用域時,編譯器會為每個對象數(shù)組元素調(diào)用析構(gòu)函數(shù)。  include   class Date  {  int mo,da,yr?! ublic:  Date(int m=0,int d=0,int y=0) { mo=m。 da=d。 yr=y。}  ~Date() {cout void display() const {cout }。  int main()  {  Date dates[2]?! ate today(12,31,2003)?! ates[0]=today?! ates[0].display()?! ates[1].display()?! eturn 0?!   運行程序,輸出為:  12/31/2003  0/0/0  Date destructor running  Date destructor running  Date destructor running  表明析構(gòu)函數(shù)被調(diào)用了三次,也就是dates[0],dates[1],today這三個對象離開作用域時調(diào)用的。二、靜態(tài)成員  可以把類的成員聲明為靜態(tài)的。靜態(tài)成員只能存在唯一的實例。所有的成員函數(shù)都可以訪問這個靜態(tài)成員。即使沒有聲明類的任何實例,靜態(tài)成員也已經(jīng)是存在的。不過類當(dāng)中聲明靜態(tài)成員時并不能自動定義這個變量,必須在類定義之外來定義該成員?!   §o態(tài)數(shù)據(jù)成員相當(dāng)于一個全局變量,類的所有實例都可以使用它。成員函數(shù)能訪問并且修改這個值。如果這個靜態(tài)成員是公有的,那么類的作用域之內(nèi)的所有代碼(不論是在類的內(nèi)部還是外部)都可以訪問這個成員。下面的程序通過靜態(tài)數(shù)據(jù)成員來記錄鏈表首項和末項的地址?! nclude   include   class ListEntry  {  public:  static ListEntry* firstentry?! rivate:  static ListEntry* lastentry?! har* listvalue?! istEntry* nextentry。  public:  ListEntry(char*)。  ~ListEntry() { delete [] listvalue。}  ListEntry* NextEntry() const { return nextentry。 }。  void display() const { cout }?! istEntry* ListEntry::firstentry。  ListEntry* ListEntry::lastentry?! istEntry::ListEntry(char* s)  {  if(firstentry==0) firstentry=this?! f(lastentry!=0) lastentrynextentry=this?! astentry=this。  listvalue=new char[strlen(s)+1]?! trcpy(listvalue,s)?! extentry=0?!   int main()  {  while (1)  {  cout\nEnter a name (39。end39。 when done): 。  char name[25]。  cinname?! f(strncmp(name,end,3)==0) break?! ew ListEntry(name)。  }  ListEntry* next = ListEntry::firstentry?! hile (next != 0)  {  nextdisplay()。  ListEntry* hold = next。  next=nextNextEntry()?! elete hold?!   return 0。  }  程序首先顯示提示信息,輸入一串姓名,以end作為結(jié)束標(biāo)志。然后按照輸入順序來顯示姓名。構(gòu)造函數(shù)將表項加入鏈表,用new運算符來聲明一個表項,但并沒有把new運算符返回的地址賦值給某個指針,這是因為構(gòu)造函數(shù)會把該表項的地址賦值給前一個表項的nextentry指針?! ∵@個程序和前面將的逆序輸出的程序都不是最佳方法,最好的方法是使用類模板,這在后面再介紹。  main()函數(shù)取得ListEntry::firstentry的值,開始遍歷鏈表,因此必需把ListEntry::firstentry設(shè)置成公有數(shù)據(jù)成員,這不符合面向?qū)ο蟪绦虻募s定,因為這里數(shù)據(jù)成員是公有的?!   〕蓡T函數(shù)也可以是靜態(tài)的。如果一個靜態(tài)成員函數(shù)不需要訪問類的任何實例的成員,可以使用類名或者對象名來調(diào)用它。靜態(tài)成員通常用在只需要訪問靜態(tài)數(shù)據(jù)成員的情況下?! §o態(tài)成員函數(shù)沒有this指針,因為它不能訪問非靜態(tài)成員,所以它們不能把this指針指向任何東西?! ∠旅娴某绦蛑校琇istEntry類中加入了一個靜態(tài)成員函數(shù)FirstEntry(),它從數(shù)據(jù)成員firstentry獲得鏈表第一項的地址,在這兒,firstentry已經(jīng)聲明為私有數(shù)據(jù)成員了?! nclude   include   class ListEntry  {  static ListEntry* firstentry?! tatic ListEntry* lastentry?! har* listvalue?! istEntry* nextentry?! ublic:  ListEntry(char*)?! ListEntry() { delete [] listvalue。}  static ListEntry* FirstEntry() { return firstentry。 }  ListEntry* NextEntry() const { return nextentry。 }。  void display() const { cout }。  ListEntry* ListEntry::firstentry。  ListEntry* ListEntry::lastentry?! istEntry::ListEntry(char* s)  {  if(firstentry==0) firstentry=this。  if(lastentry!=0) lastentrynextentry=this。  lastentry=this?! istvalue=new char[strlen(s)+1]。  strcpy(listvalue, s)?! extentry = 0?!   int main()  {  while (1)  {  cout\nEnter a name (39。end39。 when done):?! har name[25]。  cin name?! f(strncmp(name,end,3)==0) break?! ew ListEntry(name)?!   ListEntry* next = ListEntry::FirstEntry()。  while (next != 0)  {  nextdisplay()?! istEntry* hold = next?! ext = nextNextEntry()?! elete hold?!   return 0?!   函數(shù)ListEntry::FirstEntry()是靜態(tài)的,返回靜態(tài)數(shù)據(jù)成員firstentry的值?!   ∪绻粋€靜態(tài)成員象上面程序一樣是公有的,那么在整個程序中都可以訪問它??梢栽谌魏蔚胤秸{(diào)用公有景泰成員函數(shù),而且不需要有類的實例存在。但公有靜態(tài)成員函數(shù)不完全是全局的,它不僅僅存在于定義類的作用域內(nèi)。在這個作用域里面,只要在函數(shù)名前加上類名和域解析運算符::就可以調(diào)用該函數(shù)。二級C++輔導(dǎo)筆記:類和堆一、構(gòu)造函數(shù)和析構(gòu)函數(shù)  前面的例子已經(jīng)運用了new和delete來為類對象分配和釋放內(nèi)存。當(dāng)使用new為類對象分配內(nèi)存時,編譯器首先用new運算符分配內(nèi)存,然后調(diào)用類的構(gòu)造函數(shù);類似的,當(dāng)使用delete來釋放內(nèi)存時,編譯器會首先調(diào)用淚的析構(gòu)函數(shù),然后再調(diào)用delete運算符?! nclude   class Date  {  int mo,da,yr?! ublic:  Date() { cout ~Date() { cout }  int main()  {  Date* dt = new Date?! out delete dt?! eturn 0?!   程序定義了一個有構(gòu)造函數(shù)和析構(gòu)函數(shù)的Date類,這兩個函數(shù)在執(zhí)行時會顯示一條信息。當(dāng)new運算符初始化指針dt時,執(zhí)行了構(gòu)造函數(shù),當(dāng)delete運算符釋放內(nèi)存時,又執(zhí)行了析構(gòu)函數(shù)。  程序輸出如下:  Date constructor  Process the date  Date destructor  二、堆和類數(shù)組  前面提到,類對象數(shù)組的每個元素都要調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)。下面的例子給出了一個錯誤的釋放類數(shù)組所占用的內(nèi)存的例子?! nclude   class Date  {  int mo, da, yr?! ublic:  Date() { cout ~Date() { cout }  int main()  {  Date* dt = new Date[5]?! out delete dt。 //這兒  return 0?!   指針dt指向一個有五個元素的數(shù)組。按照數(shù)組的定義,編譯器會讓new運算符調(diào)用Date類的構(gòu)造函數(shù)五次。但是delete被調(diào)用時,并沒有明確告訴編譯器指針指向的Date對象有幾個,所以編譯時,只會調(diào)用析構(gòu)函數(shù)一次。下面是程序輸出;  Date constructor  Date constructor  Date constructor  Date constructor  Date constructor  Process the date  Date
點擊復(fù)制文檔內(nèi)容
教學(xué)教案相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1