《【2017年整理】实验四_成员函数的重载》由会员分享,可在线阅读,更多相关《【2017年整理】实验四_成员函数的重载(8页珍藏版)》请在金锄头文库上搜索。
1、实验四 成员函数的重载1、实验目的及要求:1)掌握普通函数重载的方法2)掌握成员函数重载的方法2、实验内容:2.1 分析下面程序,指出程序运行的结果#includeusing namespace std;class ListClassint *List;unsigned nMax;unsigned nElem;public:void Init(int n=10) List=new int n;nMax=n;nElem=0;int Elem(int);int &Elem(unsigned n)return Listn;unsigned Elem(void)return nElem;unsigne
2、d Max(void)return nMax;void Print(void);int GetElem(int i)if (i=0)&(i#includeusing namespace std;int distence(int x0,int y0,int x1,int y1)int d;d=sqrt(x0-x1)*(x0-x1)+(y0-y1)*(y0-y1);return d;double distence(double x2,double y2,double x3,double y3)double d;d=sqrt(x2-x3)*(x2-x3)+(y2-y3)*(y2-y3);return
3、 d;int main()int x0,y0,x1,y1;double x2,y2,x3,y3;cinx0y0x1y1;cinx2y2x3y3;cout#includeusing namespace std;class Cpointpublic:Cpoint(int a0,int b0,int c0,int d0)x0=a0;y0=b0;x1=c0;y1=d0;Cpoint(double a1,double b1,double c1,double d1)x2=a1;y2=b1;x3=c1;y3=d1;distence(int x0,int y0,int x1,int y1);distence(
4、double x2,double y2,double x3,double y3);output0();output1();private:int x0,y0,x1,y1;double x2,y2,x3,y3;Cpoint:distence(int x0,int y0,int x1,int y1)coutusing namespace std;class Squarpublic:int squ(int);double squ(double);long squ(long);private:int x;double y;long z;int Squar:squ(int a)x=a;return x*x;double Squar:squ(double b)y=b;return y*y;long Squar:squ(long c)z=c;return z*z;int main()Squar c;cout2 的平方是: c.squ(2)endl;cout1.1 的平方是: c.squ(1.1)endl;cout1000 的平方是: c.squ(1000)endl;return 0;运行结果:2 的平方是: 41.1 的平方是: 1.211000 的平方是: 1000000Press any key to continue3实验总结: