C++实验二
来源:互联网 发布:淘宝商家怎么进天猫 编辑:程序博客网 时间:2024/06/16 01:21
在实验一的基础上:
加一个常成员函数;
用对象数组初始化两个人的坐标,然后用友元函数求出距离并输出;
#include <iostream>
#include <cmath>
using namespace std;
class point
{
public:
point(int my_x, int my_y, int my_z);
point()
{
x = 0;
y = 0;
z = 0;
}
void tellmeyourpoint(int your_x = 1, int your_y = 1, int your_z = 1);
friend double ourdistance(point &p1, point &p2);//定义友元函数
void showpoint()
{
cout<<x<<","<<y<<","<<z<<endl;
}
void cshowpoint() const;//定义常成员函数
~point()
{
cout<<"OK"<<endl;
}
private:
int x;
int y;
int z;
};
void point::cshowpoint() const
{
cout<<x<<"! "<<y<<"! "<<z<<endl;
}
point::point(int my_x, int my_y, int my_z)
{
x = my_x;
y = my_y;
z = my_z;
}
void point::tellmeyourpoint(int your_x, int your_y, int your_z)
{
x = your_x;
y = your_y;
z = your_z;
}
double ourdistance(point &p1, point &p2)//友元函数计算距离
{
double x = p1.x - p2.x;
double y = p1.y - p2.y;
double z = p1.z - p2.z;
return static_cast<double>(sqrt(x * x + y * y + z * z));
}
int main()
{
point P(6, 6, 6);
P.showpoint();//P调用有参构造函数
point PP;
PP.showpoint();//PP调用无参构造函数
int yx, yy, yz;
cin>>yx>>yy>>yz;
P.tellmeyourpoint(yx, yy, yz);
P.showpoint();
const point cp(1, 2, 3);//常成员函数
cp.cshowpoint();//调用常成员函数
point dis[2] = {
point(1, 1, 1),
point(2, 2, 2)
};//对象数组初始化
cout<<"our distance :"<<endl;
cout<<ourdistance(dis[0], dis[1])<<endl;
return 0;
}
- C语言程序设计实验手册--实验二
- C#.net实验二-计算器实验
- 实验二 线性表的综合实验(c++)
- 实验二 线性表的综合实验(c++)
- 实验二 线性表的综合实验(c++)
- 实验二 线性表 综合实验(c++)
- [C++]实验二:Pointer and Reference
- C 程序设计实验报告(二)
- C语言函数连接实验二
- C 四舍五入取整(实验二)
- 实验二 单链表实验
- 【实验二】串口实验
- 实验一&实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- (一)java基础篇笔记库(14)
- 一个组件的识别和开发过程(持续更新)
- 评价成绩
- 5-1 继承与派生
- 学习资源地址汇总
- C++实验二
- JVM--垃圾收集算法
- Marriage Match IV HDU
- 鲍捷 | 知识图谱从 0 级到 10 级简化版
- 关于 点亮8*8LED点阵(24脚)的爱心教程
- CstringArray error C2248: 'CObject::CObject' : cannot access private member declared in class
- Xcode9学习笔记81
- Python 爬虫的工具列表
- 3-5 学生成绩统计