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

编程小练习

2016-04-11 12:41 435 查看

/*Copyright (c)2016,烟台大学计算机与控制工程学院 
 *All rights reserved. 
 *文件名称:main.cpp 
 *作    者:李落才
 *完成日期:2016年4月11日 
 * 
 *问题描述:两点间距离之综合类 
 */  
#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;  
    }  
    friend void len3(CPoint &p1,CPoint &p2);//友元函数声明  
    double getX()  
    {  
        return x;  
    }  
    double getY()  
    {  
        return y;  
    }  
};  
class Line  
{  
public:  
    Line(CPoint xp1,CPoint xp2);  
    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()   //调用成员函数  
{  
    cout<<"The distance is:"<<len<<endl;  
}  
void len2(CPoint &p1,CPoint &p2) //调用一般函数  
{  
    double len;  
    double x=p1.getX()-p2.getX();  
    double y=p1.getY()-p2.getY();  
    len=(double)sqrt(x*x+y*y);  
    cout<<"The distance is:"<<len<<endl;  
}  
void len3(CPoint &p1,CPoint &p2)  //调用友元函数  
{  
    double len;  
    double x=p1.x-p2.x;  
    double y=p1.y-p2.y;  
    len=static_cast<float>(sqrt(x*x+y*y));  
    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();       //调用成员函数  
    len2(myp1,myp2);   //调用一般函数  
    len3(myp1,myp2);   //调用友元函数  
    return 0;  
}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: