类和对象(中)

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

上篇我们学习了类,说到类我们就不得不说到类的6个默认成员函数了。

如果一个类中什么都没有,简称空类:

class A {};//这样就是一个空类

但是空类真的就是什么都没有吗?答案是否定的,任何类在我们什么都不写的情况下,编译器会为该类生成6个默认成员函数。

默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

image-20240401114138403

二.构造函数

① 构造函数的概念

构造函数是个什么东西呢?在之前我们学习的栈中,我们需要使用InitStack函数来初始化栈,故而构造函数就来了,它可以实现初始化的工作,非常之爽!

使用一个简单的Date类来举个例子:

class Date
{
public:
 void Init(int year, int month, int day)
 {
     _year = year;
     _month = month;
     _day = day;
 }
 void Print()
 {
     cout << _year << "-" << _month << "-" << _day << endl;
 }
private:
     int _year;
     int _month;
     int _day;
};
int main()
{
     Date d1;
     d1.Init(2022, 7, 5);
     d1.Print();
     Date d2;
     d2.Init(2022, 7, 6);
     d2.Print();
 return 0;
}

这里我们使用公有函数Init来实现类对象日期的设置,每次创建一个新的对象,我们都需要使用Init函数来设置该对象的日期,是不是未免有些麻烦,反正C语言的时候我们就是这么玩的。

我们就可以思考,是否在创建对象的时候,就可以把信息设置进去呢?答案是肯定的,这就是C++中的构造函数。

构造函数:构造函数是一个特殊的成员函数,它的名字和类名相同,创建类对象时由编译器自动调用,以保证每个数据成员都有一个合适的初始值,并且在对象整个生命周期内只调用一次。

② 构造函数的特性

构造函数只是特殊的成员函数,它的名字虽然叫构造函数,但是它不是开辟空间创建对象,而是初始化对象。

其特征如下:

  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();//这里是错误的
 }

这里d1和d2分别调用两个构造函数,两个构造函数构成了函数重载。

这里我们是显示的定义了构造函数,编译器就不会生成默认的构造函数,如果我们不写构造函数,那么编译器会自动生成一个无参的默认构造函数。

class Date
 {
  public:
 /*
 // 如果用户显式定义了构造函数,编译器将不再生成
 Date(int year, int month, int day)
 {
     _year = year;
     _month = month;
     _day = day;
 }
 */
 
 void Print()
 {
      cout << _year << "/" << _month << "/" << _day << endl;
 }
  private:
     int _year;
     int _month;
     int _day;
 };
  
  int main()
 {
 // 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数
 // 将Date类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成

 // 无参构造函数,放开后报错:error C2512: “Date”: 没有合适的默认构造函数可用
 //因为Date d1需要的是无参的构造函数,如果你把带参的构造函数写上,编译器就不会生成默认的无参构造函数,
 //即d1无没有合适的默认构造函数可用,就会报错
    Date d1;
    d1.Print();
 return 0;
 }

这里没写构造函数,编译器生成了一个无参的默认构造函数。我们可以调用Print函数来看看值。

image-20240401150423427

这里就很疑惑了,在我们自己不实现默认构造函数时,编译器生成的默认构造函数,为什么对成员变量没有初始化,为什么_year/_month/_day,依旧是随机值。也就说在这里编译器生成的默认构造函数并没有什么用?

这里就要说到C++对于类型的分类了,C++把类型分为内置类型和自定义类型。

内置类型就是语言提供的数据类型,如:int/char/double…。
自定义类型就是我们使用class/struct/union等自己定义的类型。
看看下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员函数。

class Time
{
public:
 Time()
 {
     cout << "Time()" << endl;
     _hour = 0;
     _minute = 0;
     _second = 0;
}
private:
     int _hour;
     int _minute;
     int _second;
};
class Date
{
private:
     // 基本类型(内置类型)
     int _year;
     int _month;
     int _day;
     // 自定义类型
     Time _t;
};
int main()
{
   Date d;
 return 0;
}

image-20240401154242203

这里创建的Date类的对象,但是调用了time类的构造函数,这里就可以看出,对于一个类来说,生成的默认构造函数,对内置类型不做处理,但是对于自定义类型会调用它的默认构造函数。

但是为了弥补内置类型不做处理,C++11做了补丁:内置类型成员变量在类中声明时可以给默认值。

