风之客-例题2

上传人:wm****3 文档编号:51904064 上传时间:2018-08-17 格式:PPT 页数:33 大小:102KB
返回 下载 相关 举报
风之客-例题2_第1页
第1页 / 共33页
风之客-例题2_第2页
第2页 / 共33页
风之客-例题2_第3页
第3页 / 共33页
风之客-例题2_第4页
第4页 / 共33页
风之客-例题2_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《风之客-例题2》由会员分享,可在线阅读,更多相关《风之客-例题2(33页珍藏版)》请在金锄头文库上搜索。

1、例题2定义类Point,有坐标数据成员x和y,取X 坐标函数GetX(void);取Y坐标函数 GetY(void); 构造函数Point(float xx, float yy); 和析构函数Point(); 定义类Distance, 有对象数据成员p1, p2和距 离数据dist;构造函数Distance (Point a,Point b); 计算两点距离函数GetDis(void) 定义两个Point类对象myp1(1,1), myp2(4,5); 定义一个Distance类对象myd(p1, p2); 主程序实现求两点间的距离例题2-1class Point private:float

2、x,y; /点的坐标public:Point(float xx, float yy); /构造函数Point();float GetX(void); /取X坐标float GetY(void); /取Y坐标 Point:Point(float xx,float yy) X=xx; Y=yy; Point:Point( ) float Point:GetX(void) return x; folat Point:GetY(void) return y;class Distanceprivate:Point p1, p2; /线段的两个端点double dist;public:Distance (

3、Point a,Point b); /构造函数double GetDis(void) return dist; /计算距离;Distance: Distance (Point a, Point b): p1(a), p2(b)double x=double(p1.GetX()-p2.GetX();double y=double(p1.GetY()-p2.GetY();dist=sqrt (x*x-y*y);void main()Point myp1(1,1), myp2(4,5);Distance myd(p1, p2);cout # include class Point public:Po

4、int (int xx=0,yy=0) x=xx,y=yy;int GetX();int GetY();friend float fdist(Point Double fdist(Point /?double dy=a.y-b.y;return sqrt (dx*dx+dy*dy); void main() Point P1(3.0,5.0), P2(4.0,6.0);double d=fdist(P1, P2);cout“the distance is”dendl;例题3-4 一个类的成员函数可以成为另外一个类的友 元函数class Student; class teacher public:void assigGrade (Student private:int noofStudent; class Student public:friend void Teacher:assigGrade (Student float score;void Teacher:assigGrade (Student 注意:第一:友元关系是不能传递的B类是A类的友元,C类是B类的友元,但C类 与A类无关。第二:友元关系是单向的B类是A类的友元,B类的成员可以访问A类的 所有成员,但A类不是B类的友元

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号