VScode 学习c++的一些笔记(持续更新)(后期用的vs2019)

VScode 学习c++的一些笔记(持续更新)

1.文件夹

文件夹以及里面所有的文件的名字都不能含中文。

2.返回值优化

在学拷贝构造函数调用时机的第三种情况:以值方式返回局部对象时出现了和老师不一样的结果。

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

class Person {
public:
	Person() {
		cout << "无参构造函数!" << endl;
		mAge = 0;
	}
	Person(int age) {
		cout << "有参构造函数!" << endl;
		mAge = age;
	}
	Person(const Person& p) {
		cout << "拷贝构造函数!" << endl;
		mAge = p.mAge;
	}
	//析构函数在释放内存之前调用
	~Person() {
		cout << "析构函数!" << 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;
	return p1;
}

void test03()
{
	Person p = doWork2();
	cout << (int *)&p << endl;
}

int main() {
	// test01();
	//test02();
	test03();//为什么输出没有拷贝函数的调用

	system("pause");

	return 0;
}

老师的结果调用了拷贝函数,而我的vscode结果如下
在这里插入图片描述
可以发现并没有调用拷贝构造函数,经百度发现是返回值优化造成的。

参考:C++中的返回值优化(RVO)

3.关于引用

引用相当于常量指针,指向不可以更改,内容可以更改,但是加入const后变成常量引用后只具有可读性。

int &ref=a;//ref和a指向同一块地址
ref=20//编译器会自动解引用,*ref=20,虽然可读性变差,但是优化了程序

如果返回值是引用,返回值就是本身;如果返回值是一个值,实际上返回的是一个值的副本
例:
下面代码是一个类的成员函数,前置递增运算符重载,这是加了引用的函数

	MyInteger& operator++() {
		//先++
		m_Num++;
		//再返回
		return *this;
	}

主函数里面输入:

MyInteger myInt;
	cout << ++myInt << endl;
	cout << myInt << endl;

初始值为0输出结果为:
1
1

主函数里面改为:

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

输出:
2
2

成员函数去掉引用后:

	MyInteger operator++() {
		//先++
		m_Num++;
		//再返回
		return *this;
	}

主函数里面:

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

输出结果为:
2
1
输出的myInt结果并不是2的原因是编译器在做第一个++时返回的是一个副本,而在做第二个++时是在第一次的那个副本的基础上做的,此时的操作对象已经不是myInt了,相当于对myInt只做了一次++操作,因此结果为1。

4.深拷贝与浅拷贝

深拷贝:需要创建新的内存处new int(*某括号内的对象成员地址)

在类中,用new要注意,浅拷贝会拷贝地址,而且还会释放地址,地址都没了,谈何数据。

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

person p2(p1)//如果利用编译器提供的拷贝构造函数,会做浅拷贝操作

析构函数释放的时候遵循堆的规则:先进后出,先进来的后被释放
浅拷贝带来的问题:堆区的内容被重复释放
在这里插入图片描述

堆区的数据需要程序员手动释放,可以利用析构代码将堆区开辟数据做释放操作

	//析构函数
	~Person() {
		cout << "析构函数!" << endl;
		if (m_height != NULL)
		{
			delete m_height;
			m_height=NULL;//防止野指针出现,做一个置空的操作
		}
	}

关于深拷贝与浅拷贝的整块代码见下:

#include <iostream>
using namespace std;
#include <ctime>
#include <string>
class Person {
public:
	//无参(默认)构造函数
	Person() {
		cout << "无参构造函数!" << endl;
	}
	//有参构造函数
	Person(int age ,int height) {
		
		cout << "有参构造函数!" << endl;

		m_age = age;
		m_height = new int(height);//利用new创建的数据,会返回该数据对应的类型的指针,用指针去接收堆区的数据
		//new创建返回的是一个地址(这个地址保存了height的值),所以要用指针接收
	}
	//拷贝构造函数  
	Person(const Person& p) {
		cout << "拷贝构造函数!" << endl;
		//如果不利用深拷贝在堆区创建新内存,会导致浅拷贝带来的重复释放堆区问题
		m_age = p.m_age;
		//m_height =p.m_height;//编译器默认实现的就是这行代码
		//深拷贝操作
		m_height = new int(*p.m_height);//先解引用,然后再开辟一块新内存,再利用指针接收
		
	}

