




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗指導 實驗1 C+語言基礎(chǔ)實驗2 類和對象實驗3 使用類和對象實驗4 重載實驗5 繼承與類的派生實驗6 多態(tài)與虛函數(shù)實驗7 輸入輸出流實驗8 異常處理與命名空間附:實驗報告注意事項實驗1 C+語言基礎(chǔ)1.實驗?zāi)康?(1)學習C+文件的建立和編譯。 (2)掌握C+基本語句,語言規(guī)范。 (3)熟悉變量及函數(shù)。 2.實驗基本要求 (1)理解C+基本程序,預編譯語句,掌握編譯方法。 (2)掌握C+基本的輸入輸出語句。 (3)熟悉函數(shù)的定義和使用,理解帶參數(shù)的主函數(shù)。實驗1 C+語言基礎(chǔ)3.實驗例題 例1 最簡單的C+程序 / 輸出一行字符。 #include void main() cout “T
2、his is the simplest C+ program.n”; 實驗1 C+語言基礎(chǔ) 例2 cin與cout的使用 #include void main() cout please enter your name and age: name; cin age; cout name is : name endl; cout age is : age endl; 實驗1 C+語言基礎(chǔ) 例3 應(yīng)用問題:程序?qū)τ脩糨斎氚霃?,計算并顯示園的面積。 一個應(yīng)用程序源碼 /* 演示C+程序的基本結(jié)構(gòu)程序 功能:對用戶輸入半徑,程序計算園的面積 */ #include using namespace st
3、d; void main() double r, area; cout r; area = 3.14159 * r * r; cout圓的面積為:areaendl; 實驗1 C+語言基礎(chǔ) 程序的模塊化例3-1 應(yīng)用程序結(jié)構(gòu)的改進 /對用戶輸入半徑,計算園的面積程序改進版v1.0 -單模塊 #include void calCircleArea() double r, area; cout r; area = 3.14159 * r * r; cout 圓的面積為: area endl; ; void main() calCircleArea(); 實驗1 C+語言基礎(chǔ) 例3-2 應(yīng)用程序結(jié)構(gòu)的
4、改進 /對用戶輸入半徑,計算園的面積程序改進版v1.1 -多模塊 #include double set_radius() double radius; cout radius; return radius; double calculate_area(double r) return 3.14159 * r * r; ;實驗1 C+語言基礎(chǔ) void print_area(double area) cout 圓的面積為: area endl; void main() double r,a; r=set_radius(); a=calculate_area(r); print_area(a);
5、 實驗1 C+語言基礎(chǔ) 例4 預處理語句 #include和#define的使用 1)新建att.h文件,在其中放入以下有關(guān)函數(shù)定義:#include #define PI 3.14159 /使用#definedouble calCircleArea(double r)double area; area = PI * r * r;return area; double set_radius() double radius; cout radius; return radius; void print_area(double area) cout 圓的面積為: area endl; 實驗1 C+
6、語言基礎(chǔ) 2)在同一目錄中,新建一個c1.cpp,代碼為: #include att.h /使用#include void main() double r,a; r=set_radius(); a=calculate_area(r); print_area(a); 3)編譯測試實驗1 C+語言基礎(chǔ) 例5 不帶參數(shù)的主函數(shù)應(yīng)用程序 #include void func1() cout Input datan; void func2() cout output datan; void main() int sel; do cout sel; switch (sel) case 1: func1()
7、; break; case 2: func2(); break; default: break; while (sel=1|sel=2); 實驗1 C+語言基礎(chǔ) 例6 帶參數(shù)的主函數(shù)應(yīng)用程序 / 處理命令行參數(shù)實例 #include void main(int argc, char *argv) cout這個程序的程序名是:argv0n; if (argc=1) cout沒有參數(shù)!; else int nCount = 1; while(nCount argc) cout第nCount個參數(shù)是: argvnCountn; nCount+; 實驗1 C+語言基礎(chǔ)應(yīng)用編程任務(wù) 任務(wù)1 程序?qū)τ脩糨?/p>
8、入正立方邊長,計算并顯示其體積。 任務(wù)2 程序?qū)τ脩糨斎雸@半徑或正方形邊長,計算并顯示其面積。實驗2 面向?qū)ο蠓椒≒art1 類的定義與對象的使用 1.實驗?zāi)康?(1)學習類與對象的定義。 (2)掌握類與對象的使用方法。 (3)學習數(shù)據(jù)成員與成員函數(shù)的訪問方式。 2.實驗要求 (1)可以定義出一個類 (2)創(chuàng)建該類的對象 (3)對象的訪問 3.實驗例題實驗2 面向?qū)ο蠓椒?例1 類和對象 /功能:輸入一個數(shù),并輸出該數(shù)。 #include #include class Sample public: void numin() cout x; void disp() cout endl 這個數(shù)是:
9、 x endl; private: int x; ; void main() Sample s; s.numin(); s.disp(); 實驗2 面向?qū)ο蠓椒ɡ? 建立一個新的計算立方體周長與體積的類。(將新建的類命名為 Box) #include class Box public: void seta(float r) a = r; void getvolume() volume = a * a * a; /計算體積 void getarea() area = 6*a*a; /計算表面積 void disp()/顯示計算結(jié)果 cout 體積: volume ,表面積: area endl;
10、 private: float a,volume,area; ; 實驗2 面向?qū)ο蠓椒?void main() Box obj;/定義對象 obj.seta(6);/對象的訪問 obj.getvolume(); obj.getarea(); obj.disp(); 4.實驗任務(wù) 以面向?qū)ο蟮木幊谭椒?,計算球面積與體積(建立一個求球面積與體積的Ball類)。實驗2 面向?qū)ο蠓椒≒art2 構(gòu)造函數(shù)與析構(gòu)函數(shù) 1實驗?zāi)康?(1)學習類與對象的定義和使用; (2)理解構(gòu)造函數(shù)與析構(gòu)函數(shù)的定義與執(zhí)行過程; 2實驗要求 (1)定義一個類,并且創(chuàng)建該類的對象。 (2)體現(xiàn)出構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用。 (3
11、)理解構(gòu)造函數(shù)與析構(gòu)函數(shù)的運行順序。 3實驗例題實驗2 面向?qū)ο蠓椒?例1 析構(gòu)函數(shù)的調(diào)用 /功能:輸出cat信息,age 和 weight。 #include class simplecat public: simplecat(int age,int weight);/構(gòu)造函數(shù) simplecat(); int GetAge() return itsage; int GetWeight() return itsweight; private: int itsage,itsweight;/定義兩個變量 ;實驗2 面向?qū)ο蠓椒?simplecat:simplecat(int age,int we
12、ight)/構(gòu)造函數(shù),對變量進行初始化 cout 調(diào)用構(gòu)造函數(shù) endl; itsage = age; itsweight = weight; simplecat:simplecat() cout 調(diào)用析構(gòu)函數(shù) endl; int main() simplecat F(5,8); cout F age is: F.GetAge() endl; cout F weight is: F.GetWeight() endl; return 0; 實驗2 面向?qū)ο蠓椒?例2 觀察構(gòu)造函數(shù)與析造函數(shù)的調(diào)用順序。(在此文件中建立一個 Tpair 的類。) #include class Student pub
13、lic: Student() coutconstructing student.n; Student() coutdestucting student.n; ;實驗2 面向?qū)ο蠓椒?class Teacher public: Teacher() coutconstructing teacher.n; Teacher() coutdestucting teacher.n; ;實驗2 面向?qū)ο蠓椒?class Tpair public: Tpair() coutconstructing tutorpair.n; Tpair() coutdestucting tutorpair.n; protect
14、ed: Student student; Teacher teacher; ; void main() Tpair tp; coutback in main.n; 實驗2 面向?qū)ο蠓椒ɡ?.程序?qū)τ脩糨斎胝⒎竭呴L,計算并顯示其體積(用面向?qū)ο蟮木幊谭椒ǎ?/ 使用構(gòu)造函數(shù) #include class CCubic private: double len; public: CCubic(); double calculate_Volue(); void print_Volue(); ; /需加; CCubic:CCubic() cout len; 實驗2 面向?qū)ο蠓椒?double CCu
15、bic:calculate_Volue() return len * len * len; ; void CCubic:print_Volue() cout 正方體積: calculate_Volue() endl; 實驗2 面向?qū)ο蠓椒?void main() int sel; sel=1; do cout sel; switch (sel) case 1: CCubic c; c.print_Volue(); break; default: break; while (sel=1); 實驗2 面向?qū)ο蠓椒ɡ?.利用類和構(gòu)造函數(shù)的方法,編寫程序,解一元一次方程。(如CLinaerEquati
16、on類) #include char sgn(double x) if (x=0) return(+); else return(-); double abs(double x) if (x=0) return(x); else return(-x); 實驗2 面向?qū)ο蠓椒?class CLinearEquation private: double a,b; public: CLinearEquation(double a1, double b1); void disp_Equation(); void disp_Root(); ; CLinearEquation:CLinearEquatio
17、n(double a1, double b1) a=a1; b=b1; 實驗2 面向?qū)ο蠓椒?void CLinearEquation:disp_Equation() cout 線性方程: a X sgn(b ) abs(b ) =0 endl; void CLinearEquation:disp_Root() cout 線性方程的根: Root= -b/a endl; ; void main() CLinearEquation e1(2,-4); e1.disp_Equation(); e1.disp_Root(); 4實驗內(nèi)容 利用類和構(gòu)造函數(shù)的方法,編寫程序,解一元二次方程。(如Comp
18、lex類和CQuadraticEquation類)實驗3 繼承與類的派生 1實驗?zāi)康?(1)學習派生類的定義和使用; (2)理解派生類對基類成員的訪問權(quán) (3)理解派生類的構(gòu)造函數(shù)與析構(gòu)函數(shù)的定義與執(zhí)行過程; (4)學習多繼承類的定義和使用 2實驗要求 (1)定義一個派生類,并且創(chuàng)建該類的對象。 (2)體現(xiàn)出派生類構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用。 (3)理解派生類的構(gòu)造函數(shù)與析構(gòu)函數(shù)的運行順序。 (4)定義一個多繼承的派生類,并且創(chuàng)建該類的對象。 3實驗例題實驗3 繼承與類的派生例1 有一個base基類,派生出兩個類,一個是私有派生,一個是公有派生。#include class base /定義一個
19、基類int x1,x2;public:void assign(int p1,int p2) /為私有數(shù)據(jù)賦值x1=p1;x2=p2;int inc1()return +x1;int inc2()return +x2;void dispaly() /輸出x1,x2的值coutbase x1=x1 x2=x2n;實驗3 繼承與類的派生class derive1:base /定義一個私有派生類int x3;public:derive1(int p3)x3=p3; /構(gòu)造函數(shù)void assign(int p1,int p2) /為基類數(shù)據(jù)賦值base:assign(p1,p2); /調(diào)用基類成員函數(shù)
20、int inc1()return base:inc1(); /調(diào)用基類成員函數(shù)為x1增值int inc2()return base:inc2();int inc3()return +x3; /求x3的值 void display()coutderive1 x3=x3n; /輸出x3的值;實驗3 繼承與類的派生 class derive2:public base /定義一個公有派生類int x4;public:derive2(int p4)x4=p4; /構(gòu)造函數(shù)int inc1()int temp=base:inc1(); /不斷調(diào)用基類的inc1()temp=base:inc1();temp
21、=base:inc1();return base:inc1();int inc4()return +x4; /對x4增值void display()base:dispaly() ; /首先調(diào)用基類的display()coutderive2 x4=x4n;實驗3 繼承與類的派生 main()base p; p.assign(-2,-2); /p1的x1,x2取-2 p.dispaly(); /顯示成員函數(shù) derive1 d1(-4); d1.assign(10,10); d1.inc1(); d1.display(); derive2 d2(5); d2.assign(-6,-6); d2.d
22、isplay(); d2.inc1(); d2.inc2(); /實質(zhì)執(zhí)行的是d2.base:inc2() d2.display(); d2.base:inc1(); /顯式調(diào)用基類的成員函數(shù) d2.display(); return 1;實驗3 繼承與類的派生例2 教師和學生之間有很多相同的信息,如姓名、年齡等,可抽象出基類person;學生類student和教師類teacher作為person 的派生類。 #include class person /定義基類char *name; /姓名int age; /年齡char *add; /住址public:person() /構(gòu)造函數(shù)cout
23、the constructor of class person!n;person() /析構(gòu)函數(shù)coutthe destructor of class person!n;實驗3 繼承與類的派生class student:public person /定義派生類studentchar *department; /所在系int level; /年級public:student() /構(gòu)造函數(shù)coutthe constructor of class student!n;student() /析構(gòu)函數(shù)coutthe destructor of class student!n;實驗3 繼承與類的派生cl
24、ass teacher:public person /定義派生類teacherchar *major; /專業(yè)float salary; /工資public:teacher() /構(gòu)造函數(shù)coutthe constructor of class teacher!n;teacher() /析構(gòu)函數(shù)coutthe destructor of class teacher! n;實驗3 繼承與類的派生main() student d1;teacher d2;return 1;實驗3 繼承與類的派生例3 圖形的基本單元是一個點,location描述屏幕位置的點,他派生出的類point具有顯示、隱去和移動
25、功能。由point類派生出的圓類circles,point類的數(shù)據(jù)成員作為圓心。 #includeclass location /定義位置類protected:int x,y; /x,y為保護段成員public:location(int x,int y); /基類構(gòu)造函數(shù);實驗3 繼承與類的派生class point:public location /定義point類為location類的公有派生public:point(int x,int y); void show(); /用當前顏色畫點void hide(); /抹去顯示的點void moveto(int nx,int ny); /移動點
26、;實驗3 繼承與類的派生class circles:point /定義circles類為point類的私有派生int radius;public:circles(int x,int y,int radius);void show(); /顯示圓void hide(); /抹去圓void moveto(int nx,ny); /在屏幕上移動圓;location:location(int x,int y)location:x=x;location:y=y;實驗3 繼承與類的派生point:point(int x,int y):location(x,y) /point類構(gòu)造函數(shù),是專門為了執(zhí)行l(wèi)oc
27、ation 類的構(gòu)造函數(shù)而設(shè)置的void point:show()putpixel(x,y,getcolor(); /用當前顏色畫點void point:hide()putpixel(x,y,getbkcolor(); /用當前背景色畫點,相當于在屏幕上刪除點 實驗3 繼承與類的派生void point:moveto(int nx,int ny) /在屏幕上移動點hide();x=nx;y=ny;show();circles:circles(int x,int y,int radius):point(x,y)/circles類的構(gòu)造函數(shù),綴上了基類的構(gòu)造函數(shù)circles:radius=rad
28、ius;實驗3 繼承與類的派生void circles:show()circle(x,y,radius); /畫一個圓void circle:hide()unsigned int sc;sc=getcolor(); /取出當前色setcolor(getbkcolor(); /將背景色作為當前色circle(x,y,radius); /用背景色畫一個圓,相當于抹去一個圓setcolor(sc); /將當前色置回到原來狀態(tài)實驗3 繼承與類的派生void circles:moveto(int nx,int ny) /在屏幕上移動圓hide();x=nx;y=ny;show();實驗3 繼承與類的派生
29、main()circles cir(320,240,100);int gdriver=DETECT,gmode;initgraph(&gdriver,&gmode,); /初始化圖形模式setcolor(14);cir.show();for (int i=100;i300;i+) /用循環(huán)在屏幕上不斷移動這個圓,/得到一個動的圓cir.moveto(2*i,i);closegraph();return 1;實驗3 繼承與類的派生例4 在一個圓內(nèi)顯示正文。要解決這個問題,實際上是完成顯示一個帶字符串的圓。它可以是從圓類和正文類多繼承來的新類,而圓類和正文類又都是從point類派生出來的類。實驗3
30、 繼承與類的派生#include #include#includeclass point protected:int x,y; /x,y為point類的保護段成員public: point(int initx,int inity);class circles:point/定義圓類int radius;public:circles(int x,int y,int radius);void show();實驗3 繼承與類的派生class gmessage:point /定義正文類 char *msg; int font; int field;public:gmessage(int mx,int m
31、y,int msgfont,int fieldsize,char *text);void show();class mcircle:circle,gmessage /定義帶字符串的圓類public:mcircle(int mrx,int mry,int mrradius,int font,char *msg);void show();實驗3 繼承與類的派生point:point(int initx,int inity)x=initx;y=inity;circles:circles(int x,int y,int radius):point(x,y)circles:radius=radius;v
32、oid circles:show()circle(x,y,radius);實驗3 繼承與類的派生gmessage:gmessage(int mx,int my,int msgfont,int fieldsize,char *text):point (mx,my)font=msgfont;field=fieldsize;msg=text;void gmessage:show()int size=field/(8*strlen(msg);settextjustify(CENTER_TEXT,CENTER_TEXT); /指定文本輸出的對齊方式settextstyle(font,HORIZ_DIR,
33、size); /設(shè)置文本輸出屬性outtextxy(x,y,msg); /輸出字符串實驗3 繼承與類的派生mcircle:mcircle(int mrx,int mry,int mrradius,int font,char *msg):circles(mrx,mry,mrradius),gmessage(mrx,mry,font,2*mrradius,msg) vod mcircle:show()circles:show();gmessage:show();實驗3 繼承與類的派生main()int gdriver=DETECT,gmode;initgraph(&gdriver,&gmode,)
34、;setcolor(12);mcircle la(250,250,225,GOTHIC_FONT,universe);la.show();getch();closegraph();return 1;實驗3 繼承與類的派生 4實驗內(nèi)容(1)設(shè)計一個大學的類系統(tǒng),學校中有學生、教師、職員,每種人員都有自己的特性,它們之間又有相同的地方。利用繼承機制定義這個系統(tǒng)中的各個類及類上必須的操作。(2)定義水果類和樹類,并描述它們的一些特性,由這兩個類派生出蘋果類和桔子類,定義它們的一些基本功能。實驗4 重載1.實驗?zāi)康?(1)學習C+函數(shù)的重載。 (2)學習C+運算符重載。2.實驗基本要求 (1)掌握C+
35、函數(shù)重載的使用。 (2)掌握C+運算符重載的使用。3.實驗例題實驗4 重載例1.使用一般函數(shù)重載實現(xiàn)兩個int、float、double和字符串相加#include#includeint plus(int x,int y) /return x+y;float plus(float x,float y)return x+y;double plus(double x,double y)return x+y;實驗4 重載char *plus(char *x,char *y)return strcat(x,y);void main()int i=12,j=34;float x1=1.2,y1=4.5;
36、double x2=24.5,y2=635.4;char str120 =object, *str2=windows;coutplus(i,j)n;coutplus(x1,y1)n;coutplus(x2,y2)n;coutplus(str1,str2)n;實驗4 重載例2:定義一個計時器類,構(gòu)造函數(shù)傳遞的參數(shù)可以用一個整數(shù)表示的秒數(shù),也可用數(shù)字串表示;還可傳遞兩個參數(shù),一個表示秒數(shù),一個表示分鐘數(shù)。#include #includeclass timerint seconds;public:timer();timer(char *t);timer(int t);timer(int,int);
37、int gettimer();實驗4 重載timer:timer()seconds=0;timer:timer(int t)seconds=t;timer:timer(char *t)seconds=atoi(t);timer:timer(int min,int sec)seconds=min*60+sec;int timer:gettimer()return seconds;實驗4 重載main()timer t1,t2(123),t3(23),t4(2,34);coutobject t1:t1.gettimer()n;coutobject t2:t2.gettimer()n;coutobj
38、ect t3:t3.gettimer()n;coutobject t4:t4.gettimer()n;return 1;實驗4 重載例3. point類中有兩個數(shù)據(jù)成員x,y, 使用重載運算符“+”,“-”的方法進行Point對象的相加或相減,實際上是兩個點坐標的x,y值各自相加或相減。實驗4 重載#include class pointint x,y;public:point(int vx,int vy)x=vx;y=vy;point() x=0;y=0;point operator + (point p1); /重載運算符+point operator - (point p1); /重載運
39、算符-void print()coutx yn;實驗4 重載point point:operator + (point p1) /定義兩個對象的+函數(shù)point p;p.x=x+p1.x;p.y=y+p1.y;return p; /返回當前對象與p對象之和point point:operator - (point p1) /定義兩個對象的+函數(shù)point p;p.x=x-p1.x;p.y=y-p1.y;return p; /返回當前對象與p對象之差實驗4 重載main()point p1(10,10),p2(20,20);p1=p1+p2;p1.print();return 1;實驗4 重載例
40、4.用友元重載運算符進行復數(shù)運算#include class complex float real,imag;public:complex(float r,float i)real=r;imag=i;complex()real=0;imag=0;實驗4 重載 void print(); friend complex operator + (complex plex b); friend complex operator - (complex plex b); friend complex operator * (complex plex b); friend complex operator
41、/ (complex plex b); ;實驗4 重載 void complex:print() cout0) cout+; if(imag!=0)coutimagin; complex operator + (complex plex b) /重載+定義 complex temp; temp.real=a.real+b.real; temp.imag=a.imag+b.imag; return temp; 實驗4 重載 complex operator - (complex plex b) /重載-定義 complex temp; temp.real=a.real-b.real; temp.
42、imag=a.imag-b.imag; return temp; complex operator * (complex plex b) /重載*定義 complex temp; temp.real=a.real*b.real-a.imag*b.imag; temp.imag=a.real*b.imag+a.imag*b.real; return temp; 實驗4 重載 complex operator / (complex plex b) /重載/定義 complex temp; float tt; tt=1/(b.real*b.real+b.imag*b.imag); temp.real
43、=(a.real*b.real+a.imag*b.imag)*tt; temp.imag=(b.real*a.imag-a.real*b.imag)*tt; return temp; 實驗4 重載main()complex c1(2.3,4.6),c2(3.6,2.8),c3;c1.print();c2.print();c3=c1+c2;c3.print();c3=c2-c1;c3.print();c3=c1*c2;c3.print();c3=c1/c2;c3.print();return 1;實驗4 重載例5.有一個計數(shù)器類,要對它定義兩個重載運算符+,-.#include class co
44、unterunsigned int value;public:counter()value=0;void operator +();void operator -();int get()return value;實驗4 重載void counter:operator +()if(value0) value-;實驗4 重載main()counter c1;for (int i=0;i10;i+)c1+;coutc1.get()n;c1-;c1-;coutc1.get()n;return 1;實驗4 重載例6.假如有一個實數(shù)矩陣,需要對它進行加法、減法和乘法運算,并且重載運算符 (),用來返回矩陣
45、元素的值。 #include class matrixshort rows,cols;double *elems;public:matrix(short rows,short cols);matrix(); double operator ()(short row,short col); /重載運算符“()”,用來返回元素值void setelem(short row,short col,double val); /為矩陣元素賦值friend matrix operator + ( matrix p,matrix q); /實現(xiàn)矩陣相加 friend matrix operator - ( m
46、atrix p,matrix q); /實現(xiàn)矩陣相減 void print(); /輸出矩陣中各元素;實驗4 重載matrix:matrix(short rows,short cols)matrix:rows=rows;matrix:cols=cols;elems=new doublerows*cols; /為矩陣動態(tài)分配內(nèi)存inline matrix:matrix() delete elems; /釋放矩陣所占內(nèi)存實驗4 重載double matrix:operator () (short row,short col)return (row=1 &row=1 &col=1 &row=1 &c
47、ol=cols)elems(row-1)*cols+(col-1)=val;實驗4 重載matrix operator + (matrix p,matrix q)matrix m(p.rows,p.cols);if(p.rows!=q.rows|p.cols!=q.cols)return m;for (int r=1;r=p.rows;r+)for (int c=1;c=p.cols;c+)m.setelem(r,c,p(r,c)+q(r,c);return m;實驗4 重載matrix operator - (matrix p,matrix q)matrix m(p.rows,p.cols)
48、;if(p.rows!=q.rows|p.cols!=q.cols)return m;for (int r=1;r=p.rows;r+)for (int c=1;c=p.cols;c+)m.setelem(r,c,p(r,c)-q(r,c);return m;實驗4 重載void matrix:print()for (int r=1;rrows;+r)for (int c=1;ccols;c+)cout(*this)(r,c) ;coutn;實驗4 重載main()matrix a(2,3),b(2,3),d(2,3);a.setelem(1,1,1.0);a.setelem(1,2,3.0)
49、;a.setelem(1,3,3.0);a.setelem(2,1,4.0);a.setelem(2,2,5.0);a.setelem(2,3,6.0); b.setelem(1,1,1.0); b.setelem(1,2,2.0); b.setelem(1,3,3.0); b.setelem(2,1,4.0); b.setelem(2,2,5.0); b.setelem(2,3,6.0); 實驗4 重載 a.print;b.print;d=a+b;d.print();d=a-b;d.print();實驗4 重載4.應(yīng)用編程任務(wù)(1)有兩個矩陣a和b,均為2行3列。求兩個矩陣之和。重載運算符”
50、+”,使之能用于矩陣相加。如c=a+b。(2)定義一個復數(shù)類Complex,重載運算符”+”,使之能用于復數(shù)的加法運算。參加運算的兩個運算量可以都是類對象,也可以其中一個是整數(shù),順序任意。如c1+c2,i+c1,c1+i均合法(設(shè)i為整數(shù),c1,c2為復數(shù))。編程序,分別求兩個復數(shù)之和,整數(shù)和復數(shù)之和。實驗5 多態(tài)與虛函數(shù)1.實驗?zāi)康?(1)理解C+多態(tài)、虛函數(shù)、抽象類。 (2)學習使用虛函數(shù)和多態(tài)編程。 (3)學習抽象類編程。2.實驗基本要求 (1)掌握虛函數(shù)和多態(tài)的使用。 (2)掌握抽象類的使用。3.實驗例題實驗5 多態(tài)與虛函數(shù)例1:虛函數(shù)的定義及使用#includeclass basep
51、ublic:virtual void who()coutbasen;class first:public basepublic:void who()coutthe first derivationn;實驗5 多態(tài)與虛函數(shù)class second:public basepublic:void who()coutwho(); /調(diào)用base類的who()ptr=&obj2;ptr-who(); /調(diào)用first類的who()ptr=&obj3;ptr-who(); /調(diào)用second類的who()return 1;實驗5 多態(tài)與虛函數(shù)例2.多態(tài)例示 #include class base publ
52、ic: virtual void fn() cout In base classn; ; class subclass:public base public: virtual void fn() cout In subclassn; ;實驗5 多態(tài)與虛函數(shù) void test(base &b) b.fn(); void main() base bc; subclass sc; test(bc); test(sc); 實驗5 多態(tài)與虛函數(shù) 例3.姓氏運動會 /多態(tài)例示 李氏兩兄妹(哥哥和妹妹)參加姓氏運動會(不同姓氏組隊參加),哥哥男子項目比賽,妹妹參加女子項目比賽, 開幕式有一個參賽隊伍代表發(fā)
53、言儀式,兄妹倆都想去露露臉,可只能一人去,最終他們決定到時抓鬮決定,而組委會也不反對, 它才不關(guān)心是哥哥還是妹妹來發(fā)言,只要派一個姓李的來說兩句話就行。實驗5 多態(tài)與虛函數(shù) #include class Li public: virtual void say() ; class Li_brother : public Li public: virtual void say() cout Im Li ming, I have 3 sports: boxing, fencing and wrestling. endl; void boxing() cout boxing endl; void fe
54、ncing() cout fencing endl; void wrestling() cout wrestling endl; ;實驗5 多態(tài)與虛函數(shù) class Li_sister : public Li public: virtual void say() cout Im Li xia, I have 2 sports: swim and skating. endl; void swim() cout swim endl; void skating() cout skatingsay(); int main() Li *p; /基類指針,李家代表 Li_brother b; Li_sis
55、ter s; p = &s; /基類指針指向派生類Li_sister對象,獲得發(fā)言機會 Speak(p); return 0; 實驗5 多態(tài)與虛函數(shù)例4 現(xiàn)有三角形、正方形和圓形三種圖形,求它們各自的面積。可以從它們抽象出一個基類,在基類中聲明一個虛函數(shù),用來求面積,并利用單界面、多實現(xiàn)版本設(shè)計各個圖形求面積的方法。程序如下:#include class figure /定義一個公共的基類protected: int x,y;public: figure(int x,int y) figure:x=x; figure:y=y; virtual void show_area() /定義一個界面接
56、口 coutno area for this claaan;實驗5 多態(tài)與虛函數(shù)class triangle:public figure /定義三角形類,基類成員x為底邊長,y為三角形的高public: triangle(int x,int y):figure(x,y); void show_area() /三角形類的虛函數(shù)版本 coutthe area of this triangle is:x*y*0.5n;class square:public figure /定義正方形類,基類的數(shù)據(jù)成員為正方形的邊長public: square(int x):figure(x,x); void sho
57、w_area() /正方形類的虛函數(shù)版本 coutthe area of this square is:x*xn;實驗5 多態(tài)與虛函數(shù) class circles:public figure /定義圓類,基類的數(shù)據(jù)成員為圓的半徑public: circles(int x):figure(x,x); void show_area() coutthe area of this circle is:x*x*3.1416show_area(); ptr=&squa; /指針ptr指向正方形類對象ptr-show_area();ptr= /指針ptr指向圓類對象ptr-show_area();retur
58、n 1; 實驗5 多態(tài)與虛函數(shù) 例5 .貓和狗 /多態(tài)例示,抽象類真正的好處。 家里養(yǎng)了一些類型的寵物,如貓和狗??梢责B(yǎng)新的寵物,并能叫出家里所有寵物的名字。#include #include using namespace std;class Pet public: string name; Pet(string s): name(s) ; virtual void printname()=0 ; /printname()純虛函數(shù),在派生類中不同的實現(xiàn) ;實驗5 多態(tài)與虛函數(shù)class Dog : public Pet public: Dog(string s): Pet(s); void
59、printname() cout this is : name endl; ; class Cat : public Pet public: Cat(string s): Pet(s); void printname() cout this is : name endl; ;實驗5 多態(tài)與虛函數(shù)class Home private: int nPets; Pet * pPets20; /基類指針,可指向各派生類對象,表現(xiàn)多態(tài) public: void Add(Pet * pPet) pPetsnPets+ = pPet; void printAll() for(int i = 0; i pri
60、ntname(); /運行時多態(tài)性 Home():nPets(0) ;實驗5 多態(tài)與虛函數(shù) int main() Home myhome; myhome.Add(new Dog(dog1); myhome.Add(new Dog(dog2); myhome.Add(new Dog(dog3); myhome.Add(new Cat(cat1); myhome.Add(new Cat(cat2); myhome.Add(new Cat(cat3); myhome.printAll(); return 0; 實驗5 多態(tài)與虛函數(shù)4. 任務(wù):1.先建立一個Point類,包含數(shù)據(jù)成員x,y。以它為基類
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 路基灰土施工方案
- 2025年護理要解剖學試題及答案
- 基于涉入理論的高爾夫球手地方依戀研究
- 5年級上冊第5單元
- 4年級下冊人教版要寫的字第七課
- 4大發(fā)明英語簡短50字左右
- 礦用管路安裝施工方案
- 站臺墻施工方案
- 【HR必看】房地產(chǎn)公司三級管控體系優(yōu)化案例
- 2025年湖北省荊門市單招職業(yè)傾向性測試題庫及參考答案1套
- (綜合治理)修復工程指南(試行) - 貴州省重金屬污染防治與土壤修復網(wǎng)
- 員工就餐簽到表
- A-level項目介紹(課堂PPT)
- 證明銀行賬戶公戶轉(zhuǎn)個人戶
- 航海計算軟件---ETA計算器
- 光伏電站運維手冊
- 南京連續(xù)運行衛(wèi)星定位綜合服務(wù)系統(tǒng)
- 半導體及集成電路領(lǐng)域的撰寫及常見問題
- 2000年考研英語真題及答案
- 設(shè)計成果接收單確認單及付款申請表
- 水保及環(huán)保管理體系與措施
評論
0/150
提交評論