c++程序設(shè)計(jì)_ (4)_第1頁
c++程序設(shè)計(jì)_ (4)_第2頁
c++程序設(shè)計(jì)_ (4)_第3頁
c++程序設(shè)計(jì)_ (4)_第4頁
c++程序設(shè)計(jì)_ (4)_第5頁
已閱讀5頁,還剩22頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、西安交通大學(xué)實(shí)驗(yàn)報(bào)告課程_C+程序設(shè)計(jì)_ 實(shí)驗(yàn)名稱_多態(tài)性_第 1 頁 共 頁系 別_材料科學(xué)與工程_ 實(shí) 驗(yàn) 日 期 2014年 12 月 12 日專業(yè)班級(jí)_材料_組別_ 實(shí) 驗(yàn) 報(bào) 告 日 期 2014年 12 月 15 日姓 名_ _學(xué)號(hào)_ 報(bào) 告 退 發(fā) ( 訂正 、 重做 )同 組 人_ 教 師 審 批 簽 字 一. 實(shí)驗(yàn)?zāi)康?掌握虛函數(shù)的定義和使用方法2掌握純虛函數(shù)和抽象類的定義和使用方法3掌握運(yùn)算符重載函數(shù)的定義和使用方法二. 實(shí)驗(yàn)內(nèi)容()實(shí)驗(yàn)題目一:定義一個(gè)哺乳動(dòng)物Mammal類,再由此派生出狗Dog類。二者都定義speak()成員函數(shù),基類中定義為虛函數(shù),定義一個(gè)Dog類的對

2、象,調(diào)用speak函數(shù),觀察運(yùn)行結(jié)果。 1 程序源代碼#include<iostream>using namespace std;class mammalpublic:virtual void speak()cout<<"zzz!"<<endl;class dog:public mammalpublic:void speak()cout<<"wangwang!"<<endl;void main()dog d;cout<<"dog類對象speak:"<<

3、endl;d.speak();mammal m;cout<<"mammal類對象speak:"<<endl;m.speak();2.實(shí)驗(yàn)結(jié)果(二)實(shí)驗(yàn)題目二:編寫一個(gè)存儲(chǔ)藝術(shù)作品的程序。藝術(shù)作品分為三類:Painting,Music和Chamber,Chamber是Music中的一類。要求如下:(1)每件作品都要標(biāo)明著者,作品標(biāo)題,作品誕生年份及其所屬分類。(2)Painting類要求顯示畫的高和寬等尺寸信息。(3)Music要求顯示能夠代表其中內(nèi)容的關(guān)鍵信息。(4)Chamber類要求顯示其中包括的演奏人員的數(shù)目。1 程序源代碼#include&l

4、t;iostream>#include<cstring>using namespace std;class artpublic:char au20;char ti20;char ye20;char cl20;void set(char a, char t, char y, char c)strcpy_s(au, a);strcpy_s(ti, t);strcpy_s(ye, y);strcpy_s(cl, c);virtual void show()cout << "作者:" << au << endl;cout <

5、;< "標(biāo)題:" << ti << endl;cout << "年代:" << ye << endl;cout << "分類:" << cl << endl;class painting :public artpublic:double le, wi;void set(char a, char t, char y, char c, double l, double w)art:set(a, t, y, c);le = l, wi =

6、w;void show()art:show();cout << "畫長:" << le << endl;cout << "畫寬:" << wi << endl;class music :public artpublic:char xx50;void set(char a, char t, char y, char c, char x)art:set(a, t, y, c);strcpy_s(xx, x);void show()art:show();cout <<"

7、;作品風(fēng)格:"<< xx << endl;class chamber :public musicpublic:int num;void set(char a, char t, char y, char c, char x, int n)music:set(a, t, y, c, x);num = n;void show()music:show();cout <<"演奏人數(shù):"<< num << endl;void main()art a;music m;painting p;chamber c;cout

