/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 体会成员函数、有元函数、一般函数的区别
* 作 者: 王智凯
* 完成日期: 2012 年 04 月 02 日
* 版 本 号: 凯子
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{private:
double x; // 横坐标
double y; // 纵坐标
double d;
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy){}
double Distance(CPoint p) ; // 两点之间的距离(一点是当前点,另一点为参数p)
void display1();
friend void display2(CPoint &);
void input(); //以x,y 形式输入坐标点
void output(); //以(x,y) 形式输出坐标点
//……//请继续写需要的代码
double get_d(){return d;}
};
void display3(CPoint &);
double CPoint::Distance(CPoint p)
{
d = sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
return d;
}
void CPoint::input()
{
char c;
cout << "请输入点坐标:(格式:x,y)" << endl;
do
{
cin >> x >> c >> y;
if(c == ',')
{
break;
}
cout << "格式不正确,请重新输入:" << endl;
}while(1);
}
void CPoint::output()
{
cout << "(" << x << "," << y << ")" << endl;
}
void CPoint::display1()
{
cout<<"两点间间的距离为"<<d<<endl;
}
void display2(CPoint &c)
{
cout<<"两点间的距离为"<<c.d<<endl;
}
void display3(CPoint &c)
{
cout<<"两点间的距离为"<<c.get_d()<<endl;
}
void main()
{
CPoint c1, c2;
c1.input();
c2.input();
c1.output();
c2.output();
c1.Distance(c2);
c1.display1();
display2(c1) ;
display3(c1) ;
system("pause");
}
上机感言:终于明白了3个函数之间的区别了,成员函数在类中可直接调用;有元函数可以调用私有成员对象;一般函数就是最普通的函数了。