您好,欢迎访问三七文档
实验项目名称:多态性实现学号:上机实践日期:2017/6/4实验项目编号:实验3组号:上机实践时间:14:00一、目的(1)掌握多态的概念。(2)理解静态多态性和动态动态性的含义。(3)掌握使用虚函数和继承实现动态多态性的方法。(4)掌握运算符重载的方法。二、实验内容与设计思想1.设有几何图形的派生关系如下图所示。几何图形geometric_shape)矩形rectangle圆circle三角形(triangle)长方体box)圆柱cylinder)圆锥cone)三棱柱(t_prism)三棱锥(t_pyramid)对平面图形可求周长和面积,对立体图形可以求体积以及底面图形的周长和底面积。设有主函数如下:intmain(){Geometric_shape*gs[]={newCircle(10),newRectangle(6,8),newTriangle(3,4,5),newBox(6,8,3),newCylinder(10,3),newCone(10,3),newT_pyramid(3,4,5,3),newT_prism(3,4,5,3)};for(inti=0;i8;i++){gs[i]-Show();coutendl;}for(i=0;i8;i++){gs[i]-Show();coutendl;}cout平面图形:endl;for(i=0;i3;i++){cout图形周长:gs[i]-perimeter()'\t';cout图形面积:gs[i]-area()'\t';cout图形体积:gs[i]-volume()endl;}cout立体图形:endl;for(i=3;i8;i++){cout图形底周长:gs[i]-perimeter()'\t';cout图形底面积:gs[i]-area()'\t';cout图形体积:gs[i]-volume()endl;}return0;}请编写各类的定义和实现代码,使给定的主函数main可以正确运行。实验代码:#includeiostream#includemath.husingnamespacestd;constdoublepi=3.14;classGeometric_shape//几何图形{public:virtualvoidshow(){};virtualdoubleperimeter(){return0;};//周长virtualdoublearea(){return0;};//面积virtualdoublevolume(){return0;};//体积};classCircle:publicGeometric_shape//圆{public:Circle(intr):radius(r){};virtualvoidshow(){cout圆的半径为:radiusendl;}virtualdoubleperimeter(){return2*pi*radius;}virtualdoublearea(){returnpi*radius*radius;}virtualdoublevolume(){return0;}protected:intradius;};classRectangle:publicGeometric_shape//矩形{public:Rectangle(intlenth,intwidth):Lenth(lenth),Width(width){}virtualvoidshow(){cout矩形长宽分别为:Lenth,Widthendl;}virtualdoubleperimeter(){return2*(Lenth+Width);}virtualdoublearea(){returnLenth*Width;}virtualdoublevolume(){return0;}protected:intLenth;intWidth;};classTriangle:publicGeometric_shape//三角形{public:Triangle(inta,intb,intc):A(a),B(b),C(c){};voidshow(){cout三角形三边分别为:A,B,Cendl;}virtualdoubleperimeter(){returnA+B+C;}virtualdoublearea(){doublep=(A+B+C)/2;returnsqrt(p*(p-A)*(p-B)*(p-C));}virtualdoublevolume(){return0;}protected:intA,B,C;};classBox:publicRectangle//长方形{public:Box(inta,intb,intc):Rectangle(a,b),Height(c){}voidshow(){cout长方体的长宽高分别为:Lenth,Width,Heightendl;}doublevolume(){returnarea()*Height;}private:intHeight;};classCylinder:publicCircle//圆柱{public:Cylinder(inta,intb):Circle(a),Height1(b){}voidshow(){cout圆柱底面半径和高度分别为:radius,Height1endl;}doublevolume(){returnarea()*Height1;}private:intHeight1;};classCone:publicCircle//圆锥{public:Cone(inta,intb):Circle(a),Height2(b){}voidshow(){cout圆锥底面半径和高度分别为:radius,Height2endl;}doublevolume(){returnarea()*Height2/3;}private:intHeight2;};classT_pyramid:publicTriangle//三棱锥{public:T_pyramid(inta,intb,intc,intd):Triangle(a,b,c),Height3(d){}voidshow(){cout三棱锥底边三角形三边和高度分别为:A,B,C,Height3endl;}doublevolume(){returnarea()*Height3/3;}private:intHeight3;};classT_prism:publicTriangle//三棱柱{public:T_prism(inta,intb,intc,intd):Triangle(a,b,c),Height4(d){}voidshow(){cout三棱柱底边三角形三边和高度分别为:A,B,C,Height4endl;}doublevolume(){returnarea()*Height4;}private:intHeight4;};intmain(){Geometric_shape*gs[]={NewCircle(10),newRectangle(6,8),newTriangle(3,4,5),newBox(6,8,3),newCylinder(10,3),newCone(10,3),newT_pyramid(3,4,5,3),newT_prism(3,4,5,3)};inti;for(i=0;i8;i++){gs[i]-show();coutendl;}cout平面图形:endl;for(i=0;i3;i++){cout图形周长:gs[i]-perimeter()'\t';cout图形面积:gs[i]-area()'\t';cout图形体积:gs[i]-volume()endl;}cout立体图形:endl;for(i=3;i8;i++){cout图形底周长:gs[i]-perimeter()'\t';cout图形底面积:gs[i]-area()'\t';cout图形体积:gs[i]-volume()endl;}return0;}2.为复数重载+、-运算符,编程实现(6+7i)+(7+8i)和(6+7i)-(7+8i)的运算。实验代码:#includeiostreamusingnamespacestd;classComplex{public:Complex(doubler=0.0,doublei=0.0):real(r),imag(i){}Complexoperator+(Complex&c2);//运算符“+”重载为成员函数Complexoperator-(Complex&c2);//运算符“-”重载为成员函数//friendComplexoperator+(Complex&c1,Complex&c2);//运算符“+”重载为友元函数//friendComplexoperator-(Complex&c1,Complex&c2);//运算符“-”重载为友元函数voiddisplay();//复数输出private:doublereal;doubleimag;};ComplexComplex::operator+(Complex&c2){Complexc;c.real=real+c2.real;c.imag=imag+c2.imag;returnc;}ComplexComplex::operator-(Complex&c2){Complexc;c.real=real-c2.real;c.imag=imag-c2.imag;returnc;}voidComplex::display(){coutreal;if(imag=0)cout+;coutimagiendl;}/*友元函数定义Complexoperator+(Complex&c1,Complex&c2){Complexc;c.real=c1.real+c2.real;c.imag=c1.imag+c2.imag;returnc;}Complexoperator-(Complex&c1,Complex&c2){Complexc;c.real=c1.real-c2.real;c.imag=c1.imag-c2.imag;returnc;}*/intmain(){Complexc1(6,7),c2(7,8);coutc1=;c1.display();coutc2=;c2.display();coutc1+c2=;(c1+c2).display();coutc1-c2=;(c1-c2).display();return0;}三、实验使用环境Windows10vs2017四、实验小结1)题(1)用虚函数来实现主程序中的动态联编。即在Geometric_shape类中分别将计算面积、周长、体积等函数声明为虚函数后,再在该类的派生类中定义与其基类虚函数原型相同的函数。当用基类指针指向这些派生类的对象时,系统会自动用派生类中的同名函数来代替基类中的虚函数,从而实现运行时的多态。学习使用虚函数实现动态多态性。而虚函数就是在基类中被关键字virtual说明,并在派生类中重新定义的函数,且在派生类中重工业新定义时,函数原型,包括返回类型、函数名、参数个数与参数类型的顺序,都必须与基类中的完全相同。此外,构造函数不能是虚函数,但析构函数可以是虚函数。2)题(2)用成员函数和友元函数两种方式实现。友元函数函数实现时,在类体内声明为该类的友元函数,然后再重载运算符
本文标题:C++上机实验
链接地址:https://www.777doc.com/doc-6055412 .html