8、<< "art類信息:" << endl;a.set("歐陽詢", "玄秘塔碑", "1000-09-23", "書法");a.show();m.set("周杰倫", "東風(fēng)破", "2006-12-12", "音樂", "中國風(fēng)音樂");cout << "music類信息:" << endl;m.show();p.set(&qu

9、ot;李端", "清明上河圖", "09002-2", "繪畫", 60.2, 34);cout << "painting類信息:" << endl;p.show();cout << "chamber類信息:" << endl;c.set("周杰倫", "東風(fēng)破", "2006-12-12", "音樂", "中國風(fēng)音樂", 20);c.sh

10、ow();2 實(shí)驗(yàn)結(jié)果(三)實(shí)驗(yàn)題目三:定義矩陣類,重載運(yùn)算符“+”和“-”,實(shí)現(xiàn)兩個(gè)矩陣相加減。編寫主函數(shù)驗(yàn)證類的功能,要求第一個(gè)矩陣的值由構(gòu)造函數(shù)設(shè)置,第二個(gè)矩陣的值由鍵盤輸入。1 程序源代碼#include<iostream>#include<cmath>using namespace std;class juzhenint ju32;public:juzhen();juzhen(int p32)for (int i = 0; i<3; i+)for (int j = 0; j<2; j+)juij = pij;void show()for (int

11、i = 0; i<3; i+)for (int j = 0; j<2; j+)cout << juij << " "cout << endl;juzhen operator+(juzhen c2)juzhen c3;for (int i = 0; i<3; i+)for (int j = 0; j<2; j+)c3.juij = juij + c2.juij;return c3;juzhen operator-(juzhen c2)juzhen c3;for (int i = 0; i<3; i+)for (

12、int j = 0; j<2; j+)c3.juij = juij - c2.juij;return c3;int main()int o32;o00 = 1;o01 = 2;o10 = 3;o11 = 4;o20 = 5;o21 = 6;juzhen p1(o);cout << "A矩陣為" << endl;p1.show();int p32;cout << "請輸入矩陣的元素:(3行2列)" << endl;for (int i = 0; i<3; i+)for (int j = 0; j&

13、lt;2; j+)cin >> pij;juzhen p2(p);cout << "B矩陣為" << endl;p2.show();juzhen p3;p3 = p2 + p1;cout << "A+B=" << endl;p3.show();juzhen p4;p4 = p1 - p2;cout << "A-B=" << endl;p4.show();return 0;2 實(shí)驗(yàn)結(jié)果(四)實(shí)驗(yàn)題目四:定義一個(gè)一元二次方程類,通過繼承方式定義一元三次方程

14、類,再繼承定義一元四次方程類。類中至少包含構(gòu)造函數(shù)、求根函數(shù)、運(yùn)算符 + 重載函數(shù)、運(yùn)算符 - 重載函數(shù)、運(yùn)算符=重載函數(shù)、輸出方程的函數(shù)等6 個(gè)函數(shù),并編寫主函數(shù)測試各成員函數(shù)。提示: 兩個(gè)一元三次方程對應(yīng)相加仍然是一個(gè)一元三次方程; 求根方法采用迭代方法,迭代公式為: Xn+1=Xn F(Xn)/F (Xn) ,結(jié)束迭代的條件 |F(Xn+1)|<10-7 與 |Xn+1-Xn|<10-7 同時(shí)成立; 一元三次方程的一般形式如下: F(X)=AX3+BX2+CX+D=0 。輸出方程格式為: A*X3+B*X2+C*X+D=0 ;兩個(gè)一元三次方程對應(yīng)相加和對應(yīng)相減仍然是一元三次方

