C++第3次上机实验报告(友元函数二题)

/* 
* 文件名称: bulabula
* 作    者:  郝荣雅
* 完成日期: 2016  年 4    月 10   日 
* 版 本 号:v1.0 
* 对任务及求解方法的描述部分:利用三种方式求两点距离
* 输入描述:  
* 问题描述: 
* 程序输出:  
* 问题分析: 
* 算法设计:  
*/  

1.(友元类)

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
Point(double xx,double yy)
{   
x1=xx;
y1=yy;
cout<<x1<<","<<y1<<endl;
}
friend void Distance(Point&Pointa,Point&Pointb);
private:
double x1;
double y1;
double dd;
};
void Distance(Point&Pointa,Point&Pointb)
{
double dx=Pointa.x1 -Pointb.x1 ;
double dy=Pointa.y1 -Pointb.y1 ;
double dd=sqrt(dx*dx+dy*dy);
cout<<"两点距离"<<dd<<endl;
}
void main()
{
Point a(5,7),b(1,4);
Distance(a,b);


}

(一般函数)

#include<iostream>
#include<cmath>
using namespace std;
void main()
{
double x1,x2;
double y1,y2;
    double dx,dy,dd;
cin>>x1>>y1>>x2>>y2;
dx=(x1-x2)*(x1-x2);
dy=(y1-y2)*(y1-y2);
dd=sqrt(dx+dy);
cout<<"两点坐标距离"<<dd;
}

(成员函数)

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
Point(double xx1,double yy1,double xx2,double yy2);
void distance();
void show();
private:
double x1,x2;
double y1,y2;
    double dd;
};
Point::Point(double xx1,double yy1,double xx2,double yy2)
{
x1=xx1,x2=xx2,y1=yy1,y2=yy2;
    cout<<x1<<","<<y1<<"   "<<x2<<","<<y2<<endl;
}
void Point::distance ()
{
    double dx,double dy;
dx=(x1-x2)*(x1-x2);
dy=(y1-y2)*(y1-y2);
dd=sqrt(dx+dy);
}
void Point::show ()
{
cout<<"diatance:"<<dd<<endl;
}
void main()
{
Point Point(5,7,1,4);
Point.distance ();
Point.show ();
}

2.运行结果



3.心得体会

其实最大的心得体会是友元函数那个,感觉是新的,对象的建立,以及对私有成员的访问;

4.知识点总结

友元函数可以访问私有成员和其他成员,但是访问时不能直接默认引用数据成员,必须使用对象进行引用

在main函数中对于友元函数的访问是不用加friend的,而且不用通过对象来引用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值