第七周上机实践项目-项目一-线段类-成员函数

来源:互联网 发布:淘宝卖家怎么查访客 编辑:程序博客网 时间:2024/05/19 02:01
<pre class="html" name="code">/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作    者:刘涛 *完成日期:2016年4月9日 *版 本 号:v1.0 * *问题描述:线段类的成员函数,友元函数和一般函数的区别  */#include <iostream>#include<cmath>using namespace std;class CPoint//点类{private:    int  x;//横坐标    int  y;//纵坐标public:    CPoint(int  xx=0,int yy=0):x(xx),y(yy){}    int getX(){return x;}    int getY(){return y;}};class CLine//直线类{public:    CLine(CPoint xp1,CPoint xp2);    double getLen(){return len;}private:    CPoint p1,p2;    double len;};CLine::CLine(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2){double x=static_cast<double>(p2.getX()-p1.getY());double y=static_cast<double>(p2.getY()-p1.getY());len=sqrt(x*x+y*y);}int main(){    CPoint coordinatesOne(1,1),coordinatesTwo(4,5);    CLine line(coordinatesOne,coordinatesTwo);    cout<<"两点之间的距离为:";    cout<<line.getLen()<<endl;    return 0;}

运行及结果:
<img src="http://img.blog.csdn.net/20160409145843056?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
知识点总结:本题主要运用了类的组合和成员函数的知识点,还有就是static_cast<类型名>的应用,在c++里这种做法比<类型名>要好用。
学习心得:明白了static_cast的使用,进一步强化了自己的弱项,类的组合的练习,今后要跟深入的学习类的组合。
</pre><pre class="cpp" name="code">

0 0
原创粉丝点击