15、程。假定類中的方程系數(shù)能求解出實(shí)根。不考慮方程存在虛根和無根的情況。求根函數(shù)應(yīng)該有一個(gè)參數(shù),該參數(shù)指明迭代初值。例如方程 2X3-4X2+3x-6 0 在 1.5 附近的根。又例如方程 X3+12X2+48X+64=0 在 -4.5 附近的根。例如方程X4-10X3+35X2-50x+240在4.5附近的根。又例如方程X4+12X3+54X2+108X+81=0在-1.5附近的根。因此類中數(shù)據(jù)成員除了系數(shù)外,還應(yīng)考慮迭代初值作為數(shù)據(jù)成員。1 程序源代碼#include <iostream>#include <cmath>using namespace std;class

16、 fc2protected:float A, B, C;public:fc2()A = 1; B = 1; C = 1;fc2(float A, float B, float C)this->A = A;this->B = B;this->C = C;void Root(float x)float x0, x1 = x;dox0 = x1;x1 = x0 - (A*x0*x0 + B*x0 + C) / (2 * A*x0 + B); while (fabs(x0 - x1)>1.0E-7 && fabs(A*x1*x1 + B*x1 + C)>1

17、.0E-7);cout << "方程在" << x << "附近的根為:" << x1 << endl;float GetA() return A; ;float GetB() return B; ;float GetC() return C; ;fc2 operator +(fc2);fc2 operator -(fc2);fc2 operator =(fc2);void show()if (A != 0)cout << A << "x2"if (B

18、>0)cout << "+" << B << "x"else if (B<0)cout << B << "x"if (C>0)cout << "+" << C;else if (C<0)cout << C;cout << "=0" << endl;fc2 fc2:operator +(fc2 e)fc2 temp;temp.A = A + e.A;tem

19、p.B = B + e.B;temp.C = C + e.C;return temp;fc2 fc2:operator -(fc2 e)fc2 temp;temp.A = A - e.A;temp.B = B - e.B;temp.C = C - e.C;return temp;fc2 fc2:operator =(fc2 e)A = e.A;B = e.B;C = e.C;return *this;class fc3 :public fc2protected:float D;public:fc3()A = GetA(); B = GetB(); C = GetC();D = 1;float

20、GetD() return D; ;fc3(float A, float B, float C, float D) : fc2(A, B, C)this->D = D;void Root(float x)float A = GetA(), B = GetB(), C = GetC();float x0, x1 = x;dox0 = x1;x1 = x0 - (D*x0*x0*x0 + A*x0*x0 + B*x0 + C) / (3 * D*x0*x0 + 2 * A*x0 + B); while (fabs(x0 - x1)>1.0E-7 && fabs(D*x1

21、*x1*x1 + A*x1*x1 + B*x1 + C)>1.0E-7);cout << "方程在" << x << "附近的根為:" << x1 << endl;fc3 operator +(fc3);fc3 operator -(fc3);fc3 operator =(fc3);void show()if (D != 0)cout << D << "x3"if (GetA()>0)cout << "+"

22、<< GetA() << "x2"else if (GetA()<0)cout << GetA() << "x2"if (GetB()>0)cout << "+" << GetB() << "x"else if (GetB()<0)cout << GetB() << "x"if (GetC()>0)cout << "+" <&l

23、t; GetC();else if (GetC()<0)cout << GetC();cout << "=0" << endl;fc3 fc3:operator +(fc3 e)fc3 temp;temp.A = GetA() + e.A;temp.B = GetB() + e.B;temp.C = GetC() + e.C;temp.D = D + e.D;return temp;fc3 fc3:operator -(fc3 e)fc3 temp;temp.A = GetA() - e.A;temp.B = GetB() - e.

24、B;temp.C = GetC() - e.C;temp.D = D - e.D;return temp;fc3 fc3:operator =(fc3 e)A = e.A;B = e.B;C = e.C;D = e.D;return *this;class fc4 :public fc2protected:float D, E;public:fc4()A = 1; B = 1; C = 1; D = 1; E = 1;fc4(float A, float B, float C, float D, float E) :fc2(A, B, C)this->D = D;this->E =

