第5周-项目4-成员函数-求点类中距离-区别成员函数、友元函数和一般函数
2015-04-11 13:44
295 查看
问题及代码:
运行结果:
知识点总结:
用成员函数求点类中距离。
学习心得:
复习了一下之前学习的成员函数~~
问题及代码:
/* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:point.cpp *作 者:单昕昕 *完成日期:2015年4月11日 *版 本 号:v1.0 *问题描述:求点类中距离。 *程序输入:两个点。 *程序输出:两个点的距离。 */ #include <iostream> #include <cmath> using namespace std; class CPoint { private: double x; // 横坐标 double y; // 纵坐标 public: CPoint(double xx=0,double yy=0):x(xx),y(yy) {} double Distance1(CPoint p) const; //两点之间的距离 void input(); //以x,y 形式输入坐标点 void output(); //以(x,y) 形式输出坐标点 }; double CPoint::Distance1(CPoint p) const//两点之间的距离 { return sqrt((p.x-x)*(p.x-x)+((p.y-y)*(p.y-y))); } void CPoint::input()//以x,y 形式输入坐标点 { char c; cin>>x>>c>>y; } void CPoint::output()//以(x,y) 形式输出坐标点 { cout<<"("<<x<<","<<y<<")"<<endl; } int main() { CPoint p1,p2,p; cout<<"请以“x,y”形式输入点p1的坐标:"; p1.input(); cout<<"请以“x,y”形式输入点p2的坐标:"; p2.input(); cout<<"p1到p2的距离="<<p1.Distance1(p2)<<endl; return 0; }
运行结果:
知识点总结:
用成员函数求点类中距离。
学习心得:
复习了一下之前学习的成员函数~~
相关文章推荐
- 第5周-项目4-友元函数-求点类中距离-区别成员函数、友元函数和一般函数
- 2015年大一下第5周项目4-成员函数、友元函数和一般函数有区别
- 第六周项目四--成员函数、友元函数和一般函数的区别(求两点的距离)
- 第5周项目4-成员函数、友元函数和一般函数的区别
- 第七周 项目二:成员函数、友元函数、一般函数之区别(实现三个版本的求两点间距离)
- 第5周-项目4-一般函数-求点类中距离-区别成员函数、友元函数和一般函数
- 第六周项目四:成员函数、友元函数和一般函数有区别(实践项目)
- 项目二(成员函数,友元函数和一般函数之区别)
- 第六周项目四 成员函数、友元函数和一般函数区别
- 第六周项目4.2 成员函数 友元函数和一般函数有区别——点类
- 第六周项目4:成员函数、友元函数和一般函数有区别
- 项目二-成员函数、友元函数和一般函数之区别
- 第七周项目2:成员函数、友元函数和一般函数求两点间的距离
- 第六周 项目四:成员函数、友元函数和一般函数有区别
- 2013级C++第7周(春)项目——深复制、友元、模板 【项目3-成员函数、友元函数和一般函数有区别】
- 【项目2-成员函数、友元函数和一般函数之区别 点类】
- 第七周项目三成员函数,友元函数以及一般函数的区别
- 项目3-成员函数、友元函数和一般函数有区别
- 第七周项目3-成员函数、友元函数和一般函数区别
- 第七周项目三-----成员函数,友元函数和一般函数的区别