C++面向对象三大特性之封装

目录

1.1封装

1.1.1封装的意义

1.1.2 struct和class区别

1.1.3成员属性设置为私有

1.2对象的初始化和清理

1.2.1构造函数和析构函数

 1.2.2构造函数的分类及调用

1.2.3拷贝函数调用时机

 1.2.4 构造函数调用规则

1.2.5深拷贝和浅拷贝

1.2.6 初始化列表

 1.2.7类对象作为类成员

1.2.8静态成员

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

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

1.3.2this指针概念

1.3.3空指针访问成员函数

1.3.4 const修饰成员函数

 1.4.友元

1.4.1全局函数做友元

1.4.2类做友元

 1.4.3成员函数做友元

 1.5运算符重载

1.5.1加号运算符重载

 1.5.2左移运算符重载

 1.5.3递增运算符重载

1.5.4赋值运算符重载

 1.5.5关系运算符重载

1.5.6 函数调用运算符重载


1.1封装

1.1.1封装的意义

一、将属性和行为作为一个整体,表现生活中的事物
语法:class 类名{访问权限:属性/行为}
设计圆类,求周长

class circle {
public:
	const int PI = 3.14;
	int m_r;
	double calculateZC() {
		return 2 * PI * m_r;
	}
};

int main() {
	circle c1;
	c1.m_r = 10;
	cout << "圆的周长为:" << c1.calculateZC() << endl;

	return 0;
}

 设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号

class Student {
public:
	string m_name;
	int m_id;
public:
	void defname(string name) {
		m_name = name;
	}
	void setID(int id) {
		m_id = id;
	}
	void showstudent() {
		cout << "name ==" << m_name << "ID:" << m_id << endl;
	}
};

int main() {
	Student stu;
	stu.defname("chick");
	stu.setID(11111);
	stu.showstudent();
}

二、类在设计的时候,可以把属性和行为放在不同的权限下,加以控制
访问权限有三种:public,protected,private

class Person {
public:
	string m_Name;
protected:
	string m_Car;
private:
	int m_Password;
public:
	void func() {
		m_Name = "nihao1";
		m_Car = "car";
		m_Password = 123457;
	}
};

int main() {
	Person p;
	p.m_Name = "nihao1";
	/*p.m_Car
	p.m_Password*///保护权限和私有权限类外无法访问
	return 0;
}

1.1.2 struct和class区别

唯一区别在于默认的访问权限不同:struct默认权限为公共,class默认权限为私有

class C1 {
	int m_A;
};
struct C2 {
	int m_A;
};
int main() {
	C1 c1;
	c1.m_A = 10;//错误,class默认访问权限为私有

	C2 c2;
	c2.m_A = 100;
	return 0;
}

1.1.3成员属性设置为私有

优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,我们可以检测数据有效性

 

class Person {
public:
	void setName(string name) {
		m_name = name;
	}
	string getname() {
		return m_name;
	}

	int getAge() {
		return m_Age;
	}
	void setAge(int age) {
		if (age < 0 || age>150) {
			cout << "false" << endl;
			return;
		}
		m_Age = age;
	}

	void setLover(string lover) {
		m_Lover = lover;
	}

private:
	string m_name;
	int m_Age;
	string m_Lover;
};

int main() {
	Person p;
	p.setName("name");
	cout << "name == " << p.getname() << endl;

	p.setAge(50);
	cout << "age == " << p.getAge() << endl;

	p.setLover("hhua1");//只是写属性的话,是不可以被读取的

	return 0;
}

1.2对象的初始化和清理

C++中面向对象来源于生活,每个对象都会有初始设置以及对象销毁前的清理数据的设置

1.2.1构造函数和析构函数

C++利用这两个函数完成对象的初始化和清理,如果我们不提供构造和析构,编译器会提供这两个的空实现
构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用
析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作

构造函数语法:类名(){}
1.构造函数,没有返回值也不写void
2.函数名称与类型相同
3.构造函数可以有参数,因此可以发生重载
4.程序在调用对象时候会自动调用构造,无需手动调用,而且只会调用一次


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

class Person {
public:
	Person() {
		cout << "this is gouzao function diaoyong" << endl;
	}

	~Person() {
		cout << "this is xigou function diaoyong" << endl;
	}
};

//C++利用这两个函数完成对象的初始化和清理,如果我们不提供构造和析构,编译器会提供这两个的空实现
void test01() {
	Person p;//在栈上的数据,test01执行完毕后,释放这个对象
}
int main() {
	test01();
	return 0;
}

 1.2.2构造函数的分类及调用

两种分类方式:
按参数分为:有参和无参
按类型分为:普通构造和拷贝构造
三种调用方式:
括号法、显示法、隐式转换法

1.构造函数分类
按照参数分类分为有参和无参构造,无参又被称为默认构造函数
按照类型分为普通构造和拷贝构造

class Person {
public:
	//无参构造函数
	Person() {
		cout << "wucanshu" << endl;
	}
	//有参
	Person(int a) {
		age = a;
		cout << "youcanshu" << endl;
	}
	//拷贝构造
	Person(const Person& p) {
		age = p.age;
		cout << "kaobei gouzao hanshu" << endl;
	}
	//析构函数
	~Person() {
		cout << "xigou hanshu" << endl;
	}
public:
	int age;
};

2.构造函数的调用
调用无参构造函数

void test01() {
	Person p;//注意:调用无参构造函数,不能加括号,加了编译器会认为这是一个函数声明
}
void test02() {
	//2.1括号法-常用
	Person p1(10);


	//2.2显式法
	Person p2 = Person(10);
	Person p3 = Person(p2);

	//2.3隐式转换法
	Person p4 = 10;
	Person p5 = p4;

	//注意2:不能利用拷贝构造函数,初始化匿名对象,编译器认为是对象声明
}

int main() {
	test01();
	test02();

	return 0;
}

1.2.3拷贝函数调用时机

C++中拷贝构造函数调用实际通常由三种情况
1.使用一个已经创建完毕的对象来初始化一个新对象
2.值传递的方式给函数参数传值
3.以值方式返回局部对象

 

class Person {
public:
	Person() {
		cout << "wucan gouzao function" << endl;
		mAge = 0;
	}
	Person(int age) {
		cout << "youcan gouzao function" << endl;
		mAge = age;
	}
	Person(const Person& p) {
		cout << "拷贝构造函数" << endl;
		mAge = p.mAge;
	}
	//析构函数在释放内存之前调用--最后输出
	~Person() {
		cout << "xigou function" << endl;
	}

public:
	int mAge;
};

//1.使用一个已经创建完毕的对象来初始化一个新对象
void test01() {
	Person man(100);//p对象已经创建完毕
	Person newman(man);//调用拷贝构造函数
	Person newman2 = man;//拷贝构造

	//特殊情况
	//Person newman3;
	//newman3 = man;//不是调用拷贝构造函数,赋值操作
}


//2.值传递的方式给函数参数传值
//相当于Person p1=p;
void dowork(Person p1) {}
void test02() {
	Person p;//无参构造函数
	dowork(p);
}

//3.以值方式返回局部对象
Person dowork2() {
	Person p1;//无参构造函数
	cout << (int*)&p1 << endl;//000000BDBBAFF8F4
	//这里的操作实际上是将person对象先获取p1的地址(&)先转化为void*类型,然后再转换为int*类型
	//主要原因是cout不知如何打印Person*类型的指针,但是它可以打印int*类型的指针
	//直接将对象转化为指针不合法
	return p1;
}

void test03() {
	Person p = dowork2();
	cout << (int*)&p << endl;//000000BDBBAFF8F4
}

int main() {
	test01();
	test02();
	test03();

	return 0;
}

 1.2.4 构造函数调用规则


默认情况下,C++编译器至少给一个类添加三个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行赋值

调用规则如下:
1.如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝函数
2.如果用户定义拷贝构造函数,C++不会再提供其他构造函数

 

class Person {
public:
	Person() {
		cout << "wucan gouzao hanshu" << endl;
	}
	Person(int a) {
		age = a;
		cout << "youcan gouzao function" << endl;
	}
	Person(const Person& p) {
		age = p.age;
		cout << "kaobei gouzao hanshu" << endl;
	}

	~Person() {
		cout << "xigou function" << endl;
	}
public:
	int age;
};

void test01() {
	Person p1(18);
	Person p2(p1);
	cout << "P2的年龄为:" << p2.age << endl;
}
 
void test02() {
	Person p1;
	Person p2(10);
	Person p3(p2);

	Person p4;
	Person p5(10);
	Person p6(p5);
}

int main() {
	test01();
	test02();
	return 0;
}

1.2.5深拷贝和浅拷贝


浅拷贝:简单的赋值拷贝操作(比如拷贝构造函数中编译器的操作)
深拷贝:在堆区重新申请空间,进行拷贝操作
如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题

class Person {
public:
	Person() {
		cout << "wucan gouzao function" << endl;
	}

	Person(int age, int height) {
		cout << "youcan gouzao function" << endl;

		m_age = age;
		//m_height现在指向一个在堆上分配的int值,该值被初始化为height,指针
		m_height = new int(height);//手动开辟堆区数据,
	}

	Person(const Person& p) {
		cout << "kaobei gouzao function" << endl;
		//如果不利用深拷贝在堆区创建系内存,会导致浅拷贝带来的重复释放堆区问题
		m_age = p.m_age;
		m_height = new int(*p.m_height);
	}

	~Person() {
		cout << "xigou function" << endl;
		//手动释放堆区数据
		if (m_height != NULL) {
			delete m_height;
		}
	}
public:
	int m_age;
	int* m_height;
};

void test01() {
	Person p1(18, 180);
	Person p2(p1);
	cout << "p1age =" << p1.m_age << "height = " << *p1.m_height << endl;
	cout << "p2age = " << p2.m_age << "height = " << *p2.m_height << endl;
}

int main() {
	test01();
	return 0;
}

1.2.6 初始化列表


作用:C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(值1),属性2(值2),...{}

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) {}
	void PrintPerson() {
		cout << "mA:" << m_A << endl;
		cout << "mB:" << m_B << endl;
		cout << "mC:" << m_C << endl;
	}
private:
	int m_A;
	int m_B;
	int m_C;
};
int main() {
	Person p(1, 2, 3);
	p.PrintPerson();

	return 0;
}

 1.2.7类对象作为类成员


C++类中的成员可以是另一个类的对象,我们称该成员为对象成员

class Phone {
public:
	Phone(string name) {
		m_PhoneName = name;
		cout << "Phone gouzao" << endl;
	}

	~Phone() {
		cout << "Phone xigou" << endl;
	}
public:
	string m_PhoneName;
};

class Person {
public:

	Person(string name, string pName) :m_Name(name), m_Phone(pName) {
		cout << "Person gouzao" << endl;
	}
	~Person() {
		cout << "Person xigou" << endl;
	}
	void playgame() {
		cout << m_Name << "使用" << m_Phone.m_PhoneName << "phone" << endl;
	}
public:
	string m_Name;
	Phone m_Phone;
};

void test01() {
	//当类中成员是其他类对象时,我们称该对象成员为  对象成员
	//构造的顺序是,对象成员->本类构造
	//析构顺序为:本类构造->对象成员

	Person p("zhan san", "apple x");
	p.playgame();
}

int main() {
	test01();
	return 0;
}

1.2.8静态成员


静态成员就是在成员变量和成员函数之前加上关键字static,称为静态成员
两类:
1.静态成员变量
所有对象共享同一份数据
在编译阶段分配内存
类内声明,类外初始化
2.静态成员函数
所有对象共享同一个函数
静态成员函数只能访问静态成员变量

1.静态成员变量

class Person {
public:
	static int m_A;//静态成员变量

	//静态成员变量特点
	//1.在编译阶段分配内存
	//2.类内声明,类外初始化
	//3.所有对象共享同一份数据

private:
	static int m_B;//亦有访问权限
};
int Person::m_A = 10;
int Person::m_B = 10;

void test01() {
	//静态成员变量两种访问方式

	//1.通过对象
	Person p1;
	p1.m_A = 100;
	cout << "p1.m_A ==" << p1.m_A << endl;

	Person p2;
	p2.m_A = 200;
	cout << "p1.m_A ==" << p1.m_A << endl;//共享同一份数据
	cout << "p2.m_A ==" << p2.m_A << endl;

	//2.通过类名
	cout << "m_A = " << Person::m_A << endl;
	//cout << "m_B = " << Person::m_B << endl;私有的,不可访问
}
int main() {
	test01();
	return 0;
}

2.静态成员函数

class Person {
public:
	
	//静态成员函数特点:
	//1.程序共享一个函数
	//2.静态成员函数只能访问静态成员变量

	static void func() {
		cout << "func diaoyong" << endl;
		m_A = 100;
		//m_B = 1000; //错误,不可以访问非静态成员变量
	}

	static int m_A;//静态成员函数变量
	int m_B;

private:
	//静态成员函数也是有访问权限的
	static void func2() {
		cout << "func2 diaoyong" << endl;
	}
};
int Person::m_A = 10;

void test01() {
	//静态成员变量两种访问方式

	//1.通过对象
	Person p1;
	p1.func();

	//2.通过类名
	Person::func();

	//Person::func2();//私有状态不可访问
}

int main() {
	test01();
	return 0;
}

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

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

在C++中,类内的成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上

class Person {
//public:
//	Person() {
//		mA = 0;
//	}
//	//非静态成员变量占对象空间
	int mA;
//	//静态成员变量不占对象空间
//	static int mB;
//
//	//函数也不占对象空间,所有函数共享一个函数实例
//	void func() {
//		cout << "mA:" << this->mA << endl;
//	}
//	//静态成员函数也不占对象空间
//	static void sfunc() {
//
//	}
};

void test01() {
	Person p;
	//空对象占用内存空间为:1
	//C++编译器会给每个对象也分配一个字节,是为了区分空对象占内存的位置
	//每个空对象也应该有一个独一无二的内存地址

	//对象不空的时候,占4个字节,也就是int类型
	cout << "sizeof(p) == " << sizeof(p) << endl;
}
int main() {
	test01();
	return 0;
}

1.3.2this指针概念


上面知道了C++中成员变量和成员函数是分开存储的
每一个静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会公用一块代码
C++提供特殊的对象指针,this指针,解决“这一块代码是如何区分哪个对象调用自己”的问题
this指针指向被调用的成员函数所属的对象

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

this指针用途
当形参和成员变量同名时,可以用this指针来区分
在类的非静态成员函数中返回对象本身,可以使用return *this

 

class Person {
public:

	Person(int age) {
		//1.当形参和成员变量同名时(名称冲突),可用this指针区分
		//this指针指向 被调用的成员函数 所属的对象
		this->age = age;
	}
	//引用方式返回不会创建新的对象,每次都是原来的那个
	Person& PersonAddPerson(Person p) {
		this->age += p.age;
		//返回对象本身
		//this是指向p2的指针,而*this指向的就是p2这个对象的本体(解引用)
		return *this;
	}
	int age;
};

void test01() {
	Person p1(10);
	cout << "p1.age = " << p1.age << endl;

	Person p2(10);
	//链式编程思想
	p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
	cout << "p2.age = " << p2.age << endl;
}

int main() {
	test01();
	return 0;
}

 

1.3.3空指针访问成员函数


C++中空指针也是可以调用成员函数的,,但是也要注意有没有用到this指针
若用到this指针,需要加以判断保证代码健壮性

空指针访问成员函数

class Person {
public:
	void showClassName() {
		cout << "I am Person class" << endl;
	}

	// this指针是隐含每一个非静态成员函数内的一种指针
	void ShowPerson() {
		if (this == NULL) {//不加这个判断会导致程序异常
			return;
		}
		//报错原因是因为传入的指针是为NULL
		cout << mAge << endl;//默认为this->mAge;
	}
public:
	int mAge;

};

void test01() {
	Person* p = NULL;
	p->showClassName();
	p->ShowPerson();
}

int main() {
	test01();
	return 0;
}

1.3.4 const修饰成员函数


常函数:
1.成员函数后加const后,我们称为这个函数为常函数
2.常函数内不可以修改成员属性
3.成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
1.声明对象前加const称该对象为常对象
2.常对象只能调用常函数

 

class Person {
public:
	Person() {
		m_A = 0;
		m_B = 0;
	}
	//this指针的本质,是指针常量,指针的指向是不可以修改的
	//this可以理解为:Person * const this;
	void ShowPerson() const {//函数后加const,等效于:“const Person * const this”
		this->m_B = 100;//this指向的值可以修改,但是如果是“const Person * const this”那么值都不可以修改
		//this->NULL;this指针不可以修改指针指向。
	}

	void MyFunc() {
		mA = 10000;
	}
public:
	int mA;
	int m_A;
	mutable int m_B;//加了之后依然可以修改
};

void test01() {
	const Person person;//对象前加const,变为常对象
	cout << person.m_A << endl;

	person.m_B = 100;//m_B是特殊值,在常对象下也可以修改

	//常对象只能调用常函数
	//person.MyFunc();,错误,MyFunc不是常函数
	person.ShowPerson();
}
int main() {
	test01();
	return 0;
}

 1.4.友元


目的:让一个函数或者类访问其中另一个类中的私有成员
关键字:friend
三种实现:全局函数做友元;类做友元;成员函数做友元

1.4.1全局函数做友元

class Building {
	//告知编译器是goodfriend全局函数,是Building类的好朋友,可以访问类中的私有内容
	friend void goodfriend(Building* building);
public:
	Building() {
		this->m_SittingRoom = "客厅";
		this->m_BedRoom = "卧室";
		this->m_Bed = "床";
	}
public:
	string m_SittingRoom;
private:
	string m_BedRoom;
protected:
	string m_Bed;
};

void goodfriend(Building* building) {
	cout << "好朋友正在访问" << building->m_SittingRoom << endl;
	cout << "好朋友正在访问" << building->m_BedRoom << endl;
	cout << "好朋友正在访问" << building->m_Bed << endl;
}

void test01() {
	Building b;
	goodfriend(&b);
}

int main() {
	test01();
	return 0;
}

1.4.2类做友元

class Building;
class goodfriend {
public:
	goodfriend();
	void visit();
private:
	Building* building;//内部维护一个指针
};

class Building {
	//告知编译器是goodfriend全局函数,是Building类的好朋友,可以访问类中的私有内容
	friend class goodfriend;
public:
	Building();
public:
	string m_SittingRoom;
private:
	string m_BedRoom;
};

//"::"用于指定一个名称所属的类或者命名空间
//比如:定义构造函数时,用来指定这个构造函数是属于Building类的

//类外写成员函数
//实例化对象
Building::Building() {
	this->m_BedRoom = "客厅";
	this->m_BedRoom = "卧室";
}

goodfriend::goodfriend() {
	//创建建筑物对象
	//并且内部指针维护对象
	building = new Building;//创建一个指向堆区的内存
}

void goodfriend::visit() {
	cout << "好基友正在访问" << building->m_SittingRoom << endl;
	cout << "好基友正在访问" << building->m_BedRoom << endl;
}
void test01() {
	goodfriend g;
	g.visit();
}
int main() {
	test01();
	return 0;
}

 1.4.3成员函数做友元

class Building;
class goodGay {
public:
	goodGay();
	void visit();
	void visit1();
private:
	Building* building;//内部维护一个指针
};

class Building {
	friend void goodGay::visit();

public:
	Building();
public:
	string m_SittingRoom;
private:
	string m_BedRoom;
};

Building::Building() {
	this->m_SittingRoom = "客厅";
	this->m_BedRoom = "卧室";
}
goodGay::goodGay() {
	building = new Building;
}
void goodGay::visit() {
	cout << "好基友正在访问" << building->m_SittingRoom << endl;
	cout << "好基友正在访问" << building->m_BedRoom << endl;
}
void goodGay::visit1() {
	cout << "好基友正在访问" << building->m_SittingRoom << endl;
	//cout << "好基友正在访问" << building->m_BedRoom << endl;访问不到,因为该成员函数不是友元
}

void test01() {
	goodGay g;
	g.visit();
}
int main() {
	test01();
	return 0;
}

 1.5运算符重载


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


1.5.1加号运算符重载


作用:实现两个自定义数据类型相加的运算

class Person {
public:
	Person() {};
	Person(int a, int b) {
		this->m_A = a;
		this->m_B = b;
	}
	//方法1:成员函数实现+号运算符重载

	//编译器给出通用名称
	Person operator+(const Person& p) {
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}
public:
	int m_A;
	int m_B;
};
//方法2,全局函数重载+号
Person operator+(const Person& p1, const Person& p2) {
	Person temp;
	temp.m_A = p1.m_A+p2.m_A;
	temp.m_B = p1.m_B+p2.m_B;
	return temp;
}
//3.运算符重载,可以发生函数重载
//Person operator+(const Person& p2, int val) {
//	Person temp;
//	temp.m_A = p2.m_A + val;
//	temp.m_B = p2.m_A + val;
//	return temp;
//}
void test01() {
	Person p1(10, 10);
	Person p2(20, 20);
	//实现:两个Person类型各自相加
	//成员函数方式
	//使用编译器提供的operator形式,简化了相加的过程(不然就是:Person p3 = p1.operator+(p2))
	Person p3 = p2 + p1;
	cout << "mA:" << p3.m_A << "mB:" << p3.m_B << endl;

	/*Person p4 = p3 + 10;
	cout << "mA:" << p4.m_A << "mB:" << p4.m_B << endl;*/
}

int main() {
	test01();
	return 0;
}
//对于内置的数据类型的表达式的运算符是不可能改变的
//不要滥用运算符重载

 1.5.2左移运算符重载


作用:可以输出自定义数据类型

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

//ostream是标准库中的一个类,用于表示输出流
//这个类是所有输出流类的基类。

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p) {
	out << "a:" << p.m_A << "b:" << p.m_B;
	return out;
}
void test() {
	Person p1(10, 20);
	cout << p1 << "hello" << endl;
}
int main() {
	test();
	return 0;
}
总结:重载左移运算符配合友元可以实现输出自定义数据类型

 1.5.3递增运算符重载


作用:通过重载递增运算符,实现自己的整型数据

class MyInteger {
	friend ostream& operator<<(ostream& out, MyInteger myint);
public:
	MyInteger() {
		m_Num = 0;
	}
	//前置++
	MyInteger& operator++() {
		//先++
		m_Num++;
		//再返回
		return *this;
	}
	//后置++
	MyInteger operator++(int) {
		//先返回
		MyInteger temp = *this;//记录当前本身的值,然后让本身的值+1,但是返回的是以前的值,达到先返回后++
		m_Num++;
		return temp;
	}
private:
	int m_Num;
};

ostream& operator<<(ostream& out, MyInteger myint) {
	out << myint.m_Num;
	return out;
}

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

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

int main() {
	test01();
	test02();
	return 0;
}
//前置递增返回引用,后置递增返回值

1.5.4赋值运算符重载


//c++编译器至少给一个类添加4个函数
//1. 默认构造函数(无参,函数体为空)
//2. 默认析构函数(无参,函数体为空)
//3. 默认拷贝构造函数,对属性进行值拷贝
//4. 赋值运算符 operator=, 对属性进行值拷贝
//如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝的问题

 

class Person {
public:
	Person(int age) {
		m_Age = new int(age);
	}
	Person& operator= (Person & p){
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}

	m_Age = new int(*p.m_Age);
	return *this;
	}
	~Person() {
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
	}
	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;
	cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
	test01();
	return 0;
}

 1.5.5关系运算符重载

class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->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 false;
		}
		else {
			return true;
		}
	}
	string m_Name;
	int m_Age;
};
void test01() {
	Person a("name", 18);
	Person b("name", 18);

	if (a == b) {
		cout << "a与b相等" << endl;
	}
	else {
		cout << "a与b不相等" << endl;
	}
}

int main() {
	test01();
	return 0;
}

1.5.6 函数调用运算符重载


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

class MyPrint {
public:
	void operator()(string text) {
		cout << text << endl;
	}
};
void test01() {
	重载的()操作符,也称为仿函数
	MyPrint myFunc;
	myFunc("hello hello");
}

class MyAdd {
public:
	int operator() (int v1, int v2) {
		return v1 + v2;
	}
};
void test02() {
	MyAdd add;
	int ret = add(10, 10);
	cout << "ret = " << ret << endl;
}

int main() {
	test01();
	test02();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值