25、 E;void Root(float x)float x0, x1 = x;dox0 = x1;x1 = x0 - (E*x0*x0*x0*x0 + D*x0*x0*x0 + A*x0*x0 + B*x0 + C) / (4 * E*x0*x0*x0 + 3 * D*x0*x0 + 2 * A*x0 + B); while (fabs(x0 - x1)>1.0E-7 && fabs(E*x1*x1*x1*x1 + D*x1*x1*x1 + A*x1*x1 + B*x1 + C)>1.0E-7);cout << "方程在" <&l

26、t; x << "附近的根為:" << x1 << endl;fc4 operator +(fc4);fc4 operator -(fc4);fc4 operator =(fc4);void show()if (E != 0)cout << E << "x4"if (D>0)cout << "+" << D << "x3"else if (D<0)cout << D << "

27、x3"if (A>0)cout << "+" << A << "x2"else if (A<0)cout << A << "x2"if (B>0)cout << "+" << B << "x"else if (B<0)cout << B << "x"if (C>0)cout << "+"

28、 << C;else if (C<0)cout << C;cout << "=0" << endl;fc4 fc4:operator +(fc4 e)fc4 temp;temp.A = A + e.A;temp.B = B + e.B;temp.C = C + e.C;temp.D = D + e.D;temp.E = E + e.E;return temp;fc4 fc4:operator -(fc4 e)fc4 temp;temp.A = A - e.A;temp.B = B - e.B;temp.C = C - e

29、.C;temp.D = D - e.D;temp.E = E - e.E;return temp;fc4 fc4:operator =(fc4 e)A = e.A;B = e.B;C = e.C;D = e.D;E = e.E;return *this;int main()fc2 e1, e2(1, 2, 3), e3, e4(1, 2, 1);cout << "默認(rèn)一元二次方程e1:" << endl;e1.show();cout << "一元二次方程e2:" << endl;e2.show();cout

30、 << "e1+e2="e3 = e1 + e2;e3.show();cout << "e1-e2="e3 = e1 - e2;e3.show();e4.show();e4.Root(1.5);fc3 ee1(1, 1, 1, 1), ee2(1, 2, 3, 4), ee3, ee4(-4, 3, -6, 2), ee5(12, 48, 64, 1);cout << "默認(rèn)一元三次函數(shù)ee1:" << endl;ee1.show();cout << "一元三次函數(shù)

31、ee2:" << endl;ee2.show();cout << "ee1+ee2="ee3 = ee1 + ee2;ee3.show();cout << "ee1-ee2="ee3 = ee1 - ee2;ee3.show();ee4.show();ee4.Root(1.5);ee5.show();ee5.Root(-4.5);fc4 eee1(1, 1, 1, 1, 1), eee2(1, 2, 3, 4, 5), eee3, eee4(35, -50, 24, -10, 1), eee5(54, 108

32、, 81, 12, 1);cout << "默認(rèn)一元四次函數(shù)類eee1:" << endl;eee1.show();cout << "一元四次函數(shù)eee2:" << endl;eee2.show();eee3 = eee1 + eee2;cout << "eee1+eee2="eee3.show();cout << "eee1-eee2="eee3 = eee1 - eee2;eee3.show();eee4.show();eee4.Root(

33、4.5);eee5.show();eee5.Root(-1.5);return 0;1 2 實(shí)驗(yàn)結(jié)果(五)實(shí)驗(yàn)題目五:給自定義虛數(shù)類增加運(yùn)算符*、/、+、-、=重載,注意+和-分前置和后置兩種情況,編寫主函數(shù)加以測試。1 程序源代碼#include <iostream>using namespace std;class Complexdouble real;double imag;public:Complex()real = 0;imag = 0;Complex(double r, double i)real = r;imag = i;double Real() return re

34、al; double Imag() return imag; Complex operator *(Complex);Complex operator /(Complex);Complex operator +();Complex operator +(int);Complex operator -();Complex operator -(int);Complex operator =(Complex);void show()if (real = 0)if (imag = 0)cout << "0"elsecout << imag <<

35、 "i"elsecout << real;if (imag>0)cout << "+" << imag << "i"else if (imag<0)cout << imag << "i"Complex Complex:operator *(Complex c)Complex temp;temp.real = real*c.real - imag*c.imag;temp.imag = real*c.imag + imag*c.real

36、;return temp;Complex Complex:operator /(Complex c)Complex temp;temp.real = (real*c.real + imag*c.imag) / (c.imag*c.imag + c.real*c.real);temp.imag = (imag*c.real - real*c.imag) / (c.imag*c.imag + c.real*c.real);return temp;Complex Complex:operator +()real+;return *this;Complex Complex:operator +(int

37、)real += 2;return *this;Complex Complex:operator -()real-;return *this;Complex Complex:operator -(int)real -= 2;return *this;Complex Complex:operator =(Complex c)real = c.real;imag = c.imag;return *this;int main()Complex c1(1, 1), c2(1, 2), c3;cout << "c1="c1.show();cout << &qu

38、ot;t"cout << "c2="c2.show();cout << endl;cout << "c1*c2="c3 = c1*c2;c3.show();cout << endl;cout << "c1/c2="c3 = c1 / c2;c3.show();cout << endl;cout << "+c1="c1+;c1.show();cout << endl;cout << "c1+

39、="c1+;c1.show();cout << endl;cout << "-c2="c2-;c2.show();cout << endl;cout << "c2-="c2-;c2.show();cout << endl;return 0;2 實(shí)驗(yàn)結(jié)果(六)實(shí)驗(yàn)題目六:完成上周蛋糕類的定義中運(yùn)算符重載,即完成上周第6題。1. 程序源代碼#include<iostream>#include<cstring>#include<cmath>#define

40、PI 3.14159using namespace std;class yuanzhucakeprivate:float r;float h;char celebrate50;public:yuanzhucake(float a, float b, char *c)r = a;h = b;strcpy_s(celebrate, c);void init(float a, float b, char *c)r = a;h = b;strcpy_s(celebrate, c);double V()double V;V = PI*r*r*h;return V;double S()double S;S

41、 = 2 * PI*r*h + PI*r*r;return S;void output()cout << "圓柱形蛋糕生日祝詞為:" << celebrate << endl;void show()cout << "圓柱形蛋糕信息如下:" << endl;cout << "半徑:" << 't' << r << 't' << "高:" << 't

42、' << h << endl;class fangcake :public yuanzhucakeprivate:float bianchang;float h1;char c150;public:fangcake(float a, float b, char *c, float bianchang1, float h2, char *c2) :yuanzhucake(a, b, c)bianchang = bianchang1;h1 = h2;strcpy_s(c1, c2);void init(float r1, float h1, char *c, flo

43、at bianchang1, float h2, char *c2)yuanzhucake:init(r1, h1, c);bianchang = bianchang1;h1 = h2;strcpy_s(c1, c2);double V()double V;V = bianchang*bianchang*h1 + yuanzhucake:V();return V;double S()double S;S = bianchang*bianchang + 4 * bianchang*h1 + yuanzhucake:S();return S;void output()yuanzhucake:out

44、put();cout << "方柱形蛋糕生日祝詞為:" << c1 << endl;void show()yuanzhucake:show();cout << "方柱形蛋糕信息如下:" << endl;cout << "邊長:" << 't' << bianchang << 't' << "高:" << 't' << h1 &

45、lt;< endl;class lingxingcake :public fangcakeprivate:float changzhou;float duanzhou;float h2;char c250;double v;public:lingxingcake(float r, float h, char *celebrate, float bianchang, float h1, char *c1, float changzhou1, float duanzhou1, float h3, char *c3) :fangcake(r, h, celebrate, bianchang,

46、h1, c1)changzhou = changzhou1;duanzhou = duanzhou1;h2 = h3;strcpy_s(c2, c3);void init(float r, float h, char *celebrate, float bianchang, float h1, char *c1, float changzhou1, float duanzhou1, float h3, char *c3)fangcake:init(r, h, celebrate, bianchang, h1, c1);changzhou = changzhou1;duanzhou = duan

