大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在写日期类日期计算器之前先实现了一个简单的复数类 //引用做参数,1.swap--在函数内部改变参数,2.Bigdata提高效率 //内联函数必须在定义处加上inline //定义在类内部的函数默认为内联函数 //c++中尽量使用const,枚举,内联去替代宏 //宏没有类型安全的检查,在预处理的时候替换了所以无法调试,宏安全性不高
创新互联专注于右江网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供右江营销型网站建设,右江网站制作、右江网页设计、右江网站官网定制、成都小程序开发服务,打造右江网络公司原创品牌,更为您提供右江网站排名全网营销落地服务。
class Complex { public: Complex(double real = 0.0, double p_w_picpath = 0.0)//定义了一个全缺省的构造函数 :_real(real) , _p_w_picpath(p_w_picpath) { cout << "Complex()" << endl;//这样可以在屏幕上输出构造函数的信息,方便分析构造函数的调用 } Complex(const Complex& c)//拷贝构造函数 { cout << "Complex(const Complex& c)" << endl; _real = c._real; _p_w_picpath = c._p_w_picpath; } ~Complex()//析构函数 { cout << "~Complex()" << endl; } Complex& operator=(const Complex& c)// Complex返回值支持连续的赋值 { cout << "Complex& operator=()" << endl; if (this != &c) { _real = c._real; _p_w_picpath = c._p_w_picpath; } return *this; } void Display() { cout << _real << "-" << _p_w_picpath << this-="">_real++; this->_p_w_picpath++; return *this; } Complex operator++(int)//后置++,返回加之前的值,int用来占位,形成重载 { Complex ret(*this); this->_real++; this->_p_w_picpath++; return ret; } bool operator== (const Complex& c) { IsEqual(c); } bool IsEqual(const Complex& c) { return _p_w_picpath == c._p_w_picpath&&_real == c._real; } bool operator> (const Complex& c) { MoreThan(c); } bool MoreThan(const Complex& c) { if (_real > c._real) { return true; } else if (_real == c._real &&_p_w_picpath > c._p_w_picpath) { return true; } return false; } Complex operator+ (const Complex& c) { /*Complex ret; ret._real = _real + c._real; ret._p_w_picpath = _p_w_picpath + c._p_w_picpath;*/ Complex ret(*this); ret._real += c._real; ret._p_w_picpath += c._p_w_picpath; return ret; } Complex& operator+= (const Complex& c) { _real += c._real; _p_w_picpath += c._p_w_picpath; return *this; } private: double _real; double _p_w_picpath; };
class Date { public: /*Date() { cout << "Date()" << endl; }*/ Date(int year = 1900, int month = 1, int day = 1) //使用初始化列表更高效 //必须使用初始化列表初始化的成员变量 //1.常量成员变量 //2.引用类型成员变量 //3.没有缺省的构造函数的类成员变量 { //cout << "Date(int year = 1900, int month = 1, int day = 1)" << if="" year=""> 1900 && month > 0 && month<13 day="">0 && day <= GetMonthDay(year, month)) { _year = year; _month = month; _day = day; } else { cout << "非法日期" << endl; } } Date(const Date& d) { //cout << "Date(const Date& d)" << endl; _year = d._year; _month = d._month; _day = d._day; } Date& operator=(const Date& d) { //cout << "Date& operator=(const Date& d)" << endl; if (this != &d) { _year = d._year; _month = d._month; _day = d._day; } return *this; } ~Date() { //cout << "~Date" << void="" -=""> const Date* this { cout << _year << "-" << _month << "-" << _day << public:="" bool="" operator="" return="" _year="=" d._year="" _month="=" d._month="" _day="=" const="" this="="> (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; return (_year > d._year) || ((_year == d._year) && (_month > d._month)) || ((_year == d._year) && (_month == d._month) && (_day > d._day)); } bool operator >= (const Date& d) { return (*this > d) || (*this == d); } bool operator <(const return="" this="">= d); } bool operator<=(const return="" this=""> d); } //日期计算器 Date operator+(int day) { if (day<0) return="" this="" -="" date="" tmp._day="" while="">GetMonthDay(tmp._year, tmp._month)) { tmp._day -= GetMonthDay(tmp._year, tmp._month); if (tmp._month == 12) { tmp._year++; tmp._month = 1; } else { ++tmp._month; } } return tmp; } Date& operator+=(int day) { /*if (day < 0) { return *this - (-day); } _day += day; while (_day > GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); if (_month == 12) { _year++; _month = 1; } else { ++_month; } } return *this;*/ *this =*this + day; return *this; } { if (day < 0) { return *this + (-day); } Date operator-(int day) Date tmp(*this); tmp._day -= day; while (tmp._day <= 0) { if (tmp._month == 1) { tmp._year--; tmp._month = 12; } else { tmp._month--; } tmp._day += GetMonthDay(tmp._year, (tmp._month)); } return tmp; } Date& operator-=(int day) { /*if (day < 0) { return *this + (-day); } _day -= day; while (_day < 0) { _day += GetMonthDay(_year, (--_month)); if (_month == 1) { _year--; _month = 12; } else { _month--; } } return *this;*/ *this =*this - day; return *this; } Date operator++() //前置++ { *this += 1; return *this; } Date operator++(int)//后置++ { /* Date tmp(*this); tmp._day += 1; while (_day > GetMonthDay(tmp._year, tmp._month)) { tmp._day -= GetMonthDay(tmp._year, tmp._month); if (tmp._month == 12) { tmp._year++; tmp._month = 1; } else { tmp._month++; } } return tmp;*/ Date tmp(*this); *this += 1; return *this; } Date operator--()//前置-- { *this -= 1; return *this; } Date operator--(int)//后置-- { /*Date tmp(*this); tmp._day -= 1; while (tmp._day < 0) { tmp._day += GetMonthDay(tmp._year, (--tmp._month)); if (tmp._month == 1) { tmp._year--; tmp._month = 12; } else { tmp._month--; } } return tmp;*/ Date tmp(*this); *this -= 1; return *this; } int operator-(const Date& d) { int flag = 1; Date max = *this; Date min = d; if (max < min) { swap(max._year, min._year); swap(max._month, min._month); swap(max._day, min._day); flag = -1; } int days = 0; while (min != max) { ++min; ++days; } return days*flag; } private: bool _IsLeapyear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; } int GetMonthDay(int year, int month) { int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = monthArray[month]; if (month == 2 && _IsLeapyear(year)) { day += 1; } return day; } private: int _year; int _month; int _day; };