【C++】【第二篇】【黑马 p106~p126】【类对象的构造、解析、深浅拷贝】【this和const使用】【友元】【运算重载符】

本文接【C++】【第一篇】【黑马 p84 - p105 】C++学习记录

4.2 对象的初始化和清理(p106)

  • 生活中我们买的电子产品基本会有出厂设置,在某一天我们不用的时候也会删除一些自己信息数据保证安全
  • C++中的面对对象来源于生活,每个对象也都会有初始设置以及对象销毁前的清理数据的设置

4.2.1 构造函数和析构函数

  • 对象的初始化和清理也是两个非常重要的安全问题
  1. 一个对象或者变量没有初始状态,对其使用后果是未知
  2. 同样的使用完一个对象或者变量,没有及时清理,也会造成一定的安全问题
  • C++利用了构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作

  • 对象的初始化和清理工作是编译器强制我们要做的事情,因此如果我们不提供构造和析构,编译器会提供编译器提供的构造函数和析构函数是空实现

  • *构造函数:主要作用于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。

    • 析构函数:主要作用于对象销毁前系统自动调用,执行一些清理工作。

  • 析构函数: 类名(){}
  1. 构造函数,没有返回值也不写void
  2. 函数名称与类名相同
  3. 构造函数可以有参数,因此可以发生重载
  4. 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
  • 析构函数语法:~类名(){}
  1. 析构函数,没有返回值也不写void
  2. 函数名称与类名相同,在名称前加上符号
  3. 析构函数不可以有参数,因此不可以发生重载
  4. 程序在对象销毁前会自动调用析构,无须手动调用而且只会调用一次。
#include <iostream>
using namespace std;
#include <cstring>

//对象的初始化和清理

class Person
{
public:
	//1、构造函数
	//没有返回值,不用谢void
	//函数名与类名相同
	//构造函数可以有参数,可以发生重载
	//创建对象的时候,构造函数会自动调用,而且只调用一次
	Person()
	{
		cout << "Preson 构造函数的使用" << endl;
	}
	//2、析构函数 进行清理的操作
	//没有返回值,不写void
	//函数名和类名相同 在名称前加~
	//析构函数不可以有参数的,所以不可以发生重载
	//对象在销毁前,会自动调用析构函数,而且只会调用一次
	~Person()
	{
		cout << "Person 的析构函数的调用" << endl;
	}

};

//构造和析构都是必须有的实现,如果我们自己不提供,编译器会提供一个空实现的构造和析构
void test01()
{
	Person person;//在栈上的数据,test01执行完毕后,就会自动释放
}
int main()
{
	test01();//构造析构都会被调用

	Person p; //但此时只会调用构造函数,也就是其到下一行没有得到释放,
			  //只有main()执行完就会执行析构函数

	system("pause");

	return 0;
}

4.2.2 构造函数的分类及调用(p107)

  • 两种分类方式:
  1. 按参数分为:有参构造和无参构造
  2. 按类型分为:普通构造和拷贝构造
  • 三种调用方式:
  1. 括号法.
  2. 显示法
  3. 隐式转换法
#include <iostream>
using namespace std;
#include <cstring>

//1、构造函数的分类及调用
//分类
// 按照参数分类  无参构造(默认构造) 和 有参构造
// 按照类型分类  普通构造  拷贝构造
class Person
{
public:
	//无参构造函数
	Person()
	{
		cout << "Person的无参构造函数调用" << endl;
	}

	//有参构造函数
	Person(int a)
	{
		age = a;
		cout << "Person的有参构造函数调用" << endl;
	}
	//拷贝构造函数
	Person(const Person &p)
	{
		//将传入的人身上所有的属性,拷贝到我身上
		age = p.age;
		cout << "Person的拷贝构造函数调用" << endl;
	}

	//析构函数
	~Person()
	{
		cout << "Person的析构函数调用" << endl;
	}
	int age;

};

//调用
void test01()
{
/*
	//1、括号法
	Person p1; //默认构造函数的调用
	Person p2(10);//有参构造函数
	//拷贝构造函数
	Person p3(p2);

	//注意事项1
	//调用默认构造函数时候,不要加()//空括号

	//不可Person p4();//会发现并没有创建对象;这是因为编译器会认为这是一个函数的声明
				//不会认为在创建对象

	//cout << "p2的年龄为: " << p2.age << endl;
	//cout << "p3的年龄为: " << p3.age << endl;
*/

/*
	//2、显示法
	Person p1;
	Person p2 = Person(10);//有参构造
	Person p3 = Person(p2);//拷贝构造

	Person(10);//称为匿名对象,特点:当前行执行结束后,系统会立即回收掉匿名对象
			  //注意是当前行;也就是说立即消失

	//注意事项2
	//不要利用拷贝构造函数,初始化匿名对象,编译器会认为Person(p3) == Person p3;
	//Person(p3);//认为是对象的声明
*/

	//3、隐式转换法
	Person p4 = 10; //相当于 写了Person p4 = Person(10);  有参构造
	Person p5 = p4;//拷贝构造;
}

int main()
{
	test01();

	system("pause");

	return 0;
}

4.2.3 拷贝构造函数调用时机

  • C++中拷贝构造函数调用时机通常有三种情况
  1. 使用一个已经创建完毕的对象来初始化一个新对象
  2. 值传递的方式给函数参数传值
  3. 以值方式返回局部对象
#include <iostream>
using namespace std;
#include <cstring>

//C++中拷贝构造函数调用时机通常有三种情况

class Person
{
public:
	Person()
	{
		cout << "person的默认构造函数调用" << endl;
	}
	Person(int age)
	{
		cout << "person的有参构造函数调用" << endl;
		m_Age = age;
	}
	Person(const Person& p)
	{
		cout << "person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;;
	}

	~Person()
	{
		cout << " Person析构函数调用" << endl;
	}

	int m_Age;
};

//1. 使用一个已经创建完毕的对象来初始化一个新对象
void test01()
{
	Person p1(20);//调用有参构造函数
	Person p2(p1);//调用拷贝构造函数

	cout << "p2的年龄为:" << p2.m_Age << endl;
}

//2. 值传递的方式给函数参数传值

void doWork(Person p)
{

}
void test02()
{
	Person p;//调用默认构造函数
	doWork(p);//调用拷贝构造函数
}

//3. 以值方式返回局部对象
Person doWork2()
{
	Person p1_3;//doWork2()函数执行结束后,就会被释放掉
	return p1_3;//但是返回的不是p1_3,而是以值的方式返回
}

void test03()
{
	Person p_3 = doWork2();//会以拷贝构造函数的方式创建p_3
}

int main()
{

	test03();

	system("pause");

	return 0;
}

4.2.4 构造函数调用规则(p109)

  • 默认情况下,C++编译器至少给一个类添加3个函数
  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  • 构造函数调用规则如下:
    • 如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
    • 如果用户定义拷贝构造函数,C++不会再提供其他构造函数
#include <iostream>
using namespace std;
#include <cstring>

// 构造函数的调用规则
//1、创建一个类,C++编译器会给每个类都添加至少3个函数
//默认构造(空实现)
//析构函数(空实现)
//拷贝构造(值拷贝)

//2、如果我们写了有参构造函数,编译器就不会再提供默认构造函数,依然提供拷贝构造
//3、如果我们写了拷贝构造,编译器就不再提供其他普通构造函数了

class Person
{
public:
	//Person() //如果运行test01()就取消该函数注释,运行test02()就注释
	//{
	//	cout << "Person的默认构造函数调用" << endl;
	//}


	//Person(int age)
	//{
	//	cout << "Person的有参构造构造函数调用" << endl;
	//	m_Age = age;
	//}


	Person(const Person& p)//即使我们没有定义,编译器也会定义
	{
		cout << "Person的拷贝构造函数调用" << endl;
		m_Age = p.m_Age;
	}

	~Person()
	{
		cout << "Person的析构函数调用" << endl;
	}
	int m_Age;

};
/*
void test01()// //运行test01()要保证有默认构造
{
	Person p;
	p.m_Age = 18;

	Person p2(p);
	cout << "p2的年龄为:" << endl;
}
*/


//void test02()//运行test02()要保证有有参构造
//{
//	//Person p;//如果自己仅定义了有参构造,那么就不会有默认构造,
//			   //自己没定义,系统也不会定义,会报错,
//	Person p2(10);
//	Person p3(p2);
//}

void test03()//该函数的执行环境为 仅自己定义了拷贝构造
{
	Person p1 = Person(p1);
}
int main()
{

	//test01();
	
	//test02();
	
	test03();
	system("pause");

	return 0;
}

4.2.5 深拷贝与浅拷贝

  • 深浅拷贝是面试经典问题,也是常见的一个坑
  • 浅拷贝:简单的赋值拷贝操作
  • 深拷贝:在堆区重新申请空间,进行拷贝操作
#include <iostream>
using namespace std;
#include <cstring>

//深拷贝与浅拷贝
//浅拷贝带来的问题就是堆区内存的重复释放
//浅拷贝的问题由深拷贝解决
class Person
{
public:
	Person()
	{
		cout << "Person的默认构造函数调用" << endl;
	}
	Person(int age, int height)
	{

		m_Age = age;
		m_Height = new int(height);
		cout << "Person的有参构造函数调用" << endl;
	}

	//自己实现拷贝构造函数,解决浅拷贝带来的问题
	Person(const Person& p)
	{
		cout << "Person拷贝构造函数的调用" << endl;
		//默认拷贝构造是生成下面两行代码
		m_Age = p.m_Age;
		//m_Height = p.m_Height;//编译器默认实现就是这行代码

		//深拷贝操作
		m_Height = new int(*p.m_Height);
	}

	~Person()
	{
		//析构代码,将堆区开辟数据做释放操作
		if (m_Height != NULL)
		{
			delete m_Height;
			m_Height = NULL;
		}
		cout << "Person的析构函数调用" << endl;
	}
	int m_Age;
	int* m_Height;
};

void test01()
{
	Person p1(18,160);

	cout << "p1的年龄为:" << p1.m_Age <<"身高为:" << *p1.m_Height << endl;

	Person p2(p1);
	cout << "p2的年龄为:" << p2.m_Age << "身高为:" << *p2.m_Height << endl;
}


int main()
{
	test01();

	system("pause");

	return 0;
}
  • 总结:如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题。

4.2.6 初始化列表(p111)

  • 作用:C++提供了初始化列表语法,用来初始化属性
  • 语法:构造函数() : 属性1(值1), 属性2(值2) , ..., {}
#include <iostream>
using namespace std;
#include <cstring>

//初始化列表
class Person
{
public:

	//传统初始化操作
	Person(int a, int b, int c)
	{
		m_A = a;
		m_B = b;
		m_C = c;
	}
	Person(int a,int b,int c): m_A(a),m_B(b),m_C(c) {}

	int m_A;
	int m_B;
	int m_C;
};

void test01()
{
	//Person p(10, 20, 30);
	Person p(30,20,10);
	cout << "ABC已赋值" << endl;
}

int main()
{
	test01();

	system("pause");

	return 0;
}

4.2.7 类对象作为类成员(p112)

  • C++类中的成员可以是另一个类的对象,我们称该成员为 对象成员
    例如;
class A {};
class B
{
	A a;
};
  • B类中有对象A作为成员,A作为对象成员
  • 那么当创建B对象时,A与B的构造和析构的顺序是谁先谁后?
    • 是A先构造,最后析构
    • 当类中成员是其他类对象时,我们称该成员为 对象成员
    • 构造的顺序是: 先调用对象成员的构造,再调用本类构造
    • 析构顺序与构造相反

4.2.8 静态成员(p113)

  • 静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员
  • 静态成员分为:
    • 静态成员变量
  1. 所有对象共享一份数据
  2. 在编译阶段分配内存
  3. 类内声明,类外初始化
    • 静态成员函数
  1. 所有对象共享同一个函数
  2. 静态成员函数只能访问静态成员变量
  3. 类外访问不到私有的静态成员函数
    • 静态成员函数访问方式:
  1. 类名的方式访问p.func()
  2. Person::func()的方式访问
#include <iostream>
using namespace std;
#include <cstring>

//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量

class Person
{
public:
	//静态成员函数
	static void func()
	{
		m_A = 100;//静态成员函数可以访问静态成员变量
		//m_B = 200;//报错,因为静态成员函数不可以访问非静态成员变量,无法区分到底是哪个对象的m_B属性
		cout << "staic void func()的调用" << endl;
	}
	static int m_A;//静态成员变量,类内声明,类外初始化
	int m_B; //非静态成员变量

	//静态成员函数也是有访问权限的
private:
	static void func2()
	{
		cout << "static void func2调用" << endl;
	}
};

int Person::m_A = 0;

//有两种访问方式
void test01()
{
	//1、通过对象访问
	Person p;
	p.func();

	//2、通过类名访问
	Person::func();
	//Person::func2();//类外访问不到私有的静态成员函数
}

int main()
{
	test01();

	system("pause");

	return 0;
}

4.3 C++对象模型和this指针

4.3.1 成员变量和成员函数分开储存

  • 在 C++中,类内的成员变量和成员函数分开存储
  • 只有非静态成员变量才属于类的对象上
#include <iostream>
using namespace std;
#include <cstring>

//成员变量 和 成员函数是分开存储的

class Person
{
	int m_A; //非静态成员变量,属于类对象上

	static int m_B;//静态成员变量,不属于类对象上

	void func() {} //非静态成员函数,不属于类的对象上

	static void func() {}//静态成员函数,不属于类的对象上


};

int Person::m_B = 0;

void test01()
{
	Person p;

	//空对象占用内存空间为: 1
	//C++编译器会给每个空对象也分配一个字节空间,是为了区分空对象占内存的位置
	//每个空对象也应该有一个独一无二的内存地址
	cout << "size of p = " << sizeof(p) << endl;
}

void test02()
{
	Person p;
	//仅包含一个int变量,所占用的内存空间为:  4
	cout << "size of p = " << sizeof(p) << endl;
}

int main()
{
	//test01();
	test02();
	system("pause");

	return 0;
}

4.3.2 this指针概念(p115)

  • 通过4.3.1我们知道在C++中成员变量和成员函数是分开存储的
  • 每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用同一块代码
  • 那么问题是:这一块代码是如何区分那个对象调用自己的呢?

  • C++通过提供特殊的对象指针,this为指针,解决上述问题,this指针指向被调用的成员函数所属的对象

  • this指针是隐含每一个非静态成员函数的一种指针
  • this指针不需要定义,直接使用即可

  • this指针的用途:
  1. 当形参和成员变量同名时,可用this指针来区分
  2. 在类的非静态成员函数中返回对象本身,可使用return *this,并且接受该返回的对象,应该是对象引用
#include <iostream>
using namespace std;
#include <cstring>

class Person
{
public:
	Person(int age)
	{
		//m_age = age;
		this->age = age;
	}

	Person& PersonAddAge(Person &p)//返回的是本体,所以以引用的方式返回,
						//如果去掉了引用,每次返回都是使用拷贝构造重新构造一个新的对象,就是以值的方式返回对象
	{
		this->age += p.age;

		//this指向p2的指针,而*this指向的就是p2这个对象本体
		return *this;
	}

	//int m_age;
	int age;
};



void test01()
{
	Person p1(18);
	//cout << "p1的年龄为: " <<p1.m_age<<  endl;
	cout << "p1的年龄为: " << p1.age << endl;
}

void test02()
{
	Person p1(10);

	Person p2(10);

	//链式编程思想
	//如果去掉了返回类型的引用,虽然最后该值为40,但是p2的age还是20
	cout << p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1).age << endl;

	cout << "p2的年龄为: " << p2.age << endl;
}

int main()
{
	//1、解决名称冲突
	//起不同的名字
	//使用this指针
	//test01();

	//2、返回对象本身用*this
	test02();

	system("pause");

	return 0;
}

4.3.3 空指针访问成员函数(p116)

  • C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
  • 如果用到this指针,需要加以判断保证代码的健壮性
#include <iostream>
using namespace std;
#include <cstring>

//空指针调用成员函数
class Person
{
public:
	void showClassName()
	{
		cout << "this is person class" << endl;
	}

	void showPersonAge()
	{
		//这个判断可以有效避免空指针对this造成的报错
		if (this == NULL)
			return;
		//报错的原因是传入的指针为NULL
		//如果定义指针是空指针,那么this指向不确切
		cout << "age = " << m_Age << endl;//m_Age 其实都默认的是 this->m_Age
	}

	int m_Age;
};

void test01()
{
	Person* p = NULL;

	p->showClassName();

	p->showPersonAge();
}

int main()
{
	test01();
	system("pause");

	return 0;
}

4.3.4 const修饰成员函数(p117)

  • 常函数:
  1. 成员函数后加const后我们称为这个函数为常函数
  2. 常函数内不可以修改成员属性
  3. 成员属性声明时加关键字mutable后,在常函数中依然可以修改
  • 常对象
  1. 声明对象前加const称该对象为常对象
  2. 常对象只能调用常函数,修改加了mutable的变量
#include <iostream>
using namespace std;
#include <cstring>

//常函数
class Person
{
public:

	//this指针的本质是 指针常量   指针的指向是不可以修改的
	//如果后面不加const   ,就相当于   Person * const this; 指针常量
	//如果也不想让指针的值可以修改  就是  const Person * const this;  所以要再加一个conts,就加在了函数后面
	//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改
	void showPerson() const
	{
		this->m_B = 100;//加了mutable,所以可以修改其值
		//有const就不可以修改
		//m_A = 100;//也可以看作为//this->m_A = 100;

		//this = NULL; // this指针不可以修改指针的指向的

	}

	void func() { m_A = 100; }

	int m_A;
	mutable int m_B;//特殊变量,即使在常函数中也可以修改
};


//常对象

void test02()
{
	const Person p;//在对象前加const,变为常对象
	//p.m_A = 100; //常对象的值不可以修改,只能调用常函数
	p.m_B = 100;//m_B是特殊值,在常对象下可以修改
	p.showPerson();//常函数   所以可以被常对象调用
	//p.func();//其不是常函数,不能被常对象调用

}


int main()
{

	system("pause");

	return 0;
}

4.4 友元

  • 在程序里,有些私有属性,也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术
  • 友元的目的就是让一个函数或者类 访问另一个类中私有成员
  • 友元的关键字为friend
  • 友元的三种实现
  1. 全局函数做友元
  2. 类做友元
  3. 成员函数做友元

4.4.1 全局函数做友元

#include <iostream>
using namespace std;
#include <cstring>

//建筑物类
class Building
{
	//goodGay全局函数是Building好朋友,可以访问Building私有成员
	friend void goodGay(Building* building);

public:
	Building()
	{
		m_BedRoom = "卧室";
		m_SittingRoom = "客厅";
	}


	string m_SittingRoom; //客厅

private:
		string m_BedRoom; // 卧室
};

void goodGay(Building *building)
{
	cout << "好基友全局函数 正在访问: " << building->m_SittingRoom << endl;
	
	cout << "好基友全局函数 正在访问: " << building->m_BedRoom << endl;//如果不是友元不可以访问
}

int main()
{
	Building building;
	goodGay(&building);

	system("pause");

	return 0;
}

4.4.2 类做友元(p119)

#include <iostream>
using namespace std;
#include <cstring>

//类做友元

class Building;

class GoodGay
{
public:

	GoodGay();

	void visit(); //参观函数 访问Building中的属性

	Building* building;

};

class Building
{
	//GoodGay类可以访问本类中的私有成员
	friend class GoodGay;

public:

	Building();

	string m_SittingRoom;

private:
	string m_BedRoom;
};

//类外写成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
	//创建建筑物对象
	building = new Building;
}

void GoodGay::visit()
{
	cout << "好基友类正在访问: " << building->m_SittingRoom << endl;

	cout << "好基友类正在访问: " << building->m_BedRoom<< endl;
}

void test01()
{
	GoodGay gg;
	gg.visit();
}

int main()
{

	test01();

	system("pause");

	return 0;
}

4.4.3 成员函数做友元

  • 首先加关键字friend,
  • 然后还有明确该函数所属
  • friend void GoodGay::visit();
#include <iostream>
using namespace std;
#include <cstring>

//类做友元

class Building;

class GoodGay
{
public:

	GoodGay();

	void visit(); //让visit函数可以 访问Building中的属性
	void visit2();//让visit2函数不可以访问Building中的属性

	Building* building;
};

class Building
{
	//告诉编译器GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员
	friend void GoodGay::visit();
public:

	Building();

	string m_SittingRoom;

private:
	string m_BedRoom;
};

//类外写成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
	//创建建筑物对象
	building = new Building;
}

void GoodGay::visit()
{
	cout << "好基友类正在访问: " << building->m_SittingRoom << endl;

	cout << "好基友类正在访问: " << building->m_BedRoom<< endl;
}

void GoodGay::visit2()
{
	cout << "好基友类正在访问: " << building->m_SittingRoom << endl;

	//不可以访问
	//cout << "好基友类正在访问: " << building->m_BedRoom << endl;
}

void test01()
{
	GoodGay gg;
	gg.visit();
	gg.visit2();
}

int main()
{

	test01();

	system("pause");

	return 0;
}

4.5 运算符重载

  • 运算符重载概念:对已有的运算符重载重新进行定义,赋予其另一种功能,以适应不同的数据类型

4.5.1 加号运算符重载

  • 作用:实现两个自定义数据类型相加的运算
  • 总结1:对于内置的数据类型的表达式的运算符不可能改变的
  • 总结2:不要滥用运算符重载
  • 可以成员运算符重载
  • 可以全局运算符重载
  • 运算符重载也可以函数重载
#include <iostream>
using namespace std;
#include <cstring>

//加号运算符重载

class Person
{
public:

//1、成员函数重载+号
	//Person operator+ (Person &p)
	//{
	//	Person temp;
	//	temp.m_A = this->m_A + p.m_A;
	//	temp.m_B = this->m_B + p.m_B;
	//	return temp;
	//}

	int m_A;
	int m_B;
};

//2、全局函数重载+号

Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}

Person operator+(Person& p1, int a)
{
	Person temp;
	temp.m_A = p1.m_A + a;
	temp.m_B = p1.m_B + a;
	return temp;
}

void test01()
{
	Person p1;
	p1.m_B = 10;
	p1.m_A = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;

	//成员函数本质的调用
	//Person p3 = p1.operator+(p2);

	//全局函数本质的调用
	//Person p3 = operator+(p1, p2);

	Person p3 = p1 + p2;
	cout << "p3.m_A = " << p3.m_A << endl;
	//运算符重载 也可以发生函数重载
	Person p4 = p1 + 100; //Person + int
	cout << "p4.m_A = " << p4.m_A << endl;
}

int main()
{

	test01();

	system("pause");

	return 0;
}

4.5.2 左移运算符重载(p122)

  • 可以输出自定义数据类型
#include <iostream>
using namespace std;
#include <cstring>

//左移运算符重载

class Person
{

	friend ostream& operator<< (ostream& out, Person& p);
public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}

private:

	//利用成员函数重载 左移运算符
	//通常不会利用成员函数重载<<运算符,无法实现cout在左侧
	//void operator<< (Person &p)

	int m_A;
	int m_B;
};
//链式编程思想
//只能利用全局函数重载 左移运算符
ostream& operator<< (ostream& out, Person &p) //本质 operator<< (cout,p) 简化 cout << p
{
	out << "m_A = " << p.m_A << " m_B = " << p.m_B << endl;
	return out;
}

void test01()
{
	Person p(10,10);

	cout << p << endl;

}

int main()
{

	test01();

	system("pause");

	return 0;
}
  • 总结:重载左移运算符配合友元可以实现输出自定义数据类型

4.5.3 递增运算符重载(p123)

  • 作用:通过重载递增运算符,实现自己的整形数据
#include <iostream>
using namespace std;
#include <cstring>

//重载递增运算符

//自定义整形
class MyInteger
{

	friend ostream& operator<< (ostream& cout, MyInteger myint);
public:
	MyInteger()
	{
		m_Num = 0;
	}
	//重载前置++运算符
	MyInteger& operator++ ()//链式编程思想,所以返回引用
	{
		//先进行++运算
		m_Num++;
		//再将自身做一个返回
		return *this;
	}

	//重载后置++运算符
	//void operator(int) int 代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++ (int)
	{
		//先 记录当时结果
		MyInteger temp = *this;
			//后 递增
		m_Num++;
		//最后 将记录的结果做返回
		return temp;
	}

private:
	int m_Num;
};

//重载左移运算符
ostream& operator<< (ostream& cout, MyInteger myint)
{
	cout << myint.m_Num;
	return cout;
}



void test01()
{
	MyInteger myint;
	cout << ++(++myint) << endl;
}

void test02()
{
	MyInteger myint;
	cout << (myint++)++ << endl;
	cout << myint << endl;
}

int main()
{

	test01();
	test02();

	system("pause");

	return 0;
}

4.5.4 赋值运算符重载(p124)

  • C++编译器至少给一个类添加4个函数
  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符 operator= ,对属性进行值拷贝

  • 如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
#include <iostream>
using namespace std;
#include <cstring>

//赋值运算符重载
class Person
{
public:
	Person(int age)
	{
		m_Age = new int(age);
	}

	~Person()
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}

	Person& operator=(Person& p)
	{
		//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		this->m_Age = new int(*p.m_Age);
		return *this;
	}


	int *m_Age;
};

void test01()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);

	p3 = p2 = p1;//赋值操作

	cout << "p1的年龄为:" << *p1.m_Age << endl;
	cout << "p2的年龄为:" << *p2.m_Age << endl;
}

int main()
{
	test01();



	system("pause");

	return 0;
}

4.5.5 关系运算符重载(p125)

  • 作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
#include <iostream>
using namespace std;
#include <cstring>

//重载关系运算符
class Person
{
public:
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}

	//重载==号
	bool operator==(Person &p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
			return true;
		else
			return false;
	}

	//重载!=号
	bool operator!=(Person& p)
	{
		if (this->m_Name != p.m_Name || this->m_Age != p.m_Age)
			return true;
		else
			return false;
	}

	string m_Name;
	int m_Age;
};

void test01()
{
	Person p1("Tom", 10);

	Person p2("Tom", 10);

	Person p3("jerry", 20);

	if (p1 == p2)
	{
		cout << "p1和p2是相等的" << endl;
	}
	else
		cout << "p1和p2是不相等的" << endl;

	if(p1!=p3)
	{
		cout << "p1和p3是不相等的" << endl;
	}
}

int main()
{

	test01();
	system("pause");

	return 0;
}

4.5.6 函数调用运算符重载(p126)

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活

  • 介绍了匿名函数对象的使用方法和特性
#include <iostream>
using namespace std;
#include <cstring>

//函数调用运算符重载

//打印输出类
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 << endl;

	//匿名的函数对象
	cout << MyAdd()(100, 100) << endl;//MyAdd()称为匿名对象,当前行执行完之后立即释放
}



int main()
{

	test01();
	test02();

	system("pause");

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值