【C++学习笔记】对象的特性

深拷贝与浅拷贝

浅拷贝:简单的赋值拷贝操作
深拷贝:在堆区重新申请空间,进行拷贝

利用编译器提供的拷贝构造函数,会做浅拷贝操作;会导致堆区的内存重复释放
![[Pasted image 20221216213144.png]]
解决方法:
利用深拷贝进行解决。

#include<iostream>

using namespace std;

class Person
{
public:
	Person()
	{
		cout << "Person的默认构造函数调用!" << endl;
	}
	Person(int a, int height)
	{
		m_Age = a;
		m_Height = new int(height); // 通过new将数据创建在堆区, new的返回值为 指针 即深拷贝 
		// 堆区的数据需要程序员手动释放

		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);
	Person p2(p1); // 相对于浅拷贝
	cout << "p1的年龄:" << p1.m_Age  << "p1的身高:" << *p1.m_Height << endl;
	cout << "p2的年龄:" << p2.m_Age  << "p2的身高:" << *p2.m_Height << endl;
}

int main()
{
	test01();

	return 0;
}

初始化列表

语法:构造函数():属性1(值1),属性2(值2),...{}

#include<iostream>
using namespace std;

// 初始化列表
class Person2 
{
public:
	Person2(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 test02()
{
	Person2 p(10, 20, 30);
	cout << "p.m_A = " << p.m_A << endl;
	cout << "p.m_B = " << p.m_B << endl;
	cout << "p.m_C = " << p.m_C << endl;
}

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

![[Pasted image 20221216215208.png]]

类对象作为类成员

  1. 当其他类对象作为本类成员,构造时先构造其他类对象,在构造自身
  2. 析构函数与之相反
    ![[Pasted image 20221217212136.png]]
#include<iostream>
#include<string>

using namespace std;

class Phone
{
public:
	Phone(string pName) : m_PName(pName)
	{
		cout << "Phone 构造函数调用!" << endl;
	}
	~Phone()
	{
		cout << "Phone 析构函数调用!" << endl;
	}
	string m_PName;
};
// 类对象作为类成员
class Person
{
public:
	Person(string name, string pName) : m_Name(name), m_Phone(pName) 
	{
		cout << "Person 构造函数调用!" << endl;
	}
	~Person()
	{
		cout << "Person 析构函数调用!" << endl;
	}
	// 姓名
	string m_Name;
	// 手机
	Phone m_Phone;
};

void test01()
{
	Person p("张三", "iphpne 13 pro");
	cout << p.m_Name << "有 " << p.m_Phone.m_PName << "手机" << endl;
}


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

静态成员函数

特点:

  1. 静态成员(函数、变量)所有类对象共享同一个静态成员函数或者静态成员变量
  2. 静态成员函数只能访问静态成员变量
    1. 因为静态成员函数无法区分非静态成员变量到底属于哪个对象,而静态成员变量是所有对象共享的因此不存在上述问题
      2.在这里插入图片描述
  3. 静态成员函数有访问权限
    1. 类外访问不到私有静态成员函数
      在这里插入图片描述
#include<iostream>

using namespace std;

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

class Person
{
public:
	//  静态成员函数
	static void func()
	{
		m_A = 100; // 静态成员函数可以访问静态成员变量,因为静态成员所有对象共享该成员,不存在区分是哪个对象的问题。
		//m_B = 100; // 静态成员函数不可以访问非静态成员变量,无法区分到底是哪个对象的m_B属性。
		cout << "static 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();
	return 0;
}

this 指针

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

C++中,类内的成员变量和成员函数分开存储

  1. 非静态成员变量 属于类的对象上的数据
  2. 静态成员变量 不属于某个对象上的数据,因此不占用对象内存
  3. 非静态成员函数 不属于类对象上的数据,因此不占用对象内存
  4. 静态成员函数 不属于类对象上的数据,因此不占用对象内存
    只有非静态成员变量才属于属于类的对象上的数据
#include<iostream>

using namespace std;

//  成员变量和成员函数分开存储
class Person
{
	int m_A; // 非静态成员变量 属于类的对象上的数据
	static int m_B; // 静态成员变量 不属于某个对象上的数据,因此不占用对象内存
	void func() {}; // 非静态成员函数 不属于类对象上的数据,因此不占用对象内存
	static void fucn2() {}; // 静态成员函数 不属于类对象上的数据,因此不占用对象内存
};

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

void test02()
{
	Person p;
	cout << "对象占用内存:" << sizeof(p) << endl;
}

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

this指针

C++中成员变量和成员函数是分开存储的,每个非静态成员函数只会诞生一份函数实例,即多个同类型的对象会公用同一块代码,如何区分是哪个对象调用该dai?
通过this指针指向被调用的成员函数所属的对象
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可

1.解决名称冲突
在这里插入图片描述

如图,在没用this时,编译器认为三个age是同一个,类的属性不相干。
解决方法:
(1).成员变量和参数名成编码不同,在成员变量前加m进行区分
在这里插入图片描述
(2).使用this指针
在这里插入图片描述
2.返回对象本身用 *this

#include<iostream>

using namespace std;

// this指针
class Person
{
public:
	Person(int age)
	{
		// this指针指向被调用的成员函数所属的对象
		this->age = age;
	}
	Person& PersonAddAge(Person &p)
	{
		this->age += p.age;

		// this是指向p2的指针,而*this指向的就是p2
		return *this; // 返回类型为引用 
		// 为什么返回引用呢?因为如果返回不是引用则编译器会重新创建一个对象,而不是返回该对象本身 
	}

	int age;
};

// 1.解决名称冲突
void test01()
{
	Person p1(10);
	cout << "p1的年龄:" << p1.age << endl;
}

// 2.返回对象本身用 *this
void test02()
{
	Person p1(10);
	Person p2(18);
	// 链式编程思想
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	cout << "p2的年龄:" << p2.age << endl;

}


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

空指针访问成员函数

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

用空指针调用成员函数,访问对象的某个属性时报错,原因:
是因为传入的指针是NULL,要判断是否为空,保证代码的健壮性。
![[Pasted image 20221219194610.png]]

#include<iostream>

using namespace std;

// 空指针调用成员函数
class Person
{
public:
	void showClassName()
	{
		cout << "This is Person." << endl;
	}
	void showPersonAge()
	{
		// 传入的指针为NULL,因此发生报错
		// 要想用this指针可以使用
		if (this == NULL)
		{
			return; // 防止报错,提高健壮性
		}
		cout << "age = " << this->m_Age << endl;
	}
	int m_Age;
};

void test01()
{
	Person *p = NULL; // 空指针
	p->showClassName();
	p->showPersonAge();
}

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

const修饰成员函数中

常函数

1.成员函数后加const 为常函数
2.常函数内不可以修改成员属性
![[Pasted image 20221219203922.png]]
3.成员属性声明时加关键字mutable后,则在常函数中可以修改

this指针的本质是指针常量,指针的指向无法修改
this指针相对于 Person *const this; 指针的指向无法修改, 但是其指向的值可以修改 即 this->某个属性 = ?
若想限制this指向的值,需设为 const Person *const this;
在这里插入图片描述

常对象

1.声明对象前加const称该对象为常对象
2.常对象只能调用常函数

常对象 不能修改普通变量
![[Pasted image 20221219204035.png]]

常对象不能调用普通成员函数,因为普通成员函数可以修改成员属性
在这里插入图片描述

友元

如果类的私有属性想让类外的特殊函数或者类进行访问时,需要用到友元
友元的目的是让一个函数或者类访问另一个类中的私有成员
友元的三种实现:
1.全局函数做友元

#include<iostream>
#include<string>
using namespace std;
// 1. 全局函数做友元
class Building
{
	// 全局函数goodGay是Building 的友元类,可以访问Building的私有成员
	friend void goodGay(Building &building);
public:
	Building()
	{
		m_SittingRoom = "客厅";
		m_BedRoom = "卧室";
	}

public:
	string m_SittingRoom; //客厅

private:
	string m_BedRoom; //卧室
};

// 全局函数
void goodGay(Building &building)
{
	cout << "好基友的全局函数,正在访问:" << building.m_SittingRoom << endl;
	cout << "好基友的全局函数,正在访问:" << building.m_BedRoom << endl;
}
void test01()
{
	Building build;
	goodGay(build);
}

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

2.类做友元

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

// 2.类做友元
class Building; // 类声明
class GoodGay
{
public:
	GoodGay();
	void visit(); // 参观函数,访问building中的属性

	Building * building;
};

class Building
{
	friend class GoodGay;
public:
	Building();

public:
	string m_SittingRoom; // 客厅

private:
	string m_BedRoom; // 卧室
};

// 类外写成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}

GoodGay::GoodGay()
{
	// 创建一个building类的对象
	building = new Building;
}
void GoodGay::visit()
{
	cout << "GoodGay类正在访问:" << building->m_SittingRoom << endl;
	cout << "GoodGay类正在访问:" << building->m_BedRoom << endl;
}

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

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

友元类是单向的,即类B是类A的友元类,但类A不是类B的友元类
友元类不能传递,即类B是类A的友元类,类C是类B的友元类,但类C不是类A的友元类

3.成员函数做友元

#include<iostream>
#include<string>
using namespace std;
// 3.成员函数做友元
class Building;
class GoodGay
{
public:
	GoodGay();

	void vist(); // 让该函数可以访问Building中的私有成员
	void vist2(); // 让该函数不可以访问Building中的私有成员

	Building * building;
};

class Building
{
	friend void GoodGay::vist(); // 将GoodGay中的vist()函数声明为友元,使其可以访问Building类的私有成员
public:
	Building();

public:
	string m_SittingRoom; // 客厅
private:
	string m_BedRoom; // 卧室
};

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

GoodGay::GoodGay()
{
	building = new Building;
}

void GoodGay::vist()
{
	cout << "GoodGay类vist()函数正在访问:" << building->m_SittingRoom << endl;
	cout << "GoodGay类vist()函数正在访问:" << building->m_BedRoom << endl;
}

void GoodGay::vist2()
{
	cout << "GoodGay类vist()函数正在访问:" << building->m_SittingRoom << endl;
	//cout << "GoodGay类vist()函数正在访问:" << building->m_BedRoom << endl;
}

void test01()
{
	GoodGay gg;
	gg.vist();
	gg.vist2();
}

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

运算符重载

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

加号运算符重载

作用:实现两个自定义数据类型的相加运算
1.成员函数重载运算符 其本质是自定义数据类型 对象 = 对象1 + operator(对象2)
2.全局函数重载运算符 其本质是``自定义数据类型 对象 = operator(对象1 + 对象2)`
3.运算符重载也可以发生函数重载

注意事项
1.对于内置的数据类型的表达式的运算符不可能改变
2.不能随意滥用运算符重载(改变运算符规则比如将+号改为-的功能)

#include<iostream>

using namespace std;

// 运算符重载
// 1.加号运算符重载

class Person
{
public:
	Person() {};
	// 1.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;
	//}
public:
	int m_A;
	int m_B;
}; 

// 1.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 m)
{
	Person temp;
	temp.m_A = p1.m_A + m;
	temp.m_B = p1.m_B + m;
	return temp;
}

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

	// 成员函数重载和全局函数重载调用
	/*Person p3;
	p3 = p1 + p2;*/

	// 成员函数重载的本质调用
	//Person p3 = p1.operator+(p2);
	
	// 全局成员函数重载的本质调用
	Person p3 = operator+(p1, p2);
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;

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

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

左移运算符重载

重载左移运算符配合友元可以实现输出自定义数据类型
本质:void operator << (ostream &cout, Person &p) // 本质 operator<<(cout, p) 只有这样才能简化为 cout << p

#include<iostream>

using namespace std;

// 运算符重载
// 1.加号运算符重载
// 2.左移运算符(输出)重载

class Person
{
public:
	Person() {};
	friend ostream& operator << (ostream &cout, Person &p);
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
	// 2.1 通过成员函数重载 << 运算符
	// 通常不会利用成员函数重载 << 运算符,因为无法实现cout 在左侧
	void operator << (ostream &cout)
	{
		cout << "m_A = " << this->m_A << ",m_B = " << this->m_B << endl;
	}
private:
	int m_A;
	int m_B;
};
// 2.2 通过全局函数重载左移运算符(输出运算符)
// cout 为 输出流 ostream类,cin 为输入流 istream类;且程序有且只能有一个cout和cin,因此将输入输出流cin、cout作为参数时要以引用的方式
//void operator << (ostream &cout, Person &p) // 本质 operator<<(cout, p) 只有这样才能简化为 cout << p
//{
//	cout << "m_A = " << p.m_A << ",m_B = " << p.m_B << endl;
//}

ostream& operator << (ostream &cout, Person &p) // 本质 operator<<(cout, p) 只有这样才能简化为 cout << p
{
	cout << "m_A = " << p.m_A << ",m_B = " << p.m_B << endl;
	return cout;
}

void test02()
{
	Person p(10, 20);
	p << cout; // 成员函数重载 << 只能实现cout在右侧
	cout << p;
	cout << p << endl;// 全局函数重载 << 可以实现cout在左侧 如果重载函数返回的是void则不可以进行链式输出;只有当重载函数返回为输出\输入流类时才能使用链式输出
}


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

注意事项:
1.通常不会利用成员函数重载 << 运算符,因为无法实现cout 在左侧
2.cout 为 输出流 ostream类,cin 为输入流 istream类;且程序有且只能有一个cout和cin,因此将输入输出流cin、cout作为参数时要以引用的方式
3.全局函数重载 << 可以实现cout在左侧 如果重载函数返回的是void则不可以进行链式输出;只有当重载函数返回为输出\输入流类时才能使用链式输出

递增运算符重载

1.前置递增重载 返回类型为 引用
返回引用是为了可以链式++ 时是在自身对象中进行的
数据类型名& operator++()
2.后置递增重载,返回类型为 类型值
以为后置递增返回的是++之前的值,而不是++后的自身的值
3.后置递增重载,要写int 占位符,来使编译器区分前置和后置
数据类型名 operator++(int)

#include<iostream>

using namespace std;
// 3.重载递增运算符
// 自定义整型
class MyInteger
{
	friend ostream& operator <<(ostream& out, MyInteger myint);
public:

	MyInteger()
	{
		m_Num = 0;
	}
	// 重载 前置++ 运算符
	MyInteger& operator ++() // 返回引用是为了可以链式++ 时是在自身对象中进行的
	{
		// 先进行++运算
		m_Num++;
		// 再将自身做返回 
		return *this;
	}

	// 重载 后置++ 运算符
	// int 代表占位参数,可以用于区分前置和后置递增 只能用int
	// 返回类型为 值 而不是引用
	MyInteger operator++(int) 
	{
		// 先 记录当时的结果
		MyInteger temp = *this;

		// 在 自身进行++运算
		m_Num++;

		// 最后 返回记录的当时的结果
		return temp;
	}
	// 重载 前置-- 运算符
	MyInteger & operator--()
	{
		m_Num--;
		return *this;
	}
	// 重载后置--运算符
	MyInteger operator--(int)
	{
		MyInteger temp = *this;
		m_Num--;
		return temp;
	}
private:
	int m_Num;
};

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

void test04()
{
	MyInteger myint;
	cout << myint << endl;
	cout << myint++ << endl;
	cout << myint << endl;
}
void test05()
{
	MyInteger myint;
	cout << "重载前置--" << endl;
	cout << myint << endl;
	cout << --myint << endl;
	cout << myint << endl;
}

void test06()
{
	MyInteger myint;
	cout << "重载后置--" << endl;
	cout << myint << endl;
	cout << myint-- << endl;
	cout << myint << endl;
}

int main()
{
	test03();
	test04();
	test05();
	test06();
	return 0;
}

在这里插入图片描述

赋值运算符重载

默认operator = 是浅拷贝:在执行析构函数时,导致堆区的同一个内存重复释放,程序崩溃。

#include<iostream>
using namespace std;

// 4.赋值运算符重载
class Person2
{
public:
	Person2(int age)
	{
		m_Age = new int(age);
	}
	~Person2()
	{
		if (*m_Age)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	int *m_Age;
};


void test07()
{
	Person2 p1(10);
	Person2 p2(20);
	p2 = p1; // 赋值操作 浅拷贝,在释放指针内存时会导致报错
	cout << "p1的年龄:" << *p1.m_Age << endl;
	cout << "p2的年龄:" << *p2.m_Age << endl;
}


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

在这里插入图片描述

解决方法:深拷贝,在堆区中重新开辟一个空间,存放赋值操作符所赋值的值
在这里插入图片描述

#include<iostream>
using namespace std;

// 4.赋值运算符重载
class Person2
{
public:
	Person2(int age)
	{
		m_Age = new int(age);
	}
	~Person2()
	{
		if (*m_Age)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	// 重载运算符
	Person2& operator = (Person2 &p) // 返回引用!!!进行链式引用
	{
		// 编译器提供的是浅拷贝 m_Age = p.m_Age;
		// 应该先判断是否有数据存放在堆区,若有 应释放后在做深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);
		return *this;
	}
	int *m_Age;
};


void test07()
{
	Person2 p1(10);
	Person2 p2(20);
	Person2 p3(30);
	p2 = p1 = p3; // 赋值操作 浅拷贝,在释放指针内存时会导致报错
	cout << "p1的年龄:" << *p1.m_Age << endl;
	cout << "p2的年龄:" << *p2.m_Age << endl;
	cout << "p3的年龄:" << *p3.m_Age << endl;
}


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

关系运算符重载

#include<iostream>

using namespace std;

// 5. 重载关系运算符
class Person3
{
public:
	Person3(string name, int age) : m_Name(name), m_Age(age){}

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

	// 重载 != 操作符
	bool operator != (Person3& p)
	{
		if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
		{
			return false;
		}
		else {
			return true;
		}
	}

	// 重载 > 操作符
	bool operator > (Person3& p)
	{
		if (this->m_Age > p.m_Age && this->m_Name != p.m_Name)
		{
			return true;
		}
		else {
			return false;
		}
	}

	string m_Name;
	int m_Age;
}
// 5. 重载关系运算符
void test08()
{
	Person3 p1("Tom", 20);
	Person3 p2("Tom", 21);
	Person3 p3("Tom", 20);
	if (p1 > p2)
	{
		cout << "p1大于p2" << endl;
	}
	else {
		cout << "p1小于p2" << endl;
	}
	if (p1 == p3)
	{
		cout << "p1与p3相等" << endl;
	}
	else {
		cout << "p1与p3不相等" << endl;
	}
}

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

在这里插入图片描述

函数调用运算符重载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xxwl.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值