您的位置:首页 > 编程语言

编程小练习

2016-04-11 12:36 218 查看


/*Copyright (c)2016,烟台大学计算机与控制工程学院 
 *All rights reserved. 
 *文件名称:main.cpp 
 *作    者:李落才
 *完成日期:2016年4月11日 
 * 
 *问题描述:两点间距离之成员函数 
 */  
#include<iostream>  
#include<cmath>  
using namespace std;  
class CPoint  
{  
public:  
   CPoint(double xx=0,double yy=0):x(xx),y(yy){}  
   double getX(){return x;}  
   double getY(){return y;}  
private:  
    double x;//横坐标  
    double y;//纵坐标  
};  
class Line  
{  
public:  
    Line(CPoint p1,CPoint p2);  
    void len1();  
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=(double)sqrt(x*x+y*y);  
}  
void Line::len1()//成员函数len1()的实现,len1前加 Line  
{  
    cout<<"The distance is: "<<len<<endl;  
}  
int main()  
{  
    CPoint myp1(1.0,1.0),myp2(4.0,5.0);  
    Line line(myp1,myp2);  
    line.len1();  
    return 0;  
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: