栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++学习 2.25

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++学习 2.25

1、关系运算符重载

用于重载自定义数据类型的==和!=

	//重载==号
	bool operator==(Person& p)
	{
		if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
//重载!=号
	bool operator!=(Person& p)
	{
		if (this->m_Age != p.m_Age || this->m_Name != p.m_Name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
2、函数调用运算符重载

函数调用()也可以重载,称为仿函数,无固定写法,较灵活。

匿名函数对象:创建一个匿名的类对象,可以调用类中函数

以下为两个仿函数的案例: 

#include
using namespace std;
class MyPrint
{
public:
	void operator()(string test)
	{
		cout << test << endl;
	}
};
void MyPrint02(string test)
{
	cout << test << endl;
}
void test01()
{
	MyPrint myprint;
	myprint("hello world");//使用起来非常类似于函数调用,因此称为仿函数
	MyPrint02("hello world");

}
//加法类
class MyAdd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};
void test02()
{
	MyAdd myadd;
	int ret = myadd(100, 100);
	cout << "ret=" << ret << endl;

	//匿名函数对象
	cout << MyAdd()(100, 100) << endl;//完成调用后析构,无名字
}
int main()
{
	test01();
	test02();

	return 0;
}
3、继承的基本语法

目的:减少重复的代码

语法:class 子类:继承方式

父类=基类,子类=派生类

class Java:public basePage
4、继承方式

public、protected、private

子类无论以哪种方式继承父类,始终无法访问父类的private成员

类外只能访问public成员,protected和private都不可访问

5、继承中构造和析构顺序

继承中,先调用父类的构造函数,再调用子类的构造函数,先调用子类的析构函数,再调用父类的析构函数,析构的顺序与构造相反。

6、继承中同名成员处理

子类和父类出现同名成员,默认调用子类成员。通过子类对象访问父类的同名成员属性、函数,只需要加上作用域即可。

如果子类中出现和父类同名的成员函数,子类的同名成员会隐藏掉父类中所有同名成员函数,要想访问父类,需要加作用域。

void test01()
{
    Son s;
    cout<<"Son 下m_A="<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/766894.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号