《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第8章 自定義類型_第1頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第8章 自定義類型_第2頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第8章 自定義類型_第3頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第8章 自定義類型_第4頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第8章 自定義類型_第5頁
已閱讀5頁,還剩33頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第8章自定義類型8.1結(jié)構(gòu)體8.2聯(lián)合體8.3枚舉8.4類型別名8.5應(yīng)用8.6本章小結(jié)1118.1結(jié)構(gòu)體22先前的數(shù)組可以將多個(gè)相同類型的數(shù)據(jù)統(tǒng)一組織起來,方便用戶使用。而當(dāng)多個(gè)數(shù)據(jù)的類型可能不同,但它們在邏輯上有內(nèi)在聯(lián)系時(shí),C++提供了一種稱為結(jié)構(gòu)體的數(shù)據(jù)類型來描述這種組合。8.1.1結(jié)構(gòu)體類型定義33結(jié)構(gòu)體數(shù)據(jù)類型的定義形式如下:struct結(jié)構(gòu)體類型名{ 成員列表;};44

比如一名學(xué)生的信息,可以用:學(xué)號(num),姓名(name),年齡(age),身高(height)等特征來描述,C++中可以通過如下的聲明來描述這樣的一個(gè)組合類型:structStudent{intnum;//整型變量num表示學(xué)號charname[10];//字符數(shù)組變量name表示姓名intage;//整型變量age表示年齡floatheight;//浮點(diǎn)變量height表示身高};10010LiFang181.65numnameageheight558.1.2結(jié)構(gòu)體變量定義定義結(jié)構(gòu)體變量的方式分成如下三種:(1)分段聲明有名類型和定義變量(2)同步聲明有名類型和定義變量(3)同步聲明匿名類型和定義變量66分段聲明有名類型和定義變量Studentstu1,stu2;//定義結(jié)構(gòu)體變量這里可以用[struct]結(jié)構(gòu)體類型變量名;的方式來定義變量,struct可以省略,比如structStudentstu1,stu2;這種早期的定義方式也是可以的。這種定義結(jié)構(gòu)體變量的方式比較靈活,適用面比較廣泛,如果將此結(jié)構(gòu)體聲明放在頭文件(h)中,則只要在任意需要使用該結(jié)構(gòu)體的地方包含該頭文件則就可以方便定義變量了。77

同步聲明有名類型和定義變量這種定義方式的語法形式:struct結(jié)構(gòu)體類型名{ 成員列表;}變量名表;這種定義的形式也支持另行基于該結(jié)構(gòu)體類型名定義結(jié)構(gòu)體變量,在只需要在本文件中使用該結(jié)構(gòu)體數(shù)據(jù)類型時(shí),可以采用該方法來定義結(jié)構(gòu)體類型和結(jié)構(gòu)體變量。88

同步聲明匿名類型和定義變量struct{intnum;charname[10];intage;floatheight;}stu1,stu2;這里聲明結(jié)構(gòu)體變量的時(shí)候沒有定義名字,這種方式比較適合這種結(jié)構(gòu)體類型只在一個(gè)局部使用,即不需要另行基于該類型創(chuàng)建其他結(jié)構(gòu)體變量。998.1.3結(jié)構(gòu)體變量初始化結(jié)構(gòu)體變量在定義時(shí)可以賦初始值,賦值的形式類似于數(shù)組的初始化,只是其各項(xiàng)數(shù)據(jù)類型需要和結(jié)構(gòu)體數(shù)據(jù)類型定義保持一致。比如:structStudent{intnum;charname[10];intage;floatheight;}stu={1000,"Li",20,175};1010

對于結(jié)構(gòu)體類型定義中包括另外結(jié)構(gòu)體類型定義的情形,可以通過嵌套賦值的方式實(shí)現(xiàn)。比如:structDate{intyear;intmonth;intday;};structStudent{intnum;charname[10];intage;floatheight;Dateenrollment;};Studentstu={1000,"Li",20,175,{2000,1,1}};11118.1.4讀寫結(jié)構(gòu)體變量對于結(jié)構(gòu)體變量,可以有兩種讀寫形式:(1)同一個(gè)結(jié)構(gòu)體類型的結(jié)構(gòu)體變量之間可以直接相互賦值。(2)對于結(jié)構(gòu)體變量的成員讀寫,可以通過成員運(yùn)算符點(diǎn)”.”進(jìn)行訪問,也就是可以通過結(jié)構(gòu)體變量名.成員名的形式指定到具體的某個(gè)變量的成員,然后進(jìn)行操作.12例8.1比較輸出身高較高學(xué)生信息structStudent{intnum;charname[10];intage;floatheight;};

intmain(){Studentstu1,stu2;cout<<"Inputthefirststudent:"<<endl;cin>>stu1.num>>>>stu1.age

>>stu1.height;

cout<<"Inputthesecondstudent:"<<endl;

cin>>stu2.num

>>

>>stu2.age

>>stu2.height;

cout<<"Thetallerstudent:"<<endl;

if(stu1.height>=stu2.height)

{

cout<<stu1.num<<'\t'<<<<'\t'<<stu1.age<<'\t'<<stu1.height<<endl;

}

else

{

cout<<stu2.num<<'\t'<<<<'\t'<<stu2.age<<'\t'<<stu2.height<<endl;

}

return0;

}138.1.5函數(shù)中的結(jié)構(gòu)體結(jié)構(gòu)體變量可以用在函數(shù)中,可以作為函數(shù)參數(shù)作為函數(shù)返回值14

作為函數(shù)參數(shù)例8.2顯示學(xué)生信息#include<iostream>usingnamespacestd;structDate{

intyear;

intmonth;

intday;};structStudent{

intnum;

charname[10];

intage;

floatheight;

Dateenrollment;};voidshowStudent(Studentstu){

cout<<"Num:"<<stu.num<<endl

<<"Name:"<<<<endl

<<"Age:"<<stu.age<<endl

<<"Height:"<<stu.height<<endl

<<"Enrollment:"<<stu.enrollment.year

<<"-"

<<stu.enrollment.month

<<"-"

<<stu.enrollment.day;}intmain(){

Studentstu={1000,"zhang",20,175,2000,1,1};

showStudent(stu);

return0;}15作為函數(shù)返回值例8.3

輸出較高學(xué)生姓名structStudent{

intnum;

charname[10];

intage;

floatheight};StudentgetOneStudent(){

Studentstu;

cout<<"Num:"<<endl;

cin>>stu.num;

cout<<"Name:"<<endl;

cin>>;

cout<<"Age:"<<endl;

cin>>stu.age;

cout<<"Height:"<<endl;

cin>>stu.height;

returnstu;}intmain(){

Studentstu1=getOneStudent();

Studentstu2=getOneStudent();

cout<<"Thetallerstudent'sname:";

if(stu1.height>=stu2.height)

{

cout<<;

}

else

{

cout<<;

}

return0;}168.1.6結(jié)構(gòu)體數(shù)組當(dāng)需要操作多個(gè)相同結(jié)構(gòu)體類型的變量時(shí),可以用結(jié)構(gòu)體數(shù)組來組織管理。結(jié)構(gòu)體數(shù)組是每一個(gè)元素均為結(jié)構(gòu)體類型的數(shù)組,對數(shù)組整體的操作仍然和一般數(shù)據(jù)類型的數(shù)組相同,只是當(dāng)需要對數(shù)組元素進(jìn)行具體操作時(shí),需要清楚其為結(jié)構(gòu)體變量,需要用讀寫結(jié)構(gòu)體變量的相關(guān)技術(shù)來訪問。

例8.4

顯示超過平均身高的學(xué)生信息17#include<iostream>usingnamespacestd;structStudent{

intnum;

charname[10];

intage;

floatheight;};

intmain(){

constintLEN=4;

Studentstu[LEN]={{1000,"Zhao",20,175},

{2000,"Qian",21,170},

{3000,"Sun",19,180},

{4000,"Li",22,176}};doubleavg=0;

for(Studenttmp:stu)

{

avg=avg+tmp.height/LEN;

}

cout<<"averageheight:"<<avg<<endl;

for(Studenttmp:stu)

{

if(tmp.height>avg)

{

cout<<tmp.num<<'\t'<<<<'\t'<<tmp.age<<'\t'<<tmp.height<<endl;

}

}

return0;}188.2聯(lián)合體聯(lián)合體,又稱共用體。在定義形式上與之前的結(jié)構(gòu)體比較類似,可以把多個(gè)成員組合在一個(gè)統(tǒng)一定義的數(shù)據(jù)類型中。定義聯(lián)合體的語法形式如下:union聯(lián)合類體型名{

成員列表;};例8.5