class Date
 {
  public:
 void Print()
 {
      cout << _year << "/" << _month << "/" << _day << endl;
 }
  private:
     int _year=2024;
     int _month=1;
     int _day=1;
     //但是注意,这里只是声明,而不是初始化,只有当对象被创建出来了,才是初始化
 };
  
  int main()
  {
      Date d;
      d.Print();
      return 0;
  }

image-20240401155515138

下面我们写一个无参的构造函数和全缺省的构造函数,看看会怎么样?

class Date
{
public:
 Date()
 {
     _year = 1900;
     _month = 1;
     _day = 1;
 }
 Date(int year = 1900, int month = 1, int day = 1)
 {
     _year = year;
     _month = month;
     _day = day;
 }
private:
     int _year;
     int _month;
     int _day;
};
// 以下测试函数能通过编译吗?
int main()
{
   Date d1;
   return 0;
}

image-20240402083119503

这里直接给你一个大大的错误,对重载函数的调用不明确,这里就有一个点说明一下。

无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。
注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。

这里创建的对象,会调用无参的构造函数和全缺省的构造函数,而默认构造函数只能有一个,所以就会报错,虽然无参的构造函数和全缺省的构造函数在语法上是可以存在的,它们两个构成函数重载,但是会引发二义性。

三.析构函数

① 析构函数的概念

前面的构造函数就像是栈里面的Init一样,完成初始化工作,知道一个对象是如何来的,那一个对象是如何消失的呢?

这里就要说到析构函数了,它就像栈里面的Destroy函数一样,完成清理工作。

析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。

说白了,析构函数就是给对象擦屁股的。

② 析构函数的特性

析构函数一样也是特殊的成员函数。其特征如下:

  1. 析构函数名是在类名前加上字符 ~
  2. 无参数无返回值类型。
  3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构函数不能重载
  4. 对象生命周期结束时,C++编译系统自动调用析构函数。
typedef int DataType;
class Stack
{
public:
 Stack(size_t capacity = 3)
 {
     _array = (DataType*)malloc(sizeof(DataType) * capacity);
     if (NULL == _array)
     {
        perror("malloc申请空间失败!!!");
     }
 return;
 }
     _capacity = capacity;
     _size = 0;
 }
 void Push(DataType data)
 {
     // CheckCapacity();
     _array[_size] = data;
     _size++;
 }
 // 其他方法...
 ~Stack()//析构函数释放空间
 {
     if (_array)
     {
     free(_array);
     _array = NULL;
     _capacity = 0;
     _size = 0;
     }
 }
private:
     DataType* _array;
     int _capacity;
     int _size;
};
int main()
{
    Stack s;
    s.Push(1);
    s.Push(2);
    return 0;
}

这就是一个栈的实现,我们在构造函数的时候,使用了malloc函数在堆上申请了空间,所以我们必须手动的写析构函数,在对象生命周期结束时,调用析构函数来对资源的清理。

class Time
{
public:
 ~Time()
 {
 	cout << "~Time()" << endl;
 }
private:
     int _hour;
     int _minute;
     int _second;
};
class Date
{
private:
     // 基本类型(内置类型)
     int _year = 1970;
     int _month = 1;
     int _day = 1;
     // 自定义类型
     Time _t;
};
int main()
{
     Date d;
     return 0;
}

image-20240402102221789

这里只创建了Date对象,但是最后却调用了Time类的析构函数,这其实和构造函数的原理是一样的,生成的默认析构函数对内置类型不做处理,但是对自定义类型会调用它的默认析构函数。

总结: 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如Date类;
有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。

四.构造函数和析构函数的顺序

class Time
{
public:
    Time()
    {
        cout << "Time()" << endl;
    }
    ~Time()
    {
        cout << "~Time()" << endl;
    }

private:
    int _hour;
    int _minute;
    int _second;
};
class Date
{
public:
    Date()
    {
        cout << "Date()" << endl;
    }
    ~Date()
    {
        cout << "~Date()" << endl;
    }
private:
    int _year = 1970;
    int _month = 1;
    int _day = 1;
};
int main()
{
    Date d;
    Time t;
    return 0;
}

image-20240402104421818

构造函数是先创建的对象先调用,而析构函数和构造函数恰好相反,先创建后调用。

五.拷贝构造函数

① 拷贝构造函数的概念

拷贝构造函数从字面意思来理解就是,拷贝一个对象,构造出一个新的对象,简单点就是,复制出一个一模一样的对象。

拷贝构造函数: 只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。

② 拷贝构造函数的特征

Date(const Date& d) //拷贝构造函数长这个样

乍一看,耶,这玩意儿咋个和构造函数那么像呢?没错,他就是构造函数的兄弟。

析构函数也特殊的成员函数,特征如下:

1.拷贝构造函数是构造函数的一个重载形式。(这就是构造函数的兄弟)

2.拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错, 因为会引发无穷递归调用

class Date
{
public:
 Date(int year = 1900, int month = 1, int day = 1)
 {
     _year = year;
     _month = month;
     _day = day;
 }
 // Date(const Date& d)   // 正确写法
    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);//这是拷贝构造函数的调用,使用d1对象拷贝出d2对象
 return 0;
}

对于调用拷贝构造函数,有两种方式,一种是Date d2(d1);还有一种是Date d2=d1。这两种形式是等价的。

image-20240402112224734

这个程序运行起来就会报错,就是因为形参的部分我们没有使用传引用的方式,导致了无限递归。那为什么会导致无限递归呢?

因为我们传参的时候就会调用拷贝构造函数,然而拷贝构造函数又需要传参,传参又需要调用拷贝构造函数……这是一个无限循环,无穷无尽。

image-20240402113044217

故而我们使用传引用的方式,引用是别名,所以传参的时候就不会再调用拷贝构造函数,也就不会导致无穷递归了。而且这里的参数最好使用const修饰一下,这样可以防止你误修改d对象中的成员变量。

image-20240402113951789

3.若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。

class Time
{
public:
    Time()
    {
        _hour = 1;
        _minute = 1;
        _second = 1;
    }
    Time(const Time& t)
    {
        _hour = t._hour;
        _minute = t._minute;
        _second = t._second;
        cout << "Time::Time(const Time&)" << endl;
    }
private:
    int _hour;
    int _minute;
    int _second;
};
class Date
{
private:
    // 基本类型(内置类型)
    int _year = 1970;
    int _month = 1;
    int _day = 1;
    // 自定义类型
    Time _t;
};
int main()
{
    Date d1;

    // 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
    // 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
    Date d2(d1);
    return 0;
}

image-20240402160542949

d1拷贝构造d2,但是Date类里面没有拷贝构造函数,所以编译器生成了一个默认的拷贝构造函数,对三个内置类型完成简单的值拷贝,对于内置类型Time类,则会调用它的拷贝构造函数,于是就会执行Time类的拷贝构造函数,打印出那句话。

4.编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗? 当然像日期类这样的类是没必要的。

但是当我们实现栈这样的类,调用拷贝构造函数stack s1(s2)时,却不自己实现拷贝构造函数,编译器就会自己生成一个默认的拷贝构造函数,就会进行值拷贝,所以s1的内容会原封不动的拷贝给s2,即s1和s2会指向同一块空间,这里有两个对象,析构就会一共析构两次,而且是对同一块空间连续析构两次,导致程序崩溃。

image-20240402162641914

**总结:**对于有资源的申请,需要自己实现拷贝构造函数,对于无资源的申请,写或不写都可以。

六.赋值运算符重载

① 运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

这里的函数名字和普通的函数名字有点不一样。

运算符重载函数名字: 关键字operator后面加上需要重载的运算符。
比如operator++,operator–,operator>……。

一些运算符重载的注意点:

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型参数,因为运算符重载大多数都是在类中进行的,所以形参有个隐含的this指针。
  • 对于内置类型的符号,你不能重载改变它的含义,比如 ‘+’ ,这是进行相加操作,你不能把它重载成其他的意思。
  • .* :: sizeof ?: . 注意以上5个运算符不能重载。

我们可以简单的写一个类,来使用一下运算符重载。

// 全局的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;
}
int main()
{
    Date d1(2018, 9, 26);
    Date d2(2018, 9, 27);
    cout<<(d1 == d2)<<endl;
    return 0;
}

image-20240403094059957

如果你这样写,operator==写成全局函数,那么你就是在类外,如果要访问类中的成员变量,你就必须把类中的成员变量开放成public公有的,但是你这样做,就不能保证其封装性了,后续学习中使用友元函数可以解决这个问题,故而我们干脆把该运算符重载函数写成成员函数。

class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    // bool operator==(Date* this, const Date& d2)
    bool operator==(const Date& d2)//这里还有个this指针
    {
        return _year == d2._year
            && _month == d2._month
            && _day == d2._day;
    }
    //这个就pass掉了
    /*bool operator==(const Date& d1, const Date& d2)
     {
        return d1._year == d2._year
     && d1._month == d2._month
        && d1._day == d2._day;
     }*/
    
