C++【類和對象】
- 1.運算符重載
- 2.賦值運算符重載
- 3.日期類的實現
1.運算符重載
(1).C++規定類類型運算符使用時,必須轉換成調用運算符重載。
(2).運算符重載是具有特殊名字的函數,名字等于operator加需要使用的運算符,具有返回類型和參數列表及函數體。
(3).重載運算符函數的參數個數和該運算符作用的運算對象的個數保持一致,一元運算符有一個參數,二元運算符有兩個參數,二元運算符左邊的傳給第一個參數,右邊的傳給第二個參數。
(4).如果一個重載運算符函數是成員函數,那么它的第一個函數參數是this指針,所以顯示出來就少一個參數。
(5).運算符重載以后,其優先級和結合性與對應的內置類型運算符保持?致。
(6).不能通過連接語法中沒有的符號來創建新的操作符:比如operator@
(7).
這五個運算符不能重載。
(8).C++為了區分前置++和后置++運算符重載函數,規定前置為operator++(),后置++為operator++(int)
#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}bool operator == (const Date& d){return _year == d._year&& _month == d._month&& _day == d._day;}//d1-d2
int operator-(const Date& d);
//日期-天數
Date operator-(int day);
private:int _year;int _month;int _day;};
int main()
{Date d1(2024, 3, 20);Date d2(2025, 3, 18);if (d1 == d2){cout << "相等" << endl;}elsecout << "不相等" << endl;return 0;
}
2.賦值運算符重載
賦值運算符重載是一個默認成員函數(不能重載到全局),用于完成兩個已經存在的對象直接的拷貝賦值。
賦值運算符重載的特點:
(1).賦值運算符重載是一個函數重載,規定重載必須為成員函數,賦值運算重載的參數建議寫成const 當前類類型引用,否則會傳值傳參會有拷貝。
(2).有返回值,且建議寫成當前類類型引?,引?返回可以提高效率,有返回值目的是為了支持連續賦值場景。
(3).沒有顯式實現時,編譯器會自動生成?個默認賦值運算符重載,默認賦值運算符重載行為跟默認拷貝構造函數類似,對內置類型成員變量會完成值拷貝/淺拷貝(?個字節?個字節的拷貝),對自定義類型成員變量會調用他的賦值重載函數。
(4).如果一個類顯示實現析構并釋放了資源,就需要我們顯示實現賦值運算符重載,否則就不需要。
#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}Date& operator=(const Date& d){if (this != &d){_year = d._year;_month =d._month;_day = d._day;}return*this;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(2004,8,18);Date d2(d1);d1.Print();d2.Print();return 0;
}
3.日期類的實現
//Date.h
#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}int GetMonthDay(int year,int month){int monthDayArray[13] = { 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;}elsereturn monthDayArray[month];}Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);// ++d1 ->d1.operator++()Date& operator++();// d1++ ->d1.operator++(1);Date operator++(int);Date& operator--();Date operator--(int);bool operator<(const Date& d);bool operator<=(const Date& d);bool operator>(const Date& d);bool operator>=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);int operator-(const Date& d);void Print();
private:int _year;int _month;int _day;
};
//Date.cpp
#include"Date.h";
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_year++;_month = 1;}}return *this;
}// d1 + 100
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}// ++d1
Date& Date::operator++()
{*this += 1;return *this;
}// d1++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// d1 < d2
bool Date::operator<(const Date& d)
{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 Date::operator==(const Date& d)
{return _year == d._year&& _month == d._month&& _day == d._day;
}// d1 <= d2
bool Date::operator<=(const Date& d)
{return *this < d || *this == d;
}bool Date::operator>(const Date& d)
{return !(*this <= d);
}bool Date::operator>=(const Date& d)
{return !(*this < d);
}
//test.cpp
#include"Date.h"void TestDate1()
{Date d1(2025, 3, 9);d1.Print();Date d2 = d1 + 100;d2.Print();d1.Print();Date d3 = d1 += 100;d1.Print();d3.Print();
}void TestDate2()
{Date d1(2025, 3, 9);Date ret1 = ++d1;//Date ret1 = d1.operator++();d1.Print();ret1.Print();Date d2(2025, 3, 9);Date ret2 = d2++;//Date ret2 = d2.operator++(10000);d2.Print();ret2.Print();
}int main()
{TestDate2();return 0;
}