• C++_默认值拷贝构造函数_构造函数顺序


    C++_默认值拷贝构造函数_构造函数顺序

    1、创建类时除了析构和构造函数还有一种值拷贝构造函数
    注意:值拷贝构造函数是用于整个类对象给另一个类对象赋值的比如这样(Person per2(per);)把实例化的per里面的参数值复制给per2,他们使用的是同一片内存空间,当per释放的时候,per2也不能使用了

    #include 
    #include 
    #include 
    
    using namespace std;
    
    class Person {
    private:
    	char *name;
    	int age;
    	char *work;
    
    public:
    
    	Person() {//cout <<"Pserson()"<
    		name = NULL;
    		work = NULL;
    	}
    	Person(char *name) 
    	{
    		//cout <<"Pserson(char *)"<
    		this->name = new char[strlen(name) + 1];
    		strcpy(this->name, name);
    		this->work = NULL;
    	}
    
    	Person(char *name, int age, char *work = "none") 
    	{
    		//cout <<"Pserson(char*, int)"<
    		this->age = age;
    
    		this->name = new char[strlen(name) + 1];
    		strcpy(this->name, name);
    
    		this->work = new char[strlen(work) + 1];
    		strcpy(this->work, work);
    	}
    
    	~Person()
    	{
    		cout << "~Person()"<<endl;
    		if (this->name) {
    			cout << "name = "<<name<<endl;
    			delete this->name;
    		}
    		if (this->work) {
    			cout << "work = "<<work<<endl;
    			delete this->work;
    		}
    	}
    
    	void setName(char *n)
    	{
    		name = n;
    	}
    	int setAge(int a)
    	{
    		if (a < 0 || a > 150)
    		{
    			age = 0;
    			return -1;
    		}
    		age = a;
    		return 0;
    	}
    	void printInfo(void)
    	{
    		//printf("name = %s, age = %d, work = %s\n", name, age, work); 
    		cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
    	}
    };
    
    int main(int argc, char **argv)
    {
    	Person per("zhangsan", 18);
    	Person per2(per);
    
    	per2.printInfo();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    2、实现自己的值拷贝函数
    注意:实现过程实际就是自己写一个(传入参数是对象)重载构造函数

    Person(Person &per) 
    {
    	cout <<"Pserson(Person &)"<<endl;
    	this->age = per.age;
    
    	this->name = new char[strlen(per.name) + 1];
    	strcpy(this->name, per.name);
    
    	this->work = new char[strlen(per.work) + 1];
    	strcpy(this->work, per.work);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3、构造函数的顺序
    注意:
    ①按照运行中定义对象的顺序调用构造函数
    ②静态对象只调用一次构造函数
    ③全局对象在main函数执行前被调用
    ④局部对象在执行完会被析构函数销毁

    在这里插入图片描述

    #include 
    #include 
    #include 
    
    using namespace std;
    
    class Person {
    private:
    	char *name;
    	int age;
    	char *work;
    
    public:
    
    	Person() {//cout <<"Pserson()"<
    		name = NULL;
    		work = NULL;
    	}
    	Person(char *name) 
    	{
    		//cout <<"Pserson(char *)"<
    		this->name = new char[strlen(name) + 1];
    		strcpy(this->name, name);
    		this->work = NULL;
    	}
    
    	Person(char *name, int age, char *work = "none") 
    	{
    		cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
    		this->age = age;
    
    		this->name = new char[strlen(name) + 1];
    		strcpy(this->name, name);
    
    		this->work = new char[strlen(work) + 1];
    		strcpy(this->work, work);
    	}
    
    	Person(Person &per) 
    	{
    		cout <<"Pserson(Person &)"<<endl;
    		this->age = per.age;
    
    		this->name = new char[strlen(per.name) + 1];
    		strcpy(this->name, per.name);
    
    		this->work = new char[strlen(per.work) + 1];
    		strcpy(this->work, per.work);
    	}
    
    	~Person()
    	{
    		cout << "~Person()"<<endl;
    		if (this->name) {
    			cout << "name = "<<name<<endl;
    			delete this->name;
    		}
    		if (this->work) {
    			cout << "work = "<<work<<endl;
    			delete this->work;
    		}
    	}
    
    	void setName(char *n)
    	{
    		name = n;
    	}
    	int setAge(int a)
    	{
    		if (a < 0 || a > 150)
    		{
    			age = 0;
    			return -1;
    		}
    		age = a;
    		return 0;
    	}
    	void printInfo(void)
    	{
    		//printf("name = %s, age = %d, work = %s\n", name, age, work); 
    		cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
    	}
    };
    
    Person per_g("per_g", 10);
    
    void func()
    {
    	Person per_func("per_func", 11);  //函数执行完即使销毁了
    	static Person per_func_s("per_func_s", 11);
    }
    
    int main(int argc, char **argv)
    {
    	Person per_main("per_main", 11);
    	static Person per_main_s("per_main_s", 11);
    
    	for (int i = 0; i < 2; i++)
    	{
    		func();
    		Person per_for("per_for", i);
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    类里面嵌套其他对象(类似结构体中包含其他结构体)
    注意:
    ①当自己已经定义了一个带有参数的构造函数,那么系统将不再给你分配默认的无参数的构造函数,必须自己加上一个无参的构造函数
    ②析构函数的调用顺序和构造函数相反的
    ③下面例子可以完成在初始化学生类时候,一起把他包含的父母类也一起初始化了
    ④当学生类里面包含其他类的时候,学生类实例化的时候,构造函数的顺序是先父亲再母亲最后自己

    #include 
    #include 
    #include 
    
    using namespace std;
    
    class Person {
    private:
    	char *name;
    	int age;
    	char *work;
    
    public:
    
    	Person() {
    		cout <<"Pserson()"<<endl;
    		name = NULL;
    		work = NULL;
    	}
    	Person(char *name) 
    	{
    		//cout <<"Pserson(char *)"<
    		this->name = new char[strlen(name) + 1];
    		strcpy(this->name, name);
    		this->work = NULL;
    	}
    
    	Person(char *name, int age, char *work = "none") 
    	{
    		cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
    		this->age = age;
    
    		this->name = new char[strlen(name) + 1];
    		strcpy(this->name, name);
    
    		this->work = new char[strlen(work) + 1];
    		strcpy(this->work, work);
    	}
    
    	Person(Person &per) 
    	{
    		cout <<"Pserson(Person &)"<<endl;
    		this->age = per.age;
    
    		this->name = new char[strlen(per.name) + 1];
    		strcpy(this->name, per.name);
    
    		this->work = new char[strlen(per.work) + 1];
    		strcpy(this->work, per.work);
    	}
    
    	~Person()
    	{
    		cout << "~Person()"<<endl;
    		if (this->name) {
    			cout << "name = "<<name<<endl;
    			delete this->name;
    		}
    		if (this->work) {
    			cout << "work = "<<work<<endl;
    			delete this->work;
    		}
    	}
    
    	void setName(char *n)
    	{
    		name = n;
    	}
    	int setAge(int a)
    	{
    		if (a < 0 || a > 150)
    		{
    			age = 0;
    			return -1;
    		}
    		age = a;
    		return 0;
    	}
    	void printInfo(void)
    	{
    		//printf("name = %s, age = %d, work = %s\n", name, age, work); 
    		cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
    	}
    };
    
    class Student {
    private:
    	Person father;
    	Person mother;
    	int student_id;
    public:
    	Student()
    	{
    		cout<<"Student()"<<endl;
    	}
    
    	Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39) : mother(mother, mother_age), father(father, father_age)
    	{
    		cout<<"Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)"<<endl;
    	}
    
    	~Student()
    	{
    		cout<<"~Student()"<<endl;
    	}
    
    	
    };
    
    int main(int argc, char **argv)
    {
    	Student s(100, "bill", "lily");  
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
  • 相关阅读:
    【tio-websocket】10、单条TCP连接上下文—ChannelContext
    十三、泛型
    mysql主从同步
    python气象科研学习路线和常用技巧
    关于AbstractQueuedSynchronizer(JDK1.8)的一点理解.
    F5负载均衡上下行接口联动机制
    【zabbix监控三】zabbix之部署代理服务器
    13 套接字Socket
    【源码+名师讲解】Java游戏开发_Java飞机大战1.0进阶版_Java28个功能点能力提升必备_Java初级项目_Java练手项目_Java课程设计
    【华为OD题库-033】经典屏保-java
  • 原文地址:https://blog.csdn.net/weixin_50183638/article/details/126164101