CPP类的组合及拷贝构造函数

类的组合及拷贝构造函数
#include <iostream>
#include<cmath>
using namespace std;

class Point
{
private:
   float x,y;
public:
   Point(float xx,float yy)
   {
       cout<<"point构造函数"<<endl;
    this->x=xx;
    this->y=yy;
   }
   Point(Point &p)
   {
       x=p.x;y=p.y;
       cout<<"pont 拷贝构造函数"<<endl;
   }
   float GetX(void){return x;}
   float GetY(void){return y;}
};

class Distance
{
private:
   Point p1,p2;
   double dist;
public:
   Distance(Point a,Point b);//构造函数
   double GetDis(void){return dist;}
};

Distance::Distance(Point a,Point b):p1(a),p2(b)
{ //有了对象成员Point a传给p1(a), Point b传给p2(b),
  //double p传给p,price=p;
   double x=double(p1.GetX()-p2.GetX());
   double y=double(p1.GetY()-p2.GetY());
   dist=sqrt(x*x+y*y);
   cout<<"Distance构造函数:"<<endl;
}

int main()
{
    Point myp1(1,1),myp2(4,5);
    Distance myd(myp1,myp2);
    cout<<"the distance is: ";
    cout<<myd.GetDis()<<endl;
    return 1;
}

输出结果:
point构造函数
point构造函数
pont 拷贝构造函数
pont 拷贝构造函数
pont 拷贝构造函数
pont 拷贝构造函数
Distance构造函数:
the distance is: 5


转载于:https://my.oschina.net/crooner/blog/147392

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值