版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、西安交通大学实验报告课程_C+程序设计_ 实验名称_多态性_第 1 页 共 页系 别_材料科学与工程_ 实 验 日 期 2014年 12 月 12 日专业班级_材料_组别_ 实 验 报 告 日 期 2014年 12 月 15 日姓 名_ _学号_ 报 告 退 发 ( 订正 、 重做 )同 组 人_ 教 师 审 批 签 字 一. 实验目的1掌握虚函数的定义和使用方法2掌握纯虚函数和抽象类的定义和使用方法3掌握运算符重载函数的定义和使用方法二. 实验内容()实验题目一:定义一个哺乳动物Mammal类,再由此派生出狗Dog类。二者都定义speak()成员函数,基类中定义为虚函数,定义一个Dog类的对
2、象,调用speak函数,观察运行结果。 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.实验结果(二)实验题目二:编写一个存储艺术作品的程序。艺术作品分为三类:Painting,Music和Chamber,Chamber是Music中的一类。要求如下:(1)每件作品都要标明著者,作品标题,作品诞生年份及其所属分类。(2)Painting类要求显示画的高和宽等尺寸信息。(3)Music要求显示能够代表其中内容的关键信息。(4)Chamber类要求显示其中包括的演奏人员的数目。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、;< "标题:" << 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、;作品风格:"<< 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 <<"演奏人数:"<< 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("周杰伦", "东风破", "2006-12-12", "音乐", "中国风音乐");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("周杰伦", "东风破", "2006-12-12", "音乐", "中国风音乐", 20);c.sh
10、ow();2 实验结果(三)实验题目三:定义矩阵类,重载运算符“+”和“-”,实现两个矩阵相加减。编写主函数验证类的功能,要求第一个矩阵的值由构造函数设置,第二个矩阵的值由键盘输入。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 实验结果(四)实验题目四:定义一个一元二次方程类,通过继承方式定义一元三次方程
14、类,再继承定义一元四次方程类。类中至少包含构造函数、求根函数、运算符 + 重载函数、运算符 - 重载函数、运算符=重载函数、输出方程的函数等6 个函数,并编写主函数测试各成员函数。提示: 两个一元三次方程对应相加仍然是一个一元三次方程; 求根方法采用迭代方法,迭代公式为: Xn+1=Xn F(Xn)/F (Xn) ,结束迭代的条件 |F(Xn+1)|<10-7 与 |Xn+1-Xn|<10-7 同时成立; 一元三次方程的一般形式如下: F(X)=AX3+BX2+CX+D=0 。输出方程格式为: A*X3+B*X2+C*X+D=0 ;两个一元三次方程对应相加和对应相减仍然是一元三次方
15、程。假定类中的方程系数能求解出实根。不考虑方程存在虚根和无根的情况。求根函数应该有一个参数,该参数指明迭代初值。例如方程 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附近的根。因此类中数据成员除了系数外,还应考虑迭代初值作为数据成员。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 << "默认一元二次方程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 << "默认一元三次函数ee1:" << endl;ee1.show();cout << "一元三次函数
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 << "默认一元四次函数类eee1:" << endl;eee1.show();cout << "一元四次函数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 实验结果(五)实验题目五:给自定义虚数类增加运算符*、/、+、-、=重载,注意+和-分前置和后置两种情况,编写主函数加以测试。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 实验结果(六)实验题目六:完成上周蛋糕类的定义中运算符重载,即完成上周第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 << "第一个
51、派生类:" << endl;fangcake f(r, h, c1, bianchang, h1, c1);f.output();f.show();cout << "圆柱形_方柱形蛋糕的总体积为:" << f.V() << endl;cout << "圆柱形_方柱形蛋糕的总表面积为:" << f.S() << endl;cout << "第二个派生类:" << 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 << "请输入第二个三层蛋糕的基本信息(祝词,圆柱半径、高,方柱边长、高,菱柱长轴、短轴、高):" <<
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 << "第一个派生类:" << endl;fangcake f1(r, h, c1, bianchang, h1, c1);f1.output();f1.show();cout << "圆柱形_方柱形蛋糕的总体积为:" << f1.V() << endl;cout << "圆柱形_方柱形蛋糕的总表面积为:" << f1.S() << endl;cout << "第二个派生类:" <&
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 << "第一个蛋糕的总体积大。n"elsecout << "第二个蛋糕的总体积大。n"return 0;2.(七)实验题目七:定义大整数类(例如1000位),成员函数包括加、减以及判大小,注意用运算符重载,编写主
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 住院基地激励奖惩制度
- 集体经济带头人奖惩制度
- 保健室奖惩制度细则
- 小朋友趣味奖惩制度大全
- 物业管理保洁员奖惩制度
- 文具店员工奖惩制度范本
- 村干部双禁期间奖惩制度
- 基层土管员奖惩制度汇编
- 中学生家庭奖惩制度
- 申通快递员工奖惩制度
- 传媒行业编导岗位招聘考试试卷及答案
- 江苏护理历年单招题库及答案解析
- 2025年农村房屋租赁合同协议
- 模版倾覆应急预案
- 2025年易性症测试题及答案
- 护理标准操作规程(SOP)全集
- 折弯工技能等级评定标准
- DB51T 3062-2023 四川省高标准农田建设技术规范
- 胃肠外科完整病历范文(4篇)
- 2024年上、下半年(小学)教师资格证【小学教育教学知识与能力】2套 真题及答案
- 《机械基础》课件 第一章 绪论
评论
0/150
提交评论