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

C++语言基础 例程 继承与组合

2015-05-09 22:47 453 查看
贺老师的教学链接 本课讲解

点与直线

#include<iostream>
#include<Cmath>
using namespace std;
class Dot
{
public:
    float x,y;
    Dot(float a=0,float b=0)
    {
        x=a;
        y=b;
    }
    void Show(void)
    {
        cout<<"x="<<x<<'\t'<<"y="<<y<<endl;
    }
};
class Line:public Dot
{
    Dot d1,d2;
public:
    Line(Dot dot1,Dot dot2):d1(dot1),d2(dot2)
    {
        x=(d1.x+d2.x)/2;
        y=(d1.x+d2.y)/2;
    }
    void Showl(void)
    {
        cout<<"Dot1:  ";
        d1.Show();
        cout<<"Dot2:  ";
        d2.Show();
        cout<<"Length="<<sqrt((d1.x-d2.x)*(d1.x-d2.x)+(d1.y-d2.y)*(d1.y-d2.y))<<endl;
        cout<<"Center:  "<<"x="<<x<<'\t'<<"y="<<y<<endl;
    }
};
int main()
{
    float a,b;
    cout<<"Input Dot1: \n";
    cin>>a>>b;
    Dot dot1(a,b);//调用Dot的构造函数
    cout<<"Input Dot2: \n";
    cin>>a>>b;
    Dot dot2(a,b);
    Line line(dot1,dot2);
    line.Showl();
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: