C++核心编程2

初始化列表

作用:c++提供了初始化列表语法,用来初始化属性

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

#include<iostream>
using namespace std;
//初始化列表
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 << "m_A= " << p.m_A << endl;
	cout << "m_B= " << p.m_B << endl;
	cout << "m_C= " << p.m_C << endl;
}
int main()
{
	test01();
	return 0;
}

类对象作为类成员

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

class A{};
class B
{
	A a;
};

 B类中有对象A作为成员,A为对象成员

那么当创建B对象时,A与B的构造和析构的顺序时谁先谁后?

Phone先构造 再构造Person 但是他们的析构函数调用相反

#include<iostream>
using namespace std;
#include<string>
//类对象作为类成员
class Phone
{
public:
	Phone(string pName)
	{
		cout << "Phone的构造函数调用" << endl;
		m_PName = pName;
	}
	~Phone()
	{
		cout << "Phone的析构函数调用" << endl;
	}
	//手机品牌的名称
	string m_PName;
};
class Person
{
public:
	//Phone m_Phone = pName  隐式转换法
	Person(string name, string pName):m_Name(name),m_Phone(pName)
	{
		cout << "Person的构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Perosn的析构函数调用" << endl;
	}
	//姓名
	string m_Name;
	//手机
	Phone m_Phone;
};
//当其他类对象作为本类成员,构造时候先构造类对象,再构造自身,析构的顺序?析构的顺序与构造相反
void test01()
{
	Person p("张三", "苹果MAX");
	cout << p.m_Name << "拿着: " << p.m_Phone.m_PName << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 静态成员

静态成员就是再成员变量和成员函数前加上关键字static,称为静态成员

静态成员分为:

->静态成员变量<-

所有对象共享同一份数据

在编译阶段分配内存

类内声明,类外初始化

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
//静态成员变量
class Person
{
public:
	//1.所有对象都共享一份数据
	//2.编译阶段就分配内存
	//3.类内要进行声明,类外要进行初始化

	static int m_A;//类内声明

	//静态成员变量也是有访问权限的
private:
	static int m_B;
};
int Person::m_A = 100;//表示Person这个类的成员
//int Person::m_B = 200; 
void test01()
{
	Person p;
	cout << p.m_A << endl;
	Person p2;

	p2.m_A = 200;
	cout << p2.m_A << endl;

}
void test02()
{
	//静态成员变量 不属于某个对象上,所有对象都共享同一份数据
	//因此静态成员变量有两种访问方式
	//1.通过对象进行访问
	/*Person p;
	cout << p.m_A << endl;*/
	//2.通过类名进行访问
	cout << Person::m_A << endl;//Person作用域下的m_A
	//cout << Person::m_B << endl; 私有作用域类外是不可以访问的

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

	return 0;
}
//如果出现报错:一个无法解析的外部命令--出错一般在链接部分

->静态成员函数<-

所有对象共享同一个函数

静态成员函数只能访问静态成员变量

#define _CRT_SECURE_NO_WARNINGS 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 << "fun2" << endl;
	}
};
int Person::m_A = 0;

//两种访问方式
void test01()
{
	//1.通过对象访问
	Person p;
	p.func();
	//2.通过类名访问
	Person::func();
	//Person::func2; 类外访问不到私有静态成员函数
}
int main()
{
	test01();
	return 0;
}

 c++对象模型和this指针

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

在c++中,类内的成员函数和成员函数分开存储

只有非静态成员变量才属于类的对象上

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
//成员变量和成员函数是分开存储的
class Person
{
	int m_A;//非静态成员 属于类的对象上
	static int m_B;//静态成员变量 不属于类的对象上
	void func()//非静态成员函数  不属于类的对象上
	{

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

};
int Person::m_B = 0;
void test01()
{
	Person p;
	//空对象占用的内存空间为?1byte
	//c++编译器会给每个空对象也分配一个字节的空间,是为了区分空对象占内存的位置
	//每一个空对象也应该有一个独一无二的内存地址
	cout << "size of p=" << sizeof(p) << endl;
}
void test02()
{
	Person p;
	//size of p=4 直接按照int
	cout << "size of p=" << sizeof(p) << endl;

	static int m_B;//
}

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

this指针的概念

我们直到c++中成员变量和成员函数是分开存储的

每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象共用一块代码

那么问题是:这一块代码是如何区分那个对象调用自己的呢?

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

this指针隐含每一个非静态成员函数内的一种指针

this指针不需要定义,直接使用即可

this指针的用途:

当形参和成员变量同名时,可用this指针来区分

在类的非静态成员函数中分会对象本身,可使用return*this

#include<iostream>
using namespace std;
// 1.解决名称冲突

//2.返回对象本身用*this


class Person
{
public:
	Person(int age)
	{
		//this指针指向的是被调用函数的所属对象
		this->age = age;
	}
	Person& PersonAddAge(Person& p)
	{//如果说返回值不是Person&而是Perspn -->20 会创建一个新的对象
		this->age += p.age;
		
		//this指向p2的指针,而*this指向的就是p2这个对象本体
		return *this;
	}
	int age;
};
//1.解决名称冲突
void test01()
{
	Person p1(18);
	cout << "p1的年龄为:" << p1.age << endl;
}
//2.返回对象本身用*this
void test02()
{
	Person p1(10);
	Person p2(20);
	//链式编程思想
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	cout << "p2的年龄为:" << p2.age << endl;

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

空指针访问成员函数

c++中的空指针也可以是调用成员函数的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

#include<iostream>
using namespace std;
//空指针调用成员函数
class Person
{
public:
	void showClassName()
	{
		cout << "this is Person class" << endl;

	}
	void showPersonAge()
	{
		//报错原因是因为传入的指针是NULL
		if (this == NULL)
		{
			return;
		}//保证程序的健壮性
		cout << "age=" << this->m_Age << endl;
		//因为p是空指针 this指向空 空访问不到成员变量
	}
	int m_Age;
};
void test01()
{
	Person* p = NULL;

	p->showClassName();

	p->showPersonAge();

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

 const修饰成员函数

常函数:成员函数加const后我们称为这个函数为常函数

常函数内不可以修改成员属性

成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:

声明对象前加const称该对象为常对象

常对象只能调用常函数

#include<iostream>
using namespace std;
//常函数 常对象
class Person
{
public:
	//this指针的本质 是指针常量 指针的指向是不可以修改的
	//const Person * const this;
	//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改
	void showPerson() const
	{
		this->m_B = 100;
		//this->m_A = 100;
		//this = NULL;//this指针不可以修改指针的指向的
	}
	void func(){}
	int m_A;
	mutable int m_B;//特殊变量 即使在常函数中,也可以修改这个值 加关键字mutable
};
void test01()
{
	Person p;
	p.showPerson();
}
//常对象
void test02()
{
	const Person p;//在对象前加const,变为常对象
	//p.m_A = 100;
	p.m_B = 100;//在常对象下也可以修改
	
	//常对象只能调用常函数
	p.showPerson();
	//p.func();//因为普通成员函数可以修改

}
int main()
{
	
	return 0;
}

友元

生活中你的家有客厅(Public),有你的卧室(Private)

课堂所有来的客人都可以进去,但是你的卧室是私有的,也就是只有你能进去

但是呢?你也可以允许你的好朋友进去。

在程序里 有些私有属性 也想让类外特殊的一些函数或者类进行访问,就需要友元的技术

友元的母的就是让一个函数或者类访问另外一个类的私有成员

友元的关键字为friend

友元的三种实现

全局函数做友元

类做友元

成员函数做友元

#include<iostream>
using namespace std;
#include<string>
//建筑物类
class Building
{
	//goodGay全局函数是Building好朋友,可以访问Building中私有的成员
	friend void goodGay(Building* building);
public:
	Building()
	{
		m_SittingRoom = "客厅";
		m_BedRoom = "卧室";

	}
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
};
//全局函数
void goodGay(Building* building)
{
	cout << "好基友全局函数 正在访问:" << building->m_SittingRoom << endl;
	cout << "好基友全局函数 正在访问:" << building->m_BedRoom<< endl;
}
void test01()
{
	Building building;
	goodGay(&building);
}
int main()
{
	test01();
	return 0;
}

#include<string>
//类做友元
class Building;
class GoodGay
{
public:

	GoodGay();
	void visit();//参观函数 访问Building中的属性
	Building* building;
};
void GoodGay::visit()
{
	cout << "好基友正在访问:" << building->m_SittingRoom << endl;
	cout << "好基友正在访问:" << building->m_BedRoom << endl;
}
class Building
{
	//GoodGay是本类的好朋友,可以访问奔类中私有成员
	friend class GoodGay;
public:
	Building();
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
};
//类外写成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
	//创建建筑物对象
	building = new Building;
}
void test01()
{
	GoodGay gg;
	gg.visit();
}
int main()
{
	test01();
	return 0;
}

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

	GoodGay();
	void visit();//让visit函数可以访问Building中私有成员
	void visit2();//让visit2函数不可以访问Building中私有成员
	Building* 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;
}
class Building
{
	//告诉编译器,GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员

	friend void GoodGay::visit();
public:
	Building();
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
};
//类外写成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
	//创建建筑物对象
	building = new Building;
}
void test01()
{
	GoodGay gg;
	gg.visit();
	gg.visit2();
}
int main()
{
	test01();
	return 0;
}

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

	GoodGay();
	void visit();//让visit函数可以访问Building中私有成员
	void visit2();//让visit2函数不可以访问Building中私有成员
	Building* 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;
}
class Building
{
	//告诉编译器,GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员

	friend void GoodGay::visit();
public:
	Building();
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
};
//类外写成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
	//创建建筑物对象
	building = new Building;
}
void test01()
{
	GoodGay gg;
	gg.visit();
	gg.visit2();
}
int main()
{
	test01();
	return 0;
}

运算符重载

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

加号运算符重载

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

#include<iostream>
using namespace std;
#include<string>
//加号运算符重载

class Person
{
public:
	//1.成员函数运算符重载+号
	//成员函数本质:Person p3=p1.operator+(p2)
	/*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.全局运算符重载+号
//本质:P3=operator+(p1,p2);
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 num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}
void test01()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A=10;
	p2.m_B=10;

	Person p3 = p1 + p2;
	Person p4 = p1 + 10;

	cout << "p3.m_A= " << p3.m_A << endl;
	cout << "p4.m_A= " << p4.m_A << endl;
	cout << "p4.m_B= " << p4.m_B << endl;
	cout << "p3.m_B= " << p3.m_B << endl;
}
int main()
{
	test01();
	return 0;
}

对于内置数据类型的表达式的运算符是不可以改变

不要滥用重载运算符 

#include<iostream>
using namespace std;
//左移运算符的重载
class Person
{
	
	friend ostream& operator<<(ostream& cout, Person& p);
public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
private:

	//利用成员函数重载 左移运算符
	 cout是一个ostream(标准输出流)类型的对象
	//void operator<<(Person& p)
	//{目的让编译器认识“调用的对象<<传入对象”这种新格式
	//不回利用成员函数重载<<运算符,因为无法实现cout在左侧
	//}所有的重载都应该使用全局,不然会有局限性,而且方便代码的规范
	int m_A;
	int m_B;

};
ostream& operator<<(ostream& cout, Person& p)//本质 operator<<(cout,p)简化->cout<<p p前加引用为了省内存也可以用值传递
{
	cout << "m_A= " << p.m_A << "m_B= " << p.m_B;
	return cout;
}
void test01()
{
	Person p(10,10);
	//p.m_A = 10;
	//p.m_B = 10;
	//cout << p.m_A << endl;
	cout << p<<endl;//这里不能加endl ->ostream& 链式编程
	//
}


int main()
{
	test01();
	return 0;
}
#include<iostream>
using namespace std;
//递增运算符的重载 实现自己的整形数据
//自定义的整型
class MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
	MyInteger()
	{
		m_Num = 0;
	}
	//重载前置++运算符
	MyInteger& operator++()//返回引用是为了一直对一个数据进行递增操作
	{
		//先进行++运算
		m_Num++;
		//再将自身做一个返回
		return *this;
	}
	//重载后置++运算符
	//这个int代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++(int)//返回值 因为temp会被销毁
	{
		//先 记录一下但是的结果
		MyInteger temp = *this;
		//后 递增
		m_Num++;
		//返回结果
		return temp;
	}

private:
	int m_Num;
};
//重载一下左移运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
	cout << myint.m_Num;
	return cout;
}
void test02()
{
	MyInteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}
void test01()
{
	MyInteger myint;
	cout << myint << endl;
	cout <<++ myint << endl;
}
int main()
{
	test01();
	test02();
	return 0;
}

自己写--。

赋值运算符重载

c++编译器至少给一个类添加4个函数

1.默认构造函数(无参 函数体为空)

2. 默认析构函数(无参,函数体为空)

3.默认拷贝构造函数,对属性进行值拷贝

4.赋值运算符operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝的问题

#include<iostream>
using namespace std;
//赋值运算符的重载
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)
	{
		//编译器提供浅拷贝
		//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 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;
}

 关系运算符重载

#include<iostream>
using namespace std;
//重载关系运算符
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 false;
		}
		
		return true;
		
	}
	string m_Name;
	int m_Age;
};
void test01()
{
	Person p1("Tom",18);

	Person p2("Tom", 17);

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


	if (p1 != p2)
	{
		cout << "p1和p2是bu相等的!" << endl;
	}
	else
	{
		cout << "p1和p2是yes相等的!" << endl;
	}
}
int main()
{
	test01();

	return 0;
}

函数调用运算符重载

函数调用运算符()也可以重载

由于重载后使用的方式非常像函数的调用,因此称为仿函数

仿函数没有固定写法,非常灵活

 

#include<iostream>
using namespace std;
#include<string>
//函数调用运算符重载
//打印输出类
class MyPrint
{
public:
	//重载的函数调用运算符
	void operator()(string test)
	{
		cout << test << endl;
	}
};
void Myprint02(string test)
{//由于使用起来非常像函数调用 所以被称之为仿函数
	cout << test << endl;
}
//写法灵活 没有固定写法
//加法类
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
}
void test01()
{
	MyPrint myprint;
	myprint("helloworld");
	Myprint02("helloworld");
}
int main()
{
	//test01();
	test02();

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值