	//析构函数
	~Person() {
		cout << "析构函数!" << endl;
		if (m_height != NULL)
		{
			delete m_height;
			m_height=NULL;//防止野指针出现,做一个置空的操作
		}
	}
public:
	int m_age;
	int* m_height;
};

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

	Person p2(p1);

	cout << "p1的年龄: " << p1.m_age << " 身高: " << *p1.m_height << endl;

	cout << "p2的年龄: " << p2.m_age << " 身高: " << *p2.m_height << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

5.类和对象-对象特性-静态成员-静态成员函数

#include <iostream>
using namespace std;
#include <ctime>
#include <string>
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();

	system("pause");

	return 0;
}

运行结果:
在这里插入图片描述
可以发现,静态成员变量是所有的对象都共有的,不管是哪个对象对静态成员变量做出了赋值操作,所有静态成员变量的值都会发生变化。

初始化是赋一个初始值,而定义是分配内存。静态成员变量在类中仅仅是声明,没有定义,所以要在类的外面定义,实际上是给静态成员变量分配内存。

静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“对象(实例)级别”的。 类级别的成员,先于该类任何对象的存在而存在,它被该类所有的对象共享。

只有非静态成员变量属于类的对象上,静态成员变量、静态/非静态成员函数不属于类的对象上,对象的占用空间只包含了非静态成员变量的大小。

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

int main() {

	cout << sizeof(Person) << endl;

	system("pause");

	return 0;
}

6.类和对象-对象特性-this指针的用途

#include <iostream>
using namespace std;
#include <ctime>
#include <string>
class Person
{
public:

	Person(int age)
	{
		//1、当形参和成员变量同名时,可用this指针来区分
		this->age = age;
	}

	Person& PersonAddPerson(Person p)//这里如果用指针返回的是地址,不是返回对象;
	{
		this->age += p.age;
		//返回对象本身
		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();

	system("pause");

	return 0;
}

对于其中某部分函数:

	Person& PersonAddPerson(Person p)//这里如果用指针返回的是地址,不是返回对象;
	{
		this->age += p.age;
		//返回对象本身
		return *this;
	}

如果这里去掉&的话变成了值的方式返回,本体创建了一个新的数据调用了拷贝构造函数,按照自身拷贝了一个新的值做了一个返回值,跟本体不一样了。
在这里插入图片描述

7.左移运算符重载

#include <iostream>
using namespace std;
#include <ctime>
#include <string>
class Person {
	friend ostream& operator<<(ostream& out, Person& p);

public:

	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}

	//成员函数 实现不了  p << cout 不是我们想要的效果
	//void operator<<(Person& p){
	//}

private:
	int m_A;
	int m_B;
};

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p) {//ostream为标准输出流,全局只有一个,所以要加&,通过地址调用,out只是引用名,就是cout
	out << "a:" << p.m_A << " b:" << p.m_B;
	return out;
}

void test() {

	Person p1(10, 20);

	cout << p1 << "hello world" << endl; //链式编程
}

int main() {

	test();

	system("pause");

	return 0;
}

注意事项:
1.全局函数实现
2.引用实现

8.递增运算符重载

前置递增引用返回
后置递增值返回—这也是后置递增递减耗时的原因

//前置++
	MyInteger operator++() {
		//先++
		m_Num++;
		//再返回
		return *this;
	}

	//后置++
	MyInteger operator++(int) {
		//先返回
		MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
		m_Num++;
		return temp;
	}

后置递增返回的是一个局部的对象,局部对象在当前函数执行完之后这个对象就被释放掉了,所以如果还要返回它的引用的话就是非法操作了
前置递增和后置递增完整版代码:

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

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();

	system("pause");

	return 0;
}

发现一个有趣的现象:当像完整版代码里面那样连续进行两次后置递增后输出结果为:
0
1
请按任意键继续. . .
发现只进行了一次后置递增,原因就是代码里面后置递增是值引用,返回的是一个副本,而不是原来的那个对象了,所以第一次返回的那个值是0,0再利用这个函数再返回一次还是0。
但是前置递增因为是引用返回,所以可以进行连续的递增,结果正常。

自己写的一个前置递减与后置递减函数如下:

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

class person
{
public:
	int p_a=3;
public:
	person(){
		cout<<"person的构造函数"<<endl;
	};

	//前置--
	person operator--(){
     p_a--;
	 return *this;
	}
	//后置--
	person operator--(int){
		person temp=*this;
		p_a--;
		return temp;
	}
	~person(){
		cout<<"person的析构函数"<<endl;
	};
};

	ostream& operator<<(ostream &cout,person& p)
	{
      cout<<p.p_a<<endl;
	  return cout;
	}

int main() {

person p1;
// --(--p1);
(p1--)--;
cout<<p1<<endl;

	system("pause");
	return 0;
}

9.赋值运算符重载

区分赋值运算符重载和拷贝构造函数
C++拷贝构造、赋值构造详解
C ++ 拷贝构造函数和赋值构造函数 非常经典

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

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符 operator=, 对属性进行值拷贝

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

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

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 = p.m_Age;

		//提供深拷贝 解决浅拷贝的问题
		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);

	p2 = p1; //赋值操作

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

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

	//cout << "p3的年龄为:" << *p3.m_Age << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

输出结果为:
p1的年龄为:18
p2的年龄为:18
请按任意键继续. . .

没懂的点是为什么写p2=p1时,在函数Person& operator=(Person &p)中调用时,先释放了p2的指针(因为p2的指针不为空这个我知道,但是等号后面是p1,为啥不是释放p1?),然后将开辟一块新的地址,将p1.m_Age的值放进这个新开辟的地址中,然后再将这个地址赋值给p2的m_Age

重新分析了一下,下面这个函数中的m_Age应该是等号前的对象的m_Age,
p.m_Age是等号后的对象的m_Age

	//重载赋值运算符 
	Person& operator=(Person &p)
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//编译器提供的代码是浅拷贝
		//m_Age = p.m_Age;

		//提供深拷贝 解决浅拷贝的问题
		m_Age = new int(*p.m_Age);

		//返回自身
		return *this;
	}

10.菱形继承

class Animal
{
public:
	int m_Age;
};

//继承前加virtual关键字后,变为虚继承
//此时公共的父类Animal称为虚基类
class Sheep : virtual public Animal {};
class Tuo   : virtual public Animal {};
class SheepTuo : public Sheep, public Tuo {};

void test01()
{
	SheepTuo st;
	st.Sheep::m_Age = 100;
	st.Tuo::m_Age = 200;

	cout << "st.Sheep::m_Age = " << st.Sheep::m_Age << endl;
	cout << "st.Tuo::m_Age = " <<  st.Tuo::m_Age << endl;
	cout << "st.m_Age = " << st.m_Age << endl;
}


int main() {

	test01();

	system("pause");

	return 0;
}
  • 菱形继承带来的主要问题是子类继承两份相同的数据,导致资源浪费以及毫无意义
  • 利用虚继承可以解决菱形继承问题

变成虚继承之后父类的m_Age相当于一个指针,第二代的两个都变成了指针,指向全文唯一的一个m_Age的地址,全文只有一个m_Age,代码的最后一句赋值语句为它赋的值就是它的最终值,所有的m_Age输出都是这个最后的值。

11.多态

利用虚函数加重写,当用父类的指针指向子类时,该指针可以找到子类的函数对应的地址,调用子类的函数,不会调用父类的函数,如果不重写的话,子类的table里面放的还是父类的函数对应的地址。
在这里插入图片描述

class Animal
{
public:
	//Speak函数就是虚函数
	//函数前面加上virtual关键字,变成虚函数,那么编译器在编译的时候就不能确定函数调用了。
	virtual void speak()
	{
		cout << "动物在说话" << endl;
	}
};

class Cat :public Animal
{
public:
	void speak()
	{
		cout << "小猫在说话" << endl;
	}
};

class Dog :public Animal
{
public:

	void speak()
	{
		cout << "小狗在说话" << endl;
	}

};
//我们希望传入什么对象,那么就调用什么对象的函数
//如果函数地址在编译阶段就能确定,那么静态联编
//如果函数地址在运行阶段才能确定,就是动态联编

void DoSpeak(Animal & animal)
{
	animal.speak();
}
//
//多态满足条件: 
//1、有继承关系
//2、子类重写父类中的虚函数
//多态使用:
//父类指针或引用指向子类对象

void test01()
{
	Cat cat;
	DoSpeak(cat);


	Dog dog;
	DoSpeak(dog);
}


int main() {

	test01();

	system("pause");

	return 0;
}

12.多态-虚析构和纯虚析构

当子类中有在堆区开辟数据时,需要在子类析构函数中释放内存,不然会造成内存泄漏,但是在使用多态时,父类指针或引用指向子类对象,在调用析构函数时只会调用父类的析构函数,不会调用子类的析构函数。在父类中加上加上虚析构和纯虚析构后可以解决在子类中调不到子类析构的问题。

纯虚析构在类内定义,在类外声明
虚析构可以实例化对象,纯虚析构不可以实例化对象

虚析构语法:

virtual ~类名(){}

纯虚析构语法:

virtual ~类名() = 0;

类名::~类名(){}

class Animal {
public:

	Animal()
	{
		cout << "Animal 构造函数调用!" << endl;
	}
	virtual void Speak() = 0;

	//析构函数加上virtual关键字,变成虚析构函数
	//virtual ~Animal()
	//{
	//	cout << "Animal虚析构函数调用!" << endl;
	//}


	virtual ~Animal() = 0;
};

Animal::~Animal()
{
	cout << "Animal 纯虚析构函数调用!" << endl;
}

//和包含普通纯虚函数的类一样,包含了纯虚析构函数的类也是一个抽象类。不能够被实例化。

class Cat : public Animal {
public:
	Cat(string name)
	{
		cout << "Cat构造函数调用!" << endl;
		m_Name = new string(name);
	}
	virtual void Speak()
	{
		cout << *m_Name <<  "小猫在说话!" << endl;
	}
	~Cat()
	{
		cout << "Cat析构函数调用!" << endl;
		if (this->m_Name != NULL) {
			delete m_Name;
			m_Name = NULL;
		}
	}

public:
	string *m_Name;
};

void test01()
{
	Animal *animal = new Cat("Tom");
	animal->Speak();

	//通过父类指针去释放,会导致子类对象可能清理不干净,造成内存泄漏
	//怎么解决?给基类增加一个虚析构函数
	//虚析构函数就是用来解决通过父类指针释放子类对象
	delete animal;
}

int main() {

	test01();

	system("pause");

	return 0;
}

后面记录的均是用的VS2019

13.写文件时想要不覆盖原有文件内容时,可以写成以下内容:

ofstream ofs;
	ofs.open(FILENAME, ios::out|ios::app);//加ios::app在写入文件时不会覆盖原有内容

14.关于容器的改变排序规则
在用VS2019时,自定义规则函数一定要加const,不然会出错

//改变排序规则——由大到小
class MyCompare {
public:
	bool operator()(int v1, int v2)const {
		return v1 > v2;
	}
};
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值