基于YOLO与卡尔曼滤波的瞳孔追踪优化方案
2026/8/31 14:14:33
在C++编程中,类与对象是面向对象编程的核心概念。今天,我们将通过实现一个功能完整的日期类,来深入理解C++中类的设计、封装、运算符重载等关键知识点。这个日期类不仅是一个优秀的教学示例,也包含了实际开发中的许多最佳实践。
class Date { private: int _year; // 年份 int _month; // 月份 int _day; // 日期 public: // 公有接口 };为什么要将成员变量设为私有?
Date::Date(int year = 1900, int month = 1, int day = 1) { _year = year; _month = month; _day = day; if (!CheckDate()) // 构造函数中进行数据验证 { cout << "日期非法" << endl; } }构造函数的特点:
int GetMonthDay(int year, int month) { assert(month > 0 && month < 13); static int monthDayArray[13] = { -1, 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; } else { return monthDayArray[month]; } }代码亮点:
static数组避免重复初始化assert进行参数检查bool Date::CheckDate() { if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month)) { return false; } return true; }bool Date::operator<(const Date& d) const { if (_year < d._year) return true; else if (_year == d._year) { if (_month < d._month) return true; else if (_month == d._month) { return _day < d._day; } } return false; }比较运算符的重载技巧:
// 基于 < 和 == 实现其他比较运算符 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); }关键设计理念:先实现复合赋值运算符(+=、-=),再基于它们实现算术运算符(+、-)。
// 1. 先实现 += Date& 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; // 返回引用,支持链式调用 } // 2. 基于 += 实现 + Date Date::operator+(int day) const { Date tmp = *this; // 拷贝当前对象 tmp += day; // 复用 += 的实现 return tmp; // 返回新对象 }为什么要这样设计?
+=直接修改对象,效率高于通过+实现// 前置++:先自增,后返回 Date& Date::operator++() { *this += 1; return *this; // 返回自身引用 } // 后置++:先保存原值,再自增,返回原值 Date Date::operator++(int) // int参数仅用于区分 { Date tmp(*this); // 保存原值 *this += 1; // 自身自增 return tmp; // 返回原值副本 }区别总结:
++:返回引用,效率高++:返回对象副本,效率较低int:仅用于编译器区分,不实际使用int Date::operator-(const Date& d) const { Date max = *this; Date min = d; int flag = 1; if (*this < d) // 比较日期大小 { max = d; min = *this; flag = -1; } int n = 0; while (min != max) // 逐天计数 { ++min; ++n; } return n * flag; // 返回带符号的天数差 }// 错误示例:作为成员函数 void Date::operator<<(ostream& out) // 第一个参数是this { out << _year << "-" << _month << "-" << _day; } // 使用:d1 << cout; // 不符合习惯 // 正确示例:友元函数 friend ostream& operator<<(ostream& out, const Date& d) { out << d._year << "年" << d._month << "月" << d._day << "日"; return out; } // 使用:cout << d1; // 自然直观流运算符的特点:
class Date { public: // const成员函数:承诺不修改对象 void Print() const; bool operator<(const Date& d) const; Date operator+(int day) const; // 非const成员函数:可能会修改对象 Date& operator+=(int day); Date& operator++(); };const成员函数的重要性:
int main() { // 1. 创建对象 Date d1(2024, 4, 14); Date d2(2024, 12, 31); // 2. 日期运算 cout << "d1: " << d1 << endl; Date d3 = d1 + 100; // 100天后 cout << "100天后: " << d3 << endl; d1 += 30; // d1增加30天 cout << "d1增加30天: " << d1 << endl; // 3. 日期比较 if (d1 < d2) cout << "d1在d2之前" << endl; // 4. 日期差 int days = d2 - d1; cout << "相差天数: " << days << endl; // 5. 自增运算 Date d4 = d1++; cout << "d1后置++: " << d1 << endl; cout << "d4(原值): " << d4 << endl; Date d5 = ++d1; cout << "d1前置++: " << d1 << endl; cout << "d5(新值): " << d5 << endl; return 0; }=、+=、-=)+、-)inline函数减少函数调用开销const提高编译器优化机会这个日期类还可以进一步扩展:
通过实现这个日期类,我们深入理解了C++类与对象的核心概念:
这个日期类不仅是一个实用的工具,更是一个优秀的学习示例。希望通过对它的分析,能帮助你更好地理解C++面向对象编程的精髓。
记住:好的类设计应该让使用变得简单,让维护变得容易,让扩展变得可能。这就是面向对象编程的魅力所在。