Day 94 Learning Record: C++ Core: Classes and Objects III (five-star important)

deep copy vs shallow copy

Deep and shallow copy is a classic interview question, and it is also a common pit. Shallow
copy: simple assignment copy operation
Deep copy: re-apply space in the heap area for copy operation

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class  Person
{
    
    
public:
	Person()
	{
    
    
		cout << "Person的无参数构造函数调用" << endl;//无参构造函数(默认构造函数)
	}
	Person(int age,int height)
	{
    
    
		m_Age = age;
		m_Height=new int(height);
		cout << "Person的带参数构造函数调用" << endl;//有参构造函数
	}
	~Person()
	{
    
    
		//析构代码,将堆区开辟的数据做释放操作
		if (m_Height != NULL)
		{
    
    
			delete m_Height;
			m_Height = NULL;
		}
		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);
	}
	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 << "p1的年龄为:" << p2.m_Age << "身高为:" << *p2.m_Height << endl;
}

int main()
{
    
    
	test01();
	system("pause");
	return 0;
}

initialization list

Role:
C++ provides an initialization list syntax for initializing attributes Syntax
: Constructor (): attribute 1 (value 1), attribute 2 (value 2)...{}

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class  Person
{
    
    
public:
	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;
};

int main()
{
    
    
	Person p(10,20,30);
	cout << p.m_A << p.m_B << p.m_C << endl;
	system("pause");
	return 0;
}

class object as class member

A member in a C++ class can be an object of another class. We call this member an object member.
insert image description here
In class B, there is object A as a member, and A is an object member.
Problem: When creating a B object, the construction and destruction of A and B Who comes first in order?

#define _CRT_SECURE_NO_WARNINGS 1

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

class Phone
{
    
    
public:
	Phone(string pName)
	{
    
    
		m_PName = pName;
		cout << "Phone的构造函数调用" << endl;
	}
	~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 << "Person的析构函数调用" << endl;
	}
	string m_Name;
	Phone m_Phone;
};

//当其他类对象作为本类成员,构造时候先构造类对象,再构造自身。
//析构的顺序与构造相反

void test01()
{
    
    
	Person p("张三", "苹果XR");
	cout << p.m_Name << "拿着" << p.m_Phone.m_PName << endl;
}

int main()
{
    
    
	test01();
	system("pause");
	return 0;
}

insert image description here

static member variable

A static member is to add the keyword static before the member variable and member function, which is called a static variable.
Static members are divided into:

static member variable

All objects share a piece of data
Allocate memory during compilation
In-class declaration, out-of-class initialization

#include<iostream>
using namespace std;

class  Person
{
    
    
public:
	//1、所有对象都共享同一份数据
	//2、在编译阶段分配内存
    //3/类内声明,类外初始化
	static int m_A;
};

int Person::m_A = 100;

void test01()
{
    
    
	Person p;
	cout << p.m_A << endl;

	Person p2;
	p.m_A = 200;
	cout << p.m_A << endl;
}

int main()
{
    
    
	test01();
	system("pause");
	return 0;
}

insert image description here

#include<iostream>
using namespace std;

class  Person
{
    
    
public:
	//1、所有对象都共享同一份数据
	//2、在编译阶段分配内存
    //3/类内声明,类外初始化
	Person()
	{
    
    

	}
	static int m_A;
	//静态成员变量也是有访问权限的
private:
	static int m_B;
};

int Person::m_A = 100;
int Person::m_B = 200;

void test01()
{
    
    
	Person p;
	cout << p.m_A << endl;

	Person p2;
	p.m_A = 200;
	cout << p.m_A << endl;
}

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

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

static member function

All objects share a function
Static member functions can only access static member variables

#include<iostream>
using namespace std;

class  Person
{
    
    
public:
	Person()
	{
    
    

	}
	static void func()
	{
    
    
		m_A = 100;//静态的成员函数可以访问静态成员变量
		//m_B = 100;//静态成员函数不可以访问非静态成员变量
		cout << "static void func调用" << endl;
	}
	static int m_A;
	int m_B;
private:
	static void fun2()
	{
    
    
		cout << "static void func2调用" << endl;
	}
};

int Person::m_A = 100;

void test01()
{
    
    
	//1、通过对象访问
	Person p;
	p.func();
	//2、通过类名访问
	Person::func();
	//Person::func2();//类外访问不到私有的静态成员函数
}

int main()
{
    
    
	test01();
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45694614/article/details/131491979