第七周项目1-点类-成员函数
2016-04-09 19:45
357 查看
问题及代码:
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:zwj.cpp
*作 者:张伟晶
*完成日期:2016年4月9日
*版 本 号: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 getx(){return x;}
double gety(){return y;}
};
class Line
{
public:
Line(CPoint xp1,CPoint xp2);
Line (Line &l);
double getlen(){return len;}
private:
CPoint p1,p2;
double len;
};
Line::Line(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2)
{
double x=p1.getx()-p2.getx();
double y=p1.gety()-p2.gety();
len=sqrt(x*x+y*y);
}
int main()
{
CPoint p1(1,1),p2(4,6.2);
Line line(p1,p2);
cout<<"p1为:("<<p1.getx()<<","<<p1.gety()<<")"<<endl;
cout<<"p2为:("<<p2.getx()<<","<<p2.gety()<<")"<<endl;
cout<<"两点间距离为:"<<line.getlen()<<endl;
return 0;
}
运行结果:
知识点总结:
构造函数的使用,创建对象,初始化对象。
学习心得:
在实践过程中练习了构造函数的使用,两个类之间的调用。
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:zwj.cpp
*作 者:张伟晶
*完成日期:2016年4月9日
*版 本 号: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 getx(){return x;}
double gety(){return y;}
};
class Line
{
public:
Line(CPoint xp1,CPoint xp2);
Line (Line &l);
double getlen(){return len;}
private:
CPoint p1,p2;
double len;
};
Line::Line(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2)
{
double x=p1.getx()-p2.getx();
double y=p1.gety()-p2.gety();
len=sqrt(x*x+y*y);
}
int main()
{
CPoint p1(1,1),p2(4,6.2);
Line line(p1,p2);
cout<<"p1为:("<<p1.getx()<<","<<p1.gety()<<")"<<endl;
cout<<"p2为:("<<p2.getx()<<","<<p2.gety()<<")"<<endl;
cout<<"两点间距离为:"<<line.getlen()<<endl;
return 0;
}
运行结果:
知识点总结:
构造函数的使用,创建对象,初始化对象。
学习心得:
在实践过程中练习了构造函数的使用,两个类之间的调用。
相关文章推荐
- Linux下硬链接和软链接
- 如何在博客园发博客时插入优酷视频
- Java-Eclipse编辑窗口不见了
- 嵌入式linux常用shell指令
- Androdi AsyncTask
- 软件工程结对作业02(原创版)
- iOS开发学习之 - 深入学习UIButton(2)
- 《leetCode》:Kth Largest Element in an Array
- c++模板实现队列
- Collection集合层次结构图
- Whu oj 1606 - Funny Sheep
- leetcode 58. Length of Last Word
- 更新到Xcode 7.3 #import 不提示的问题
- 结对编程——电梯调度系统 的 感想与总结
- 软件工程_东师站_课堂笔记
- find之depth参数
- 认识 EXT2 文件系统、磁盘分区 ( Partition )、文件系统:
- LeetCode之13---Roman to Integer
- POJ 2987 Firing 最小割(最大权闭合子图)
- 分享最新版 nginx内置变量 大全