提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
Date类中构造函数的功能是对变量进行初始化,若不写,系统会默认生成。但对于Date类而言,系统默认生成的构造函数初始化的年月日是随机值。为使得d1在初始化时就有一个确定的值,因此自定义一个全缺省的构造函数。这样,每个创建出来的日期类变量都有一个初始值1900,1,1。
Date(int year = 1900, int month = 1, int day = 1);
// 全缺省构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;//检查日期是否合法if (!(year >= 1&& (month >= 1 && month <= 12)&& (day >= 1 && day <= GetMonthDay(year, month)))){cout << "非法日期" << endl;}
}
析构函数是销毁某些特定的成员变量,销毁的意义是取消指定变量地址的使用权。对于malloc出的内存,若不通过析构函数指定操作,则很可能造成内存泄漏。对于Date类中的成员,生长在栈帧上,会随着函数栈帧结束自动释放掉,因此使用默认析构函数即可。
创建对象时使用同类对象初识化,拷贝构造函数其实是一个构造函数的重载,依次拷贝类成员进行初始化。若不写,系统也会默认生成。
传值返回:返回过程中生成一个临时变量,并且会调用一次构造函数,开销较大
传引用返回:若要返回的值出了当前函数作用域还在(如:静态变量 static 或者传值传来的变量),只要生命周期大于函数的周期,便可使用传引用返回。另外还需考虑这个变量会不会改变,如d1+1,d1不会改变,需要返回的值只可以是类里面创建的对象,只能传值返回;如d1+=1,d1改变,即可通过传引用返回。
若写好了 ’ > ‘,则可通过‘ !< ’实现‘ <= ’逻辑。这样代码效率更高,需要修改和调整的部分也更少,只需检查实现’ > '逻辑的函数即可。
两者实现的逻辑相似,区别是后置操作会传一个(int)形参,作用只用于区分前置和后置。
C++中,可以重载输入输出流操作符<<与>>来自定义其输入输出功能。
但若在流对象之前使用const,编译器会报错输入流对象 没有匹配的 常量类型 输出。
这是由于:const 是用来修饰不会改变的对象或变量状态的常量修饰符。而此处不能在重载的流对象之前加 const 说明Date类的友元函数输出流对象之后改变了流对象本身的状态。
另外,为实现cout<
Date.h
class Date{// 友元声明(类的任意位置)friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:// 获取某年某月的天数int GetMonthDay(int year, int month);// 全缺省的构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数// d2(d1)// 形参加const,防止写反Date(const Date& d);// 析构函数~Date(){}// 打印void Print();// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date& d);// 日期+=天数Date& operator+=(int day);// 日期+天数Date operator+(int day);// 日期-天数Date operator-(int day);// 日期-=天数Date& operator-=(int day);// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();// >运算符重载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);private:int _year;int _month;int _day;};// cout << d1 => operator<<(cout, d1);
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}// cin >> d1 operator(cin, d1);
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}
Date.cpp
// 获得某年某月某日
int Date::GetMonthDay(int year, int month)
{static 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;}else{return monthDayArray[month];}
}// 全缺省构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;//检查日期是否合法if (!(year >= 1&& (month >= 1 && month <= 12)&& (day >= 1 && day <= GetMonthDay(year, month)))){cout << "非法日期" << endl;}
}// 拷贝构造函数
// 形参加const,防止写反
Date::Date(const Date & d)
{/*cout << "Data 拷贝构造" << endl;*/_year = d._year;_month = d._month;_day = d._day;}// 打印
void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}// 日期+=天数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;
}// 日期+天数Date Date::operator+(int day)
{Date ret(*this);ret += day;return ret;
}// 日期-天数Date Date::operator-(int day)
{Date ret(*this);ret -= day;return ret;}// 日期-=天数Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}// 前置++
Date& Date::operator++()
{*this += 1;return *this;
}// 后置++ -- 多一个int参数主要是为了区分前置
// 构成函数重载
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// 后置--Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}// 前置--Date& Date::operator--()
{*this -= 1;return *this;
}// >运算符重载
// 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;}return false;
}// ==运算符重载
bool Date::operator==(const Date& d)
{return _year == d._year&& _month == d._month&& _day == d._day;
}// >=运算符重载
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);
}// !=运算符重载
bool Date::operator!=(const Date& d)
{return !(*this == d);
}// 日期-日期 返回天数
// d1-d2
int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;++min;}return n * flag;
}