C++语言基础 例程 this指针

贺老师的教学链接  本课讲解


内幕: 每个对象有个自己的this指针

#include <iostream>
using namespace std;
class Time
{
public:
    void set_time(int,int,int);
    void show_time();
private:
    int hour;
    int minute;
    int sec;
};
int main( )
{
    Time t1,t2;
    t1.set_time(12,45,32);
    t2.set_time(21,32,15);
    t1.show_time();
    t2.show_time( );
    return 0;
}


void Time::set_time(int h,int m,int s)
{
    hour=h;
    minute=m;
    sec=s;
}
void Time::show_time()
{
    cout<<hour<<":";
    cout<<minute<<":";
    cout<<sec<<endl;
}


分清你我
class CPoint
{
private:
     double x,y; 
public:
     double Distance(CPoint p);
};
// 求两点之间的距离
double CPoint::Distance(CPoint p)
{
     double dx, dy;
     dx=p.x-x;
     dy=p.y-y;
    d=sqrt(dx*dx+dy*dy);
    return d;
}

class CPoint
{
private:
    double x,y; 
public:
    double Distance(CPoint p);
    void setxy(double x, double y);  
};
// 求两点之间的距离
void CPoint::setxy(double x, double y)
{
     x = x; 
     y =y;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值