总结让人明白。
表明覆盖意图的限定符 override
如图:
说明:1.使用关键字const后,由于函数特征不同,派生类不会再隐藏基类方法
2.想要覆盖基类方法可使用关键字override,此关键字会强制覆盖基类方法,若覆盖失败,则不能通过编译。
3.禁止覆盖可使用关键字final
被声明为 final 的类不能用作基类,同样,对于被声明为 final 的虚函数,不能在派生类中进行覆盖。
#include<iostream>
using namespace std;
class Fish {
public:
Fish() {}
virtual void Swim() {
cout << "Fish swims!" << endl;
}
virtual ~Fish() {}
};
class Tuna final: public Fish { //禁止再继承Tuna
public:
Tuna() {}
// override Fish::Swim and make this final
void Swim() override final { //覆盖基类Fish中的Swim方法,并禁止Tuna的子类修改Tuna版本的Swim方法(虽然Tuna不会再有子类了)
cout << "Tuna swims!" << endl;
}
virtual ~Tuna() {}
};
int main() {
return 0;
}
将复制构造函数声明为虚函数,感兴趣可以自己去了解下,这里不再提。
问与答
答:最好如此。如果编写了如下代码:
Base* pBase = new Derived();
delete pBase;
问:假如需要实现圆和三角,要求二者都必须包含Area()和Print()函数,何如?
答:
运算符类型与运算符重载
operator:n. 经营者;操作员;电话接线员;运算符
increment:n. 增长;增量;增额; 定期的加薪
decrement:n. 消耗;减量;缩减
代码举例:(简单的改变日期写法)
#include<iostream>
using namespace std;
class Date {
private:
int day, month, year;
public:
Date(int inMonth, int inDay, int inYear)
: month(inMonth), day(inDay), year(inYear) {};
Date& operator ++() {
++day;
return *this;
}
Date& operator --() {
--day;
return *this;
}
void DisPlay() {
cout << month << '/' << day << '/' << year << endl;
}
};
int main() {
Date holiday(12, 25, 2022);
holiday.DisPlay();
++holiday;
holiday.DisPlay();
--holiday;
holiday.DisPlay();
return 0;
}
样例输出:
12/25/2022
12/26/2022
12/25/2022
转换运算符:
(5条消息) C++类型转换运算符介绍_疯狂的挖掘机的博客-CSDN博客_c++类型转换运算符
(5条消息) sstream用法_南方以北的博客-CSDN博客_sstream
有些晦涩,先放一放。
智能指针:
(5条消息) 常见的智能指针_m0_60492395的博客-CSDN博客_智能指针
还是看不懂,/(ㄒoㄒ)/~~
双目运算符:
单目:一个变量 双目:两个变量
对两个操作数进行操作的运算符称为双目运算符。(第二个参数可从类属性获得)
日历类的简单操作:
#include<iostream>
using namespace std;
class Date {
private:
int day, month, year;
public:
/*
写函数时将Date当作是一个已知的类,而非通类
*/
Date(int inMonth, int inDay, int inYear)
: month(inMonth), day(inDay), year(inYear) {};
Date& operator ++() {
++day;
return *this;
}
Date& operator --() {
--day;
return *this;
}
Date operator + (int daysToAdd) { // binary addition
Date newDate (month, day + daysToAdd, year);
return newDate;
}
Date operator - (int daysToSub) { // binary subtraction
return Date(month, day - daysToSub, year);
}
void operator+=(int daysToAdd) {
day += daysToAdd;
}
void operator-=(int daysToAdd) {
day -= daysToAdd;
}
bool operator==(const Date& cmp) {
return (day == cmp.day) && (month == cmp.month) && (year == cmp.year);
}
bool operator!=(const Date& cmp) {
return !(this/*this指针*/->operator==(cmp));
}
bool operator<(const Date& cmp) {
if (year < cmp.year) {
return true;
} else if (month < cmp.month) {
return true;
} else if (day < cmp.day) {
return true;
} else {
return false;
}
}
bool operator<=(const Date& cmp) {
if (this->operator==(cmp)) {
return true;
} else {
return this->operator<(cmp);
}
}
void DisPlay() {
cout << month << '/' << day << '/' << year << endl;
}
};
int main() {
Date holiday(12, 25, 2022);
//++and--
holiday.DisPlay();
++holiday;
holiday.DisPlay();
--holiday;
holiday.DisPlay();
//+and-
Date Nextholiday(holiday + 6);
Nextholiday.DisPlay ();
Date Previousholiday (holiday - 19);
Previousholiday.DisPlay();
//+=and-=
holiday.operator -= (19);
holiday.DisPlay();
holiday += 25;
holiday.DisPlay();
//==and!=
Date holiday1(12, 25, 2022);
Date holiday2(12, 31, 2022);
if (holiday1 == holiday2)//只作用于holiday1,将holiday2按引用
cout << "Equality operator: The two are on the same day" << endl;
else
cout << "Equality operator: The two are on different days" << endl;
if (holiday1 != holiday2)
cout << "Inequality operator: The two are on different days" << endl;
else
cout << "Inequality operator: The two are on the same day" << endl;
//<,<=,>and>=
if (holiday1 < holiday2)
cout << "operator<: holiday1 happens first" << endl;
if (holiday1 <= holiday2)
cout << "operator<=: holiday1 happens on or before holiday2" << endl;
/*..........................*/
return 0;
}
另一实例见下:
(5条消息) C++运算符重载详解_Mr_Lsz的博客-CSDN博客_c++运算符重载
创建头文件参考:
(5条消息) 教你如何自己创造一个头文件_屁孩君yeah的博客-CSDN博客_头文件怎么建立
(5条消息) 超级简单的头文件制作_恺哥不懂。的博客-CSDN博客_头文件怎么建立
explicit关键字详见:
(5条消息) C++ explicit关键字详解_tiankong19999的博客-CSDN博客_explicit
双目运算符未完待续。。。。