private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    Date d1(2018, 9, 26);
    Date d2(2018, 9, 27);
    cout << (d1 == d2) << endl;
    return 0;
}

运行结果:

image-20240403094833755

小细节:这里的(d1==d2)会被编译器识别为d1.operator ==(&d1,d2),但是我们写只用写两个操作对象加中间的操作符,非常的方便。

② 赋值运算符重载

1.赋值运算符重载格式

  • 参数:使用const &传递,提高效率。
  • 返回值:T&,返回值使用引用提高返回效率,如遇到连续赋值的情况下。
Myclass obj1,obj2,obj3;
obj1=obj2=obj3;

如果重载的赋值运算符返回的是对象本身的引用,就可以通过连续赋值来实现链式操作,这在某些情况下可以提高代码的简洁性和可读性。

赋值运算符重载写成成员函数:

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;
   }
 //同样这里有个隐含的this指针
 //Date& operator=(Date*this,const Date& d)
 Date& operator=(const Date& d)
 {
       if(this != &d)
       {
            _year = d._year;
            _month = d._month;
            _day = d._day;
       }
        return *this;
 }
private:
 int _year ;
 int _month ;
 int _day ;
};

那我们把赋值运算符重载写成全局函数呢?答案是不能,验证一下。

class Date
{
public:
 Date(int year = 1900, int month = 1, int day = 1)
 {
     _year = year;
     _month = month;
     _day = day;
 }
 int _year;
 int _month;
 int _day;
};
// 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数
Date& operator=(Date& left, const Date& right)
{
 if (&left != &right)
 {
     left._year = right._year;
     left._month = right._month;
     left._day = right._day;
 }
 return left;
}

image-20240403102123357

把这个代码放在VS上会直接报错,因为operator=只能是成员函数。

对于赋值运算符重载为什么只能是成员函数,而不能是全局函数

: 因为赋值运算符重载也是一个默认的成员函数,如果你不在类中实现赋值运算符重载,那么编译器会为该类生成一个默认的赋值运算符重载,如果你还在类外实现了一个全局的赋值运算符重载,两个赋值运算符重载就会发生冲突,故赋值运算符重载只能是类的成员函数。

2. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。
注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。

赋值运算符重载函数和拷贝构造函数是一样的,如果类中没有资源的申请,自己写不写都可以;反之,有资源的申请,那么你必须自己写这两个函数。

③ 前置++和后置++重载的区别

class Date
{
public:
 Date(int year = 1900, int month = 1, int day = 1)
 {
     _year = year;
     _month = month;
     _day = day;
 }
 // 前置++:返回+1之后的结果
 // 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
 Date& operator++()
 {
     _day += 1;
     return *this;
 }
 // 后置++:
 // 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
 // C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
 // 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,然后给this+1
 // 而temp是临时对象,因此只能以值的方式返回,不能返回引用
 Date operator++(int)
 {
     Date temp(*this);//先将旧的this保存下来。
     _day += 1;
     return temp;
 }
private:
     int _year;
     int _month;
     int _day;
};
int main()
{
     Date d;
     Date d1(2022, 1, 13);
     d = d1++;    // d: 2022,1,13   d1:2022,1,14
     d = ++d1;    // d: 2022,1,15   d1:2022,1,15
 return 0;
}

前置++:

image-20240403115239004

后置++:

image-20240403115306463

因为这是简单的日期类,所以不用自己实现拷贝构造函数,赋值运算符重载函数,析构函数,编译器生成的默认的,就已经够用了,这里其实很多函数都可以进行复用,实现这个日期类还是比较简单的,这里有个好文章,细节的实现了日期类,不懂的可以去看看:C++要笑着学

7.const成员

class Date
{
public:
    Date(int year=1, int month=1, int day=1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << "Print()" << endl;
    }
private:
    int _year; // 年
    int _month; // 月
    int _day; // 日
};
int main()
{
    Date d1;
    d1.Print();
    return 0;
}

这里d1对象调用Print函数自然是可以调用。

image-20240404152650329

但是我们在该对象前面加上一个const呢?

image-20240404152800063

原因是权限放大了,为了可以用const对象调用成员函数,那么只有在成员函数后面加上const。

