• 类与对象(中)



    1. 类的6个默认成员函数

    class ToTheMoon
    (
    
    }
    
    • 1
    • 2
    • 3
    • 4

    如果一个类里面什么都没有,我们称它为空类。
    但是真的什么都没有吗,其实不是的。
    类中有6个默认成员函数。
    在这里插入图片描述

    2. 构造函数

    构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但构造函数的作用是初始化对象。
    特征如下:
    1. 函数名与类名相同。
    2. 无返回值。
    3. 对象实例化时编译器自动调用对应的构造函数。
    4. 构造函数可以重载。

    class Date
    {
    public :
    // 1.无参构造函数
    Date ()
    {}
    
    // 2.带参构造函数
    Date (int year, int month , int day )
    {
    	_year = year ;
    	_month = month ;
    	_day = day ;
    }
    private :
    	int _year ;
    	int _month ;
    	int _day ;
    };
    void TestDate()
    {
    	Date d1; // 调用无参构造函数
    	Date d2 (2015, 1, 1); // 调用带参的构造函数
    // 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
    // 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
    	Date d3();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    3. 析构函数

    析构函数也是特殊的成员函数。
    特征如下:
    1. 析构函数名是在类名前加上字符 ~。
    2. 无参数无返回值。
    3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
    4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。

    typedef int DataType;
    class SeqList
    {
    public :
    SeqList (int capacity = 10)
    {
    	_pData = (DataType*)malloc(capacity * sizeof(DataType));
    	assert(_pData);
    	_size = 0;
    	_capacity = capacity;
    }
    ~SeqList()
    {
    	if (_pData)
    	{
    	free(_pData ); // 释放堆上的空间
    	_pData = NULL; // 将指针置为空
    	_capacity = 0;
    	_size = 0;
    	}
    }
    private :
    	int* _pData ;
    	size_t _size;
    	size_t _capacity;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    上面运用构造函数初始化了顺序表,并且写了一个析构函数用来释放申请的空间。

    4. 拷贝构造函数

    1.拷贝构造函数是构造函数的一个重载形式。
    2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用

    如果是值传递,我们在传的过程中首先要拷贝一份值,相当于调用了Date d(d1);此时又会调用拷贝构造…最后陷入无穷递归,因而需要使用引用传参。

    class Date
    {
    public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
    	_year = year;
    	_month = month;
    	_day = day;
    }
    Date(const Date& d)
    {
    	_year = d._year;
    	_month = d._month;
    	_day = d._day;
    }
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    int main()
    {
    	Date d1;
    	Date d2(d1);
    	//这里是拷贝构造
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    5. 赋值操作符重载

    C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
    函数名字为:关键字operator后面接需要重载的运算符符号。
    函数原型:返回值类型 operator操作符(参数列表)

    // 全局的operator==
    class Date
    {
    public:
    	Date(int year = 1900, int month = 1, int day = 1)
    {
    	_year = year;
    	_month = month;
    	_day = day;
    }
    //private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    bool operator==(const Date& d1, const Date& d2)
    {
    	return d1._year == d2._year;
    	&& d1._month == d2._month
    	&& d1._day == d2._day;
    }
    void Test ()
    {
    	Date d1(2018, 9, 26);
    	Date d2(2018, 9, 27);
    	cout<<(d1 == d2)<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    6. 日期类的实现

    class Date
    
    {
    
    public:
    
    // 获取某年某月的天数
    
    int GetMonthDay(int year, int month)
    
    {
    
    static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int day = days[month];
    
    if (month == 2 
    
    &&((year % 4 == 0 && year % 100 != 0) || (year%400 == 0)))
    
    {
    
    day += 1;
    
    }
    
    return day;
    
    }
    
    
    
      // 全缺省的构造函数
    
    Date(int year = 1900, int month = 1, int day = 1)
    
    {
    
    if(year < 1900
    
    || month < 1 || month > 12
    
    || day < 1 || day > GetMonthDay(year ,month) )
    
    {
    
    cout<<"非法日期"<<endl;
    
    }
    
    
    
    _year = year;
    
    _month = month;
    
    _day = day;
    
    }
    
    
    
      // 拷贝构造函数
    
    // d2(d1)
    
    Date(const Date& d)
    
    {
    
    this->_year = d._year;
    
    _month = d._month;
    
    _day = d._day;
    
    }
    
    
    
      // 赋值运算符重载
    
    // d2 = d3 -> d2.operator=(&d2, d3)
    
    Date& operator=(const Date& d)
    
    {
    
    if (this != &d)
    
    {
    
    this->_year = d._year;
    
    this->_month = d._month;
    
    this->_day = d._day;
    
    }
    
    
    
    return *this;
    
    }
    
       
    
      // 析构函数
    
    ~Date()
    
    {
    
    // 清理工作
    
    }
    
    
    
    void Print()
    
    {
    
    cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    
    }
    
    
    
      // 日期+=天数
    
    // d1 += 10
    
    // d1 += -10
    
    Date& operator+=(int day)
    
    {
    
    if (day < 0)
    
    {
    
    return *this -= -day;
    
    }
    
    
    
    _day += day;
    
    while (_day > GetMonthDay(_year, _month))
    
    {
    
    _day -= GetMonthDay(_year, _month);
    
    _month++;
    
    if (_month == 13)
    
    {
    
    _year++;
    
    _month = 1;
    
    }
    
    }
    
    
    
    return *this;
    
    }
    
    
    
      // 日期+天数
    
    // d + 10
    
    Date operator+(int day)
    
    {
    
    Date ret(*this);
    
    ret += day;
    
    
    
    return ret;
    
    }
    
    
    
      // 日期-天数
    
    Date operator-(int day)
    
    {
    
    Date ret(*this);
    
    ret -= day;
    
    return ret;
    
    }
    
    
    
       // 日期-=天数
    
    // d -= 100
    
    // d -= -100
    
    Date& operator-=(int day)
    
    {
    
    if (day < 0)
    
    {
    
    return *this += -day;
    
    }
    
    
    
    _day -= day;
    
    while (_day <= 0)
    
    {
    
    --_month;
    
    if (_month == 0)
    
    {
    
    --_year;
    
    _month = 12;
    
    }
    
    _day += GetMonthDay(_year, _month);
    
    }
    
    
    
    return *this;
    
    }
    
    
    
      // 前置++
    
    // ++d -> d.operator++(&d)
    
    Date& operator++() 
    
    {
    
    *this += 1;
    
    return *this;
    
    }
    
    
    
      // 后置++
    
    // d++ -> d.operator++(&d, 0)
    
    Date operator++(int) 
    
    {
    
    Date ret(*this);
    
    *this += 1;
    
    return ret;
    
    }
    
    
    
      // // 后置--
    
    Date operator--(int)
    
    {
    
    Date ret(*this);
    
    *this -= 1;
    
    return ret;
    
    }
    
    
    
      // 前置--
    
    Date& operator--()
    
    {
    
    *this -= 1;
    
    return *this;
    
    }
    
    
    
    // d1 > d2
    
      // >运算符重载
    
    bool operator>(const Date& d)
    
    {
    
    if (_year > d._year)
    
    {
    
    return true;
    
    }
    
    else if (_year == d._year)
    
    {
    
    if (_month > d._month)
    
    {
    
    return true;
    
    }
    
    else if (_month == d._month)
    
    {
    
    if (_day > d._day)
    
    {
    
    return true;
    
    }
    
    }
    
    }
    
    
    
    return false;
    
    }
    
    
    
      // ==运算符重载
    
    bool operator==(const Date& d)
    
    {
    
    return _year == d._year
    
    && _month == d._month
    
    && _day == d._day;
    
    }
    
    
    
      // 下面复用上面两个的实现
    
      // >=运算符重载
    
    bool operator >= (const Date& d)
    
    {
    
    return *this > d || *this == d;
    
    }
    
    
    
      // <运算符重载
    
    bool operator < (const Date& d)
    
    {
    
    return !(*this >= d);
    
    }
    
    
    
       // <=运算符重载
    
    bool operator <= (const Date& d)
    
    {
    
    return !(*this > d);
    
    }
    
    
    
      // !=运算符重载
    
    bool operator != (const Date& d)
    
    {
    
    return !(*this == d);
    
    }
    
    
    
    // d1 - d2
    
      // 日期-日期 返回天数
    
    int operator-(const Date& d)
    
    {
    
    int flag = 1;
    
    Date max = *this;
    
    Date min = d;
    
    if (*this < d)
    
    {
    
    max = d;
    
    min = *this;
    
    flag = -1;
    
    }
    
    
    
    int day = 0;
    
    while (min < max)
    
    {
    
    ++(min);
    
    ++day;
    
    }
    
    
    
    return day*flag;
    
    }
    
    
    
    private:
    
    int _year;
    
    int _month;
    
    int _day;
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505

    7. const成员函数

    将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

    class Date
    {
    public :
    void Display ()
    {
    	cout<<"Display ()" <<endl;
    	cout<<"year:" <<_year<< endl;
    	cout<<"month:" <<_month<< endl;
    	cout<<"day:" <<_day<< endl<<endl ;
    }
    void Display () const
    {
    	cout<<"Display () const" <<endl;
    	cout<<"year:" <<_year<< endl;
    	cout<<"month:" <<_month<< endl;
    	cout<<"day:" <<_day<< endl<<endl;
    }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    上面的const修饰Display的this指针,也就是说该成员函数不能对类的成员进行修改。

    总结

    以上就是今天要讲的内容。
    在这里插入图片描述

  • 相关阅读:
    js判断对象是否为空
    AOP是什么? AOP底层原理、AOP的术语
    【数据结构练习】二叉树相关oj题集锦二
    你真的知道什么是 JDK吗?
    springboot单体项目部署
    Maven
    【Linux】权限管理
    IDEA抢救10年前WEB项目
    防火墙 FireWall
    Go Web——Beego之view设计
  • 原文地址:https://blog.csdn.net/m0_63742310/article/details/124233228