C++学习:多态与运算符(Day.7~)

 总结让人明白。

表明覆盖意图的限定符 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;
}

将复制构造函数声明为虚函数,感兴趣可以自己去了解下,这里不再提。

问与答

问:为何在基类函数声明中使用关键字 virtual,因为即便不使用该关键字,代码也能通过编译?
答: 如果不使用关键字 virtual ,就不能确保 objBase.Function( ) 执行 Derived::Function( ) 。另外,代码够通过编译并不意味着其质量上乘。
问:基类总应包含一个虚析构函数吗?

答:最好如此。如果编写了如下代码:

Base* pBase = new Derived();
delete pBase;
则仅当析构函数 Base( ) 被声明为虚函数时, delete pBase 才会调用析构函数 Derived( )
抽象基类(ABC)都不能被实例化,它有何用途呢?
:ABC 并非为实例化而创建的,而仅充当基类。它包含纯虚函数,指定了派生类必须实现哪些
函数,可充当接口。
问:在继承层次结构中,需要在所有虚函数声明中都使用关键字 virtual,还是只需在基类中这
样做?
答: 只需在基类的函数声明中使用关键字 virtual 即可。
问:在 ABC 中,可定义成员函数和成员属性吗?
答: 当然可以。这样的 ABC 也不能被实例化,因为它至少包含一个纯虚函数,派生类必须实现该
函数。 (回答了我再上一章中的问题)

问:假如需要实现圆和三角,要求二者都必须包含Area()和Print()函数,何如?

答:

声明抽象基类 Shape ,并在其中将 Area( ) Print( ) 声明为纯虚函数,从而要求 Circle Triangle
必须实现这些函数。

运算符类型与运算符重载

 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

双目运算符未完待续。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值