在Print()成员函数中有个隐藏的this指针,它的原型是Date const this,这个const是修饰的this指针本身不能被修改,在该函数后面加上的const,会使得原型变为const Date * const this*,新加的这个const是修饰*this,保护成员变量不被修改。

我们写的:
void Print() const 
{
   cout << "Print()" << endl;
}

实际上的:
void Print(const Date *const this) 
{
   cout << "Print()" << endl;
}

为了增加代码的可靠性,我们建议成员函数后面都加上const。

对于const:

  • 权限可以缩小
  • 权限可以平移
  • 权限不可以放大
  1. const对象可以调用非const成员函数吗? 答:能,权限缩小
  2. 非const对象可以调用const成员函数吗? 答:不能,权限放大
  3. const成员函数内可以调用其它的非const成员函数吗? 答:不能,权限放大
  4. 非const成员函数内可以调用其它的const成员函数吗? 答:能,权限缩小

const不是在成员函数后面都能加上,像构造函数后面就不能加上const,因为构造函数的目的是初始化对象的状态,也就像是在修改对象的状态,但是const关键字用于指示成员函数不会修改对象的状态,因此在构造函数后面添加const是不合法的。

对于const的使用标准:只读函数可以加上const,内部不涉及修改。
对于加上const的成员函数,非const对象和const对象都能调用。

八.日期类的实现理解默认成员函数

class Date
{
public:
    // 获取某年某月的天数
    int GetMonthDay(int year, int month) const
    {
        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)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print() const
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    // 拷贝构造函数
    // d2(d1)
    //Date(const Date& d);

    // 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
    //Date& operator=(const Date& d);
    // 析构函数
    //~Date();
    // 日期+=天数
    Date& operator+=(int day) //设计内部成员的修改,不能加const
    {
        _day += day;
        while (_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            _month++;
            if (_month > 12)
            {
                _year++;
                _month = 1;
            }
        }
        return *this;
    }
    // 日期+天数
    Date operator+(int day) const
    {
        Date tmp(*this);
        tmp += day;
        return tmp;
    }
    // 日期-天数
    Date operator-(int day) const
    {
        Date tmp(*this);
        tmp -= day;
        return tmp;
    }
    // 日期-=天数
    Date& operator-=(int day)//同样设计内部成员的修改,不能加上const
    {
        _day -= day;
        while (_day < 0)
        {
            _day += GetMonthDay(_year, _month);
            _month--;
            if (_month < 1)
            {
                _year--;
                _month = 12;
            }
        }
        return *this;
    }
    // 前置++
    Date& operator++()
    {
        *this += 1;
        return *this;
    }
    // 后置++
    Date operator++(int)
    {
        Date tmp(*this);
        *this += 1;
        return tmp;
    }
    // 后置--
    Date operator--(int)
    {
        Date tmp(*this);
        *this += 1;
        return tmp;
    }
    // 前置--
    Date& operator--()
    {
        *this += 1;
        return *this;
    }

    // >运算符重载
    bool operator>(const Date& d) const
    {
        if (_year > d._year)
        {
            return true;
        }
        else if (_year == d._year && _month > d._month)
        {
            return true;
        }
        else if(_year == d._year && _month == d._month&&_day>d._day)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    // ==运算符重载
    bool operator==(const Date& d) const
    {
        return _year ==d._year&&
            _month == d._month
            && _day == d._day;
    }
    // >=运算符重载
    bool operator >= (const Date& d) const
    {
        return (*this > d) || (*this == d);
    }

    // <运算符重载
    bool operator < (const Date& d) const
    {
        return !(*this >= d);
    }
    // <=运算符重载
    bool operator <= (const Date& d) const
    {
        return !(*this < d);
    }
    // !=运算符重载
    bool operator != (const Date& d) const
    {
        return !(*this == d);
    }
    // 日期-日期 返回天数
    int operator-(Date& d) const
    {
        Date max(*this);
        Date min(d);
        int flag = 1;
        if (*this < d)
        {
            max = d;
            min = *this;
            flag = -1;
        }
        int count = 0;
        while (max != min)
        {
            min++;
            count++;
        }
        return count * flag;
    }
private:
    int _year;
    int _month;
    int _day;
};

九.取地址及const取地址操作符重载

class Date
{ 
public :
 Date* operator&()
 {
    return this ;
 }
 const Date* operator&()const
 {
    return this ;
 }
private :
   int _year ; // 年
   int _month ; // 月
   int _day ; // 日
};

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值