c++实验3-2
2016-04-12 16:16
232 查看
一、问题及代码
二、运行结果:
三、心得体会:
第二个C++程序,有很多错误,这些都只能通过实践来慢慢改正,所以应该多多练习。
C++,我会好好学的!
四、知识点总结:
成员函数,友元函数,一般函数,调用要分清。各有各的优点,应熟练掌握。
对博文提交的程序书写规范很重要,最起码的要求:(1)缩格排版;(2)一句一行;(3)花括号位置;(4)适度的注释。
#include<iostream.h> #include<cmath> class Long { private: double x; double y; public: Long(double xx=0,double yy=0):x(xx),y(yy){} void distance1(Long &); friend void distance2 (Long &,Long &); int getx() { return x; } int gety() { return y; } }; void Long::distance1(Long &t) { cout<<sqrt((this->x-t.x)*(this->x-t.x)+(this->y-t.y)*(this->y-t.y))<<endl; } void distance2 (Long &t1,Long &t2) { cout<<sqrt((t1.x-t2.x)*(t1.x-t2.x)+(t1.y-t2.y)*(t1.y-t2.y))<<endl; } void distance3 (Long &t1,Long &t2) { cout<<sqrt((t1.getx()-t2.getx())*(t1.getx()-t2.getx())+(t1.gety()-t2.gety())*(t1.gety()-t2.gety()))<<endl; } int main() { Long L1(4,6),L2(2,3); L1.distance1(L2); distance2(L1,L2); distance3(L1,L2); return 0; }
二、运行结果:
三、心得体会:
第二个C++程序,有很多错误,这些都只能通过实践来慢慢改正,所以应该多多练习。
C++,我会好好学的!
四、知识点总结:
成员函数,友元函数,一般函数,调用要分清。各有各的优点,应熟练掌握。
对博文提交的程序书写规范很重要,最起码的要求:(1)缩格排版;(2)一句一行;(3)花括号位置;(4)适度的注释。
相关文章推荐