2021-02-24 Check in and learn C++ for the tenth day


One, class and object

1. Package

2. Initialization and cleanup of objects

(5) Deep copy and shallow copy

Deep copy is a classic interview question and a common pit

  • Shallow copy: If you use the copy constructor provided by the compiler, you will do a shallow copy operation, which is a simple assignment copy operation
  • Deep copy: re-apply for space in the heap area for copy operation

If the attribute is developed in the heap area, you must provide your own copy constructor to prevent problems caused by shallow copy
Example

#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(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);
	cout << "p1的年龄为:" << p1.m_age ;
	cout << " p1的身高为:" << *p1.m_height << endl;
	person p2(p1);
	cout << "p2的年龄为:" << p2.m_age;
	cout << " p2的身高为:" << *p2.m_height << endl;
}

int main()
{
    
    
	test01();

	system("pause");
	return 0;
}


(6) Initialization list

Role: C++ provides initialization list syntax to initialize properties

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

Example

#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(1, 2, 3);
	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();

	system("pause");
	return 0;
}


(7) Class objects as class members

A member in C++ can be an object of another class, we call this member an object member

When other class objects are used as members of this class, the class object is constructed first, and then itself

E.g

class A {
    
     }
class B
{
    
    
	A a;
}

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

Example

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

//类对象作为类成员

//手机类
class phone
{
    
    
public:
	phone(string pname)
	{
    
    
		m_Pname = pname;
		cout << "phone的构造函数调用" << endl;
	}

	//手机品牌名
	string m_Pname;

};

class person
{
    
    
public:

	person(string name, string pname) :m_name(name), m_phone(pname)
	{
    
    
		cout << "person的构造函数调用" << endl;
	}
	string m_name; //姓名
	phone m_phone;//手机品牌

};

void test01()
{
    
    
	
	person p("张三", "三星");
	cout << p.m_name  << endl;
	cout << p.m_phone.m_Pname << endl;
}

int main()
{
    
    
	test01();

	system("pause");
	return 0;
}


Output result
Insert picture description here

(8) Static members

A static member is to add the keyword static before member variables and member functions, called static members

Static members are divided into:

1* Static member variable

  • All objects share the same data
  • Allocate memory during compilation
  • In-class declaration, out-of-class initialization

2* Static member function

  • All objects share the same function
  • Static member functions can only access static member variables

Static member functions also have access rights: private static member functions cannot be accessed outside the class

Example

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

//静态成员函数

class person
{
    
    
public:
	static void func()
	{
    
    
		A = 20;
		//B = 20;//静态成员函数不可访问非静态成员变量,无法区分是哪个对象的属性
		cout << "static void func调用" << endl;
	}
	
	static int A;//静态成员变量
	int B;//非静态变量成员

};
int person::A = 0;
//两种访问方式
void test01()
{
    
    
	//1、通过对象访问
	person p;
	p.func();

	//2、通过类名访问
	person::func();
}

int main()
{
    
    
	test01();

	system("pause");
	return 0;
}


3. C++ object model and this pointer

(1) separate storage of member variables and member functions

In C++, member variables and member functions in a class are stored separately

Only non-static member variables belong to objects of the class

The C++ compiler will allocate each empty objectOne byte space, Is to distinguish the memory location occupied by the object, each empty object should have a unique memory address

  1. Non-static member variables: on objects belonging to the class
  2. Static member variables: on objects that do not belong to the class
  3. Non-static member function: on objects that do not belong to the class
  4. Static member function: on objects that do not belong to the class

(2) The concept of this pointer

The essence of this pointer is a pointer constant, the pointer cannot be modified, but the value can be modified

Each non-static member function will only produce a function instance, which means that multiple objects of the same type will share a piece of code

C++ solves the above problems by providing a special object pointer, this pointer

The this pointer points to the object to which the member function is called

  • This pointer is a pointer that implies every non-static member function
  • This pointer does not need to be defined, it can be used directly

The purpose of this pointer:

  • When the formal parameter and the member variable have the same name, the this pointer can be used to distinguish (resolve name conflicts)
  • To return the object itself in the non-static member function of the class, you can use return this ( use this to return the object itself )

Example

#include<iostream>
using namespace std;

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(18);
	cout << "p1的年龄为:" << p1.age << endl;

}

//2、返回对象本身
void test02()
{
    
    
	person p1(10);
	person p2(10);

	//链式编程思想
	p2.personAddage(p1).personAddage(p1);
	cout << "p2的年龄为:" << p2.age << endl;

}

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

(3) Null pointer access to member functions

Null pointers in C++ can also call member functions, but also pay attention to whether the this pointer is used

If this pointer is used, it needs to be judged to ensure the robustness of the code

Example

#include<iostream>
using namespace std;

class person
{
    
    
public:
	
	void showClassName()
	{
    
    
		cout << "this is person class" << endl;
	}

	void showPersonAge()
	{
    
    
		if (this == NULL)
			return;
		cout << "age = " << m_age << endl;
	}
	int m_age;
};
//1、解决名称冲突
void test01()
{
    
    
	person *p = NULL;
	p->showClassName();
	p->showPersonAge();
}



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

(4) const modified member function

Constant function:

  • After adding const after the member function, we call this function a constant function
  • Member properties cannot be modified in constant functions
  • After adding the keyword mutable in the member attribute declaration, it can still be modified in the normal function

Regular object:

  • Add const before declaring the object to call the object a constant object
  • Constant objects can only call constant functions

Example

#include<iostream>
using namespace std;

class person
{
    
    
public:
	//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可修改
	void showPerson()const
	{
    
    
		m_b = 100;//可修改

	}
	int m_age;
	mutable int m_b;//特殊变量
};

void test01()
{
    
    
	person p;
	p.showPerson();
}

//常对象
//常对象只能调用常函数
void test02()
{
    
    
	const  person p;
	//p.m_age = 100;
	p.m_b = 100;
}


int main()
{
    
    
	test01();

	system("pause");
	return 0;
}

[Note] The learning course is-Dark Horse Program C++ Tutorial

Guess you like

Origin blog.csdn.net/qq_42616280/article/details/114034340