47、zhou1;h2 = h3;strcpy_s(c2, c3);double V()v = changzhou*duanzhou*h2 / 2 + fangcake:V();return v;double S()double S;S = changzhou*duanzhou / 2 + 2 * h2*sqrt(changzhou*changzhou + duanzhou*duanzhou) + fangcake:S();return S;void output()fangcake:output();cout << "菱柱形蛋糕生日祝詞為:" << c2

48、 << endl;void show()fangcake:show();cout << "菱柱形蛋糕信息如下:" << endl;cout << "長軸:" << 't' << changzhou << 't' << "短軸:" << 't' << duanzhou << "高:" << 't' <<

49、; h2 << endl;bool operator>(lingxingcake &b)if (v>b.v)return true;elsereturn false;int main()float r;float h;float bianchang;float h1;char c150;float changzhou;float duanzhou;float h2;cout << "請輸入三層蛋糕的基本信息(祝詞,圓柱半徑、高,方柱邊長、高,菱柱長軸、短軸、高):" << endl;cin >> c1 &g

50、t;> h >> r >> bianchang >> h1 >> changzhou >> duanzhou >> h2;yuanzhucake y(r, h, c1);y.output();y.show();cout << "圓柱形蛋糕的體積為:" << y.V() << endl;cout << "圓柱形蛋糕的表面積為:" << y.S() << endl;cout << "第一個(gè)

51、派生類:" << endl;fangcake f(r, h, c1, bianchang, h1, c1);f.output();f.show();cout << "圓柱形_方柱形蛋糕的總體積為:" << f.V() << endl;cout << "圓柱形_方柱形蛋糕的總表面積為:" << f.S() << endl;cout << "第二個(gè)派生類:" << endl;lingxingcake l(r, h, c1,

52、 bianchang, h1, c1, changzhou, duanzhou, h2, c1);l.output();l.show();cout << "圓柱形_方柱形_菱柱形蛋糕的總體積為:" << l.V() << endl;cout << "圓柱形_方柱形_菱柱形蛋糕的總表面積為:" << l.S() << endl;cout << "請輸入第二個(gè)三層蛋糕的基本信息(祝詞,圓柱半徑、高,方柱邊長、高,菱柱長軸、短軸、高):" <<

53、endl;cin >> c1 >> h >> r >> bianchang >> h1 >> changzhou >> duanzhou >> h2;yuanzhucake y1(r, h, c1);y1.output();y1.show();cout << "圓柱形蛋糕的體積為:" << y1.V() << endl;cout << "圓柱形蛋糕的表面積為:" << y1.S() <<

54、endl;cout << "第一個(gè)派生類:" << endl;fangcake f1(r, h, c1, bianchang, h1, c1);f1.output();f1.show();cout << "圓柱形_方柱形蛋糕的總體積為:" << f1.V() << endl;cout << "圓柱形_方柱形蛋糕的總表面積為:" << f1.S() << endl;cout << "第二個(gè)派生類:" <&

55、lt; endl;lingxingcake l1(r, h, c1, bianchang, h1, c1, changzhou, duanzhou, h2, c1);l1.output();l1.show();cout << "圓柱形_方柱形_菱柱形蛋糕的總體積為:" << l1.V() << endl;cout << "圓柱形_方柱形_菱柱形蛋糕的總表面積為:" << l1.S() << endl;if (l>l1)cout << "第一個(gè)蛋糕的總體積大。n"elsecout << "第二個(gè)蛋糕的總體積大。n"return 0;2.(七)實(shí)驗(yàn)題目七:定義大整數(shù)類(例如1000位),成員函數(shù)包括加、減以及判大小,注意用運(yùn)算符重載,編寫主

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(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

提交評論