C++初阶--类与对象--const成员和日期类的实现

简介: C++初阶--类与对象--const成员和日期类的实现


const 成员

将const修饰的成员函数称之为const成员函数。

在一个成员函数里面,对于this指针指向的对象,是隐藏式的,没有办法用常规的方法去修饰它,所以我们是这样进行修饰的:

注意事项:

对一个日期类的实现

源码

date.h

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
private:
  int _year;
  int _month;
  int _day;
public:
  Date(int year=1 , int month=1 , int day=1 );
  Date(const Date& d);
  Date& operator+=(int day);
  Date operator+(int day)const;
  //日期-天数
  Date& operator-=(int day);
  //日期-天数
  Date operator-(int day)const;
  Date& operator++();
  Date operator++(int);
  Date& operator--();
  Date operator--(int);
  //日期-日期
  int operator-(const Date& d)const;
  bool operator>(const Date& d)const;
  bool operator<(const Date& d)const;
  bool operator==(const Date& d)const;
  bool operator>=(const Date& d)const;
  bool operator<=(const Date& d)const;
  void Print()const;
  int GetMonthDay()const;
};

date.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
Date::Date(int year,int month,int day)
{
  _year = year;
  _month = month;
  _day = day;
  if (_year<1 || _month < 1 || _month>12 || _day>GetMonthDay())
  {
    Print();
    cout << "日期非法" << endl;
  }
}
Date::Date(const Date& d)
{
  _year = d._year;
  _month = d._month;
  _day = d._day;
}
void Date::Print()const
{
  cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay()const
{
  assert(_year >= 1 && _month >= 1 && _month<=12);
  int day=0;
  day+= _day;
  int Array[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  if (_month == 2 && (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0))
  {
    return 29;
  }
  return Array[_month];
}
//d1+=50;
Date& Date::operator+=(int day)
{
  if (day < 0)
  {
    return *this -= (-day);
  }
  _day += day;
  while (_day > GetMonthDay())
  {
    _day -= GetMonthDay();
    _month++;
    if (_month > 12)
    {
      _month = 1;
      _year++;
    }
  }
  return (*this);
}
//d2=d1+100
Date Date::operator+(int day)const
{
  Date tmp(*this);
  tmp += day;
  return tmp;
}
//d1-=100
Date& Date::operator-=(int day)
{
  if (day < 0)
  {
    return *this += (-day);
  }
  _day -= day;
  while (_day <= 0)
  {
    _month--;
    if (_month == 0)
    {
      _month = 12;
      _year--;
    }
    _day += GetMonthDay();
  }
  return (*this);
}
//d1=d2-100
//日期-天数
Date Date::operator-(int day)const
{
  Date tmp(*this);
  tmp -= day;
  return tmp;
}
//++d1
Date& Date::operator++()
{
  *this += 1;
  return *this;
}
//d1++
Date Date::operator++(int)
{
  Date tmp(*this);
  *this += 1;
  return tmp;
}
//--d1
Date& Date::operator--()
{
  *this -= 1;
  return *this;
}
//d1--
Date Date::operator--(int)
{
  Date tmp(*this);
  *this -= 1;
  return tmp;
}
bool Date::operator>(const Date& d)const
{
  if (_year < d._year)
  {
    return false;
  }
  if (_year == d._year && _month < d._month)
  {
    return false;
  }
  if (_year == d._year && _month == d._month && _day < d._day)
  {
    return false;
  }
  return true;
}
bool Date::operator==(const Date& d)const
{
  if (_year == d._year && _month == d._month && _day == d._day)
  {
    return true;
  }
  return false;
}
bool Date::operator>=(const Date& d)const
{
  return *this > d || *this == d;
}
bool Date::operator<(const Date& d)const
{
  return !(*this>d || *this==d);
}
bool Date::operator<=(const Date& d)const
{
  return *this < d || *this == d;
}
int Date::operator-(const Date& d)const
{
  int day = 0;
  Date min = *this;
  Date max = d;
  if (*this>d)
  {
    min = d;
    max = *this;
  }
  while (min < max)
  {
    min++;
    day++;
  }
  return day;
}

解析

这里采用多文件编程的方式,所以在date.cpp中,是在Date类外使用的,需要加上作用域限定符;

对于不改变类对象,也就是this指针指向的对象的,尽量都加上const进行修饰,有一定程度提高效率;

在date.h中,不管成员变量还是成员函数,在类中都只是起到声明的作用,类的成员变量的定义将会在对象中进行定义,类的成员函数都是在date.cpp定义的

构造函数的验证

在我们定义时,我们已经加上了缺省值了,

所以在定义时就不用加上缺省值了,加上反而非产生冲突,使编译器无法辨别要哪个缺省值,产生矛盾;

在定义时我们加上了对日期是否合法进行了判断,但没有强制性退出,只是打印警告;

我们将给出三组数据进行验证:

运算符的重复利用

对于重复的步骤,我们可以运用另一个函数来进行完成:

而这里也是有一定讲究的,对于参数和返回值来说,都会创建一个临时变量对象来拷贝传参的值,而这就表示着要调用拷贝构造函数,所以我们在写程序时,要尽量的少调用函数,提高一定的效率

上述程序中,调用函数次数:

如果我们反过来写的话:

注意:对于+=来说,会改变this指针指向对象的值,所以这里不能使用const来进行修饰;

而+ 到最后会返回一个值给对应的对象,这个对象不是this指针所指向的对象的,所以可以使用const进行修饰;

我们在date.cpp中没有写明=的重载,类会生成一个默认的运算符重载;

前置++与后置++

在C++中,前置++的重载形式与后置++的重载形式是相同的,为了辨别这两种重载形式,后置++重载时在参数中多加一个int类型的参数,但调用函数时该参数是不参与传递的。

前置++就是直接+=1;而后置++会等到语句结束后才加上1,所以先找一个中间值tmp来进行代替,表示还没有+1;

其他运算符的验证

相关文章
|
1天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
1天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
1天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
1天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现
|
8天前
|
存储 编译器 C++
c++的学习之路:6、类和对象(2)
c++的学习之路:6、类和对象(2)
22 0
|
8天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
23 0
|
8天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
21 0
|
1天前
|
编译器 C++
【C++类和对象】日期类的实现(上)
【C++类和对象】日期类的实现
|
1天前
|
编译器 C++ 索引
【C++类和对象】拷贝构造与赋值运算符重载(下)
【C++类和对象】拷贝构造与赋值运算符重载
|
1天前
|
存储 编译器 C++
【C++类和对象】拷贝构造与赋值运算符重载(上)
【C++类和对象】拷贝构造与赋值运算符重载