同成員的結(jié)構(gòu)體和聯(lián)合體內(nèi)存容量比較19#include<iostream>usingnamespacestd;intmain(){structsData{inti;charc[8];};unionuData{inti;charc[8];};sDatas1;uDatau1;

cout<<"size"<<endl;cout<<"int:"<<sizeof(int)<<endl<<"char:"<<sizeof(char)<<endl<<"struct:"<<sizeof(s1)<<endl<<"union:"<<sizeof(u1)<<endl;return0;}20例8.5內(nèi)存占用示意圖C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐2121例8.6

判斷數(shù)據(jù)存儲的大小端#include<iostream>#include<iomanip>usingnamespacestd;intmain(){

unionuData

{

inti;

charc[4];

};

uDatau;

u.i=0x12345678;

for(inti=0;i<4;i++)

{

cout<<hex<<int(u.c[i])<<'\t';

}

if(u.c[0]==0x78)

{

cout<<endl<<"LittleEndian"<<endl;

}

else

{

cout<<endl<<"BigEndian"<<endl;

}

return0;}C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐2222

8.3枚舉定義枚舉類型的語法形式:enum類型名{枚舉常量列表};定義之后,可以使用這種類型來定義變量,比如:enumanswer{A,B,C,D};

//定義一道選額題的答案為一種新的枚舉類型answeranswerans1,ans2; //基于這種類型定義了兩個(gè)變量ans1,ans2這里,answer是一種新的數(shù)據(jù)類型,ans1和ans2是這種數(shù)據(jù)類型定義的兩個(gè)變量,ans1和ans2只能取A,B,C或D中的一個(gè)值。這里的A,B,C,D是自定義的標(biāo)識符,表示四個(gè)不同的值。C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐23例8.7枚舉類型簡單使用#include<iostream>usingnamespacestd;intmain(){

enumanswer{A,B,C,D};

answerans1=A;

answerans2=D;

cout<<"ans1:"<<ans1<<"\tans2:"<<ans2<<endl;

answertmp=ans1;

ans1=ans2;

ans2=tmp;

cout<<"ans1:"<<ans1<<"\tans2:"<<ans2<<endl;

return0;}C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐248.4類型別名為了適應(yīng)多用戶以及多平臺下的軟件開發(fā),有時(shí)需要將已有的數(shù)據(jù)類型重新取一個(gè)名字,也就是類型別名,這樣可以更好地協(xié)調(diào)軟件開發(fā)或多平臺下的軟件移植。C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐258.4.1#define#define主要用來做宏定義,先前用來定義名稱常量即是一種使用情況。還可以用:#define類型別名

現(xiàn)有類型名這種方式將現(xiàn)有的一種數(shù)據(jù)類型定義成一種新的名字的類型。例8.8

#define類型別名使用示例C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐26#include<iostream>usingnamespacestd;structStudent{

intnum;

charname[10];

intage;

floatheight;};#defineSTUDENTStudent#defineBIGINTintintmain(){

STUDENTstu={1000,"zhang",20,175};

BIGINTb=1000;

cout<<stu.num<<'\t'<<<<'\t'<<stu.age<<'\t'<<stu.height<<endl;

cout<<sizeof(b)<<endl;

return0;}C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐278.4.2typedef#define定義類型別名本質(zhì)上是利用了其標(biāo)識符替換的特征。而typedef定義類型別名則是C++提供的專用別名定義的關(guān)鍵字,其定義別名的形式為:typedef現(xiàn)有類型名

類型別名;注意,定義中所用到的兩個(gè)名字的順序和#define剛好相反,這里是別名在后,已有的名字在前,同時(shí)這里是一條語句,最后需要加分號“;”。例8.9

typedef別名定義示例C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐28#include<iostream>usingnamespacestd;structStudent{

intnum;

charname[10];

intage;

floatheight;};typedefStudentSTUDENT;typedefint

BIGINT;typedefchar

Name[10];intmain(){

STUDENTstu={1000,"zhang",20,175};

BIGINTb=1000;

NameaMan="zhao",bMan="qian",cMan="sun",dMan="li";

cout<<stu.num<<'\t'<<<<'\t'<<stu.age<<'\t'<<stu.height<<endl;

cout<<sizeof(b)<<endl;

cout<<aMan<<'\t'<<bMan<<'\t'<<cMan<<'\t'<<dMan<<endl;

return0;}C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐298.4.3using采用using進(jìn)行類型別名定義語法形式如下:using類型別名=現(xiàn)有類型名;這里采用了賦值語句的形式來定義類型別名,語句的末尾需要有分號。例8.10

using類型別名示例C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐30

#include<iostream>

usingnamespacestd;

structStudent

{

intnum;

charname[10];

intage;

floatheight;

};

usingSTUDENT=Student;

usingBIGINT=int;

usingName=char[10];intmain(){STUDENTstu={1000,"zhang",20,175};BIGINTb=1000;

NameaMan="zhao",bMan="qian",cMan="sun",dMan="li";

cout<<stu.num<<'\t'<<<<'\t'<<stu.age<<'\t'<<stu.height<<endl;

cout<<sizeof(b)<<endl;

cout<<aMan<<'\t'<<bMan<<'\t'<<cMan<<'\t'<<dMan<<endl;

return0;

}C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐308.5應(yīng)用例8.11文明城市投票:現(xiàn)有5個(gè)文明城市,以及用戶的投票列表20個(gè),需要統(tǒng)計(jì)這5個(gè)文明城市獲得選票的情況,用戶投票列表中的名單可能會有大小寫不一致的情況,最終需要根據(jù)城市得票數(shù)由高到低排序顯示城市名稱和得票數(shù)。C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐32structCity{

charname[N];

intvote;};usingCITY=City;boolisequal(charstr1[],charstr2[],intn){

boolis=true;

for(inti=0;i<n;i++)

{

if(str1[i]==str2[i]||

str1[i]-str2[i]==32||

str1[i]-str2[i]==-32)

continue;

else{

is=false;

break;

}}

returnis;}intmain(){constintLEN=5;constintNUM=20;CITYcitys[LEN]={{"beijing",0},{"shanghai",0},{"tianjin",0},{"chongqing",0},{"xuzhou"}};charvotes[NUM][N]={"beijing","shanghai","tianjin","chongqing","ShangHai","beijing","shanghai","tianJin","beiJing","Xuzhou","beijing","shanghai","Shanghai","chongqing","xuZhou","beijing","Beijing","tianjin","XUZHOU","XuZhou"};for(inti=0;i<NUM;i++){for(intj=0;j<LEN;j++){if(isequal(votes[i],citys[j].name,N)){citys[j].vote++;break;}}}for(intround=1;round<LEN;round++){for(inti=0;i<LEN-round;i++){if(citys[i].vote<citys[i+1].vote){CITYctemp=citys[i];citys[i]=citys[i+1];citys[i+1]=ctemp;}}}for(inti=0;i<LEN;i++){cout<<citys[i].vote<<'\t'<<citys[i].name<<endl;}C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐33例8.12紙牌花色組合:從4個(gè)不同花色的紙牌中任意選兩張紙牌,求其具體花色紙牌組合形式。利用枚舉表示花色。C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐34#include<iostream>usingnamespacestd;enumCard{Spade,Heart,Diamond,Club};voidshowCard(Cardc){

switch(c)

{

caseSpade:

cout<<"Spade";

break;

caseHeart:

cout<<"Heart";

break;

caseDiamond:

cout<<"Diamond";

break;

caseClub:

cout<<"Club";

break;

}}intmain(){

intn=0;

for(intc1=Spade;c1<=Club;c1++)

{

for(intc2=Spade;c2<=Club;c2++){

if(c1==c2)

continue;

cout<<++n<<":\t";

showCard(Card(c1));

cout<<'\t';

showCard(Card(c2));

cout<<endl;}

}

return0;}C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐35例8.13顯示入學(xué)日期相同的學(xué)生,利用結(jié)構(gòu)體數(shù)據(jù)類型表示學(xué)生信息。C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐36structDate{intyear;intmonth;intday;};structStudent{intnum;charname[10];Dateenrollment;};boolisSameDay(Studentstu1,Studentstu2){if(stu1.enrollment.year==stu2.enrollment.year&&stu1.enrollment.month==stu2.enrollment.month&&stu1.enrollment.day==stu2.enrollment.day){returntrue;}else{returnfalse;}}voidshowDay(Studentstu){cout<<stu.enrollment.year

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論