类和对象(中)后面部分
目录
前置++和后置++
日期类的实现
运算符重载
容易出现的问题
日期- 日期
流操作符的重载
cin与cout
在竞赛中想要去提升效率
const
const 对象可以调用非const的成员函数吗:X
const取地址和取地址
权限可以缩小,但是不可以放大
优点:
类型的转换
大家一起加油,写完真的也不简单,fight,fight,fight
前置++和后置++
Data& Data:: operator++()
{*this += 1;return *this;
}Data Data::operator++(int)//有参数是后置
{Data tmp = *this;*this += 1;return tmp;
}
由于前置++我们使用的比较多,可以减少拷贝的次数,所以我们为了方便就去在后置++的函数后面加上一个参数,但是其实这个参数是不用传递任何东西的,只是为了方便去区分前置++和后置++的
日期类的实现
int Getmonthdays(int year, int month)
{assert(year > 0 && month < 13);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;
}
当然加是进位逻辑,见识一个借位逻辑
这里面的day数组前面加上了static了,是应为我们要平凡调用,所以我们放到静态区比较好,回减少我们来回开辟的时间,当然这段获取某年某月的天数的函我们不用去声明,因为在类里面的成员,默认是类联的,这样的话调用的会比较快。
这里面有一个小技巧,4年一闰。百年不闰,400年又闰。
运算符重载
Data& operator+=(int day);Data operator+(int day);Data& operator-=(int day);Data operator-(int day);bool operator<(const Data& day);bool operator>(const Data& day);bool operator>=(const Data& day);bool operator==(const Data& day);bool operator!=(const Data& day);Data& operator++();Data operator++(int);//有参数是后置bool checkData();//检测日期是否合法
在函数里面我们可以互相去复用,但是我们要去写一个完整的,这里面我们以 - 和 -= 为例子, 我们可以去相互复用,但是我们建议去写-=比较好,然后-在去复用-=,对与-来说都是一样的,但是如果-=自己写的话就没有 了拷贝了,如果去调用-的话就吃亏了。
容易出现的问题
如果我们去在++的时候用到了-88,也就是,这样的话就会出现负数,则需要考虑day会不会出现负数,+=-100就相当于-=100
我们的比较的赋值重载中,比如说d1<d2中
bool Data::operator<(const Data& d)
{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;
}
我们可以先将true的情况给拿出来,这里面d1是传给*this,d2是传给day的,复用的话可以*this<d1,和数学里面的正难则反差不多,只要写了大于,那么小于就是不大于
日期- 日期
d1-d2的时候,我们是要的到天数,如果天数不够的话,我们就要去借位,这里面有许多的方法,我们多定义一个参数让小的不断加数字然后看看加到多少就相等了,其余的方法就十分的不方便,因为要考虑借位的是不是闰年。
这里给大家看一下函数的样子
Data Data:: operator- (int day) //区防止借位不够
{Data tmp = *this;tmp._day -= day;while(tmp._day <= 0)//{tmp._month--;if (tmp._month == 0){tmp._year--;tmp._month = 12;}tmp._day += Getmonthdays(tmp._year, tmp._month);}return *this;
}
流操作符的重载
在c语言中,我们的打印printf要去取对应%d或者其他的内置类型,而在c++中cin能够自动识别出自定义类型和内置类型,能够识别函数重载
istream& operator>>(istream& in, Data& d)
{cout << "请输入你要写的数字";in >> d._year >> d._month >> d._day;while (1){if (!d.checkData()){cout << "非法日期";d.print();cout << "请重新输入";in >> d._year >> d._month >> d._day;}else{break;}}return in;
}
ostream& operator<<(ostream& out,const Data& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
这里面注意看一下返回值,在写了返回值的时候就可以连续的输入和输出,如果只是去写void就没有办法去连续的输入输出
这里面我们将cout可以理解为终端,其实在一开始我们都是不去传后面的const Data&d的,但是后面发现了一个倒翻天罡的写法,将终端流向了日期,我们当时写成了data<<cout,也是十分的离谱,后面我们发现第一个会被this指针给站了,后面加上一个const变成一个全局的,重载以后将osteram放在第一个当做了形参,第二个放到了类类型,但是在cin,也就是流提取的时候就不要加上const了,我们要去修改里面的值
我们知道流插入是从左向右,而流提取是从右向左,在流提取的时候我们的istream&是要必须引用的,因为它不支持拷贝构造
cin与cout
在cin与cout是由绑定关系的,默认是绑定在一起的,在cin和cout使用前会对相互的缓冲区给刷一下,也就是在cin的重载里面写cout也是没有关系的
在竞赛中想要去提升效率
我们可加上
std::ios::sync_with_stdio(false); // 解除与 C 标准库同步std::cin.tie(nullptr); // 解除 cin 和 cout 的绑定
const
用const修饰的成员函数就被称为const成员函数,const实际上修饰隐含的this指针,表明该成员函数不能对类的任何成员进行修改
const 对象可以调用非const的成员函数吗:X
这样会让权限放大,但是权限是可以缩小的,所以非const对象是可以调用const的成员函数的
取地址和const取地址一般不用去定义
const取地址和取地址
这两个函数我们一般都不需要去重载,编译器会自动生成默认的重载,只有在特殊的情况下才需要去重载,比如说想让别人获取到特定的内容
权限可以缩小,但是不可以放大
我们在接收对象的时候,可以会接受到普通的对象,也可能会接受到const修饰的对象
考虑到会有权限的放大与缩小,我们要想办法在传值的时候再需要的时候,加一个const
当然不能全部加上const,比如说在拷贝构造里面,不能加上const,不修改的前面加上const
取地址是不用重载的,但是默认类型和自定义类型都是需要重载
当然:可以写提个错误的地址来阻止去访问
优点:
可以检查比如说==有没有写成=。这样的汇报报错,因为不可以修改,
类型的转换
类型的转换会先定义一个参数然后再去赋值
int i = 1;double d = i;const double& rd = i;
中间的变量是常属性的,所以在赋值的时候要在前面加上const
函数的形态,太多了,所以我放在了后面
Data& Data:: operator+= (int day)
{if (day < 0){return *this -= (-day);}_day += day;while (_day > Getmonthdays(_year, _month)){_day -= Getmonthdays(_year, _month);++_month;if (_month == 13){_year++;_month = 1;}}return *this;
}
Data& Data:: operator++()
{*this += 1;return *this;
}Data Data::operator++(int)//有参数是后置
{Data tmp = *this;*this += 1;return tmp;
}bool Data::checkData()
{if (_month < 1 || _month>12|| _day<1 || _day>Getmonthdays(_year, _month)){return false;}else{return true;}
}
Data Data:: operator+(int day)//becaues it is a local variable , it cannot be passed by inference
{Data tmp = *this;tmp += day;//while (tmp._day > Getmonthdays(tmp._year, tmp._month))//{// tmp._day -= Getmonthdays(tmp._year, tmp._month);// ++tmp._month;// if (tmp._month == 13)// {// tmp._year++;// tmp._month = 1;// }//}return tmp;
}
Data& Data:: operator-= (int day) //区防止借位不够
{if (day < 0){return *this += (-day);}_day -= day;while(_day <= 0)//{_month--;if (_month == 0){_year--;_month = 12;}_day += Getmonthdays(_year,_month);}return *this;
}
// Data Data:: operator- (int day) //区防止借位不够
{Data tmp = *this;tmp._day -= day;while(tmp._day <= 0)//{tmp._month--;if (tmp._month == 0){tmp._year--;tmp._month = 12;}tmp._day += Getmonthdays(tmp._year, tmp._month);}return *this;
}ostream& operator<<(ostream& out,const Data& d){out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;}//Data& Data:: operator-= (int day)
//{
// *this = *this - day;
// return *this;
//}//
//Data Data::operator-(int day)
//{
//
// Data tmp = *this;
// tmp -= day;//用二元操作符
//
// return tmp;//返回值是tmp,类型是Date类型
//}bool Data::operator == (const Data & d)
{return _year == d._year&& _month == d._month&&_day == d._day;
}bool Data::operator<(const Data& d)
{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 Data:: operator>=(const Data & day)
{return !(*this < day);
}
bool Data::operator!=(const Data& day)
{return !(*this == day);
}bool Data::operator>(const Data& d)
{if (_year > d._day){return true;}elseif (_year == d._year){if (_month > d._month){return true;}else if (_month = d._month){return _day > d._day;}}return false;
}int Data::operator- (const Data& d)
{int flag = 1;Data max = *this;Data min = d;if (*this < d){flag = -1; max = d;min = *this;}int n = 1;while (min != max){++min;++n;}return flag * n;
}istream& operator>>(istream& in, Data& d)
{cout << "请输入你要写的数字";in >> d._year >> d._month >> d._day;while (1){if (!d.checkData()){cout << "非法日期";d.print();cout << "请重新输入";in >> d._year >> d._month >> d._day;}else{break;}}return in;
}