4-构造函数与析构函数

♀尐吖头ヾ 提交于 2019-12-29 04:48:43

//返回值问题
char *Add(char a, char b)
{
//分配
char *p = new char[2];
//strcpy
strcpy(p, &a);

//不能使用+
return strcat(p, &b);
}

string Add(string a, string b)
{
//1.连接方式
//2.append
return a.append(b);
}

回顾:
1.类?方法?属性?
2.定义类
3.定义数据成员+成员函数
4.构建对象+初始化对象
-------------------------------------------------------------
构建对象的过程:调用构造函数
构造函数:初始化基本成员
调用方式:系统调用
定义形式:
类名(参数列表){函数语句};
可重载
可缺省参数
注意点:
1.函数名与类名一致
2.函数没有返回值
3.有一个默认的构造,如果你写了构造函数,默认的构造没有了
4.构造函数只能是public属性
5.不能够自己调用
析构函数:释放构建对象的过程动态申请内存
定义形式:
~类名()
不可以重载
没有参数
存在默认构造函数
注意点:
1.函数名与类型一致
2.函数没有返回值
3.没有参数
4.不能重载
5.对象作用结束后
拷贝构造函数:实现一个对象给另一个对象初始化 (默认的构造函数)
定义方式:
类名(对象的引用)
参数只有一个;

1.浅拷贝
1.1:默认的拷贝构造函数(相当于赋值)
1.2:不涉及动态内存分配的拷贝构造函数
2.深拷贝
2.1:涉及到指针
2.2: 涉及到指针的动态分配
类的组合:
1.构造函数的调用顺序:对象的构建次序
2.析构函数的调用顺序:对象的析构顺序

 1 //构造与析构.app
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 
 6 class GirlFriend
 7 {
 8 public:
 9     GirlFriend(){}
10     //GirlFriend(int x, int y)
11     //{
12     //    //形参和基本数据成员一致的时候 1.this 2.初始化参数列表
13     //    this->x = x;
14     //    this->y = y;
15     //}
16 
17     //初始化参数列表 --->构造函数独有
18     GirlFriend(int a, int b) :x(a), y(b){};
19     void show();
20 protected:
21     int x;
22     int y;
23 private:
24 };
25 
26 void GirlFriend::show()
27 {
28     cout << "GirlFriend.x=" << x << endl;
29     cout << "GirlFriend y=" << y << endl;
30 }
31 int main()
32 {
33     GirlFriend mGirlFriend;
34     GirlFriend IGirlFriend(1, 2);
35     mGirlFriend.show();
36     IGirlFriend.show();
37 
38     system("pause");
39     return 0;
40 }
 1 //析构函数.cpp
 2 #define _CRT_SECURE_NO_WARNINGS
 3 #include <iostream>
 4 #include <string>
 5 using namespace std;
 6 
 7 class BoyFriend
 8 {
 9 public:
10     BoyFriend(char *xname)
11     {
12         //坑注意点  动态分配多大空间
13         //1 strlen(xname)+1
14         name = new char[strlen(xname) + 1];
15         strcpy(name, xname);
16         cout << "name1:" << name << endl;
17     }
18 
19     void show()
20     {
21         cout << "name2:" << name << endl;
22     }
23     ~BoyFriend()
24     {
25         delete[]name;
26         cout << "析构被调用" << endl;
27     }
28 private:
29 
30 protected:
31     char *name;
32 
33 };
34 
35 
36 int main()
37 {
38     //{} 作用限定符
39     {
40         char name[] = "不销魂";
41         BoyFriend yourFriend(name);
42         yourFriend.show();
43     }
44 
45     system("pause");
46     return 0;
47 }
 1 //拷贝构造函数.cpp
 2 #include <iostream>
 3 #include<string>
 4 using namespace std;
 5 
 6 class yourfriend
 7 {
 8 public:
 9     int age;
10     char *name;
11     yourfriend(){};
12     yourfriend(int age, char *name):age(age), name(name){};
13     //拷贝构造函数
14     yourfriend(yourfriend &me)
15     {
16         age = me.age;
17         name = new char[10];
18         name = me.name;
19     }
20     void show()
21     {
22         cout << "age\t" << age << endl;
23         cout << "name\t" << name << endl;
24     }
25     ~yourfriend()
26     {
27         delete []name;
28         
29     }
30 };
31 
32 int main()
33 {
34     yourfriend me(12, "dabu");
35     yourfriend I(me);
36     I.show();
37     yourfriend your = I;
38     your.show();
39     system("pause");
40     return 0;
41 }
 1 //类的组合.cpp
 2 #include <iostream>
 3 using namespace std;
 4 //类的组合
 5 class A
 6 {
 7 public:
 8     int a;
 9     A(int a) :a(a)
10     {
11         cout << "\tA::A(a)" << endl;
12     }
13     void show()
14     {
15         cout << "\tA::a" << a << endl;
16     }
17     ~A()
18     {
19         cout << "\tA::~A()" << endl;
20     }
21 };
22 
23 class B
24 {
25 public:
26     int b;
27     B(int b) :b(b)
28     {
29         cout << "\tB::B(b)" << endl;
30     }
31     void show()
32     {
33         cout << "\tB::b" << b << endl;
34     }
35     ~B()
36     {
37         cout << "\tB::~B()" << endl;
38     }
39 };
40 
41 class C
42 {
43 public:
44     int c;
45     B b;  //先构建对象b
46     A a;  //在构建a
47     //注意点,以类的对象为基本成员的时,构造函数把自己成员初始化也要把组合类成员初始化
48     //初始化,构建过程
49     C(A a, B b, int c) :a(a), b(b), c(c)
50     {
51         cout << "\tC::C(a,b,c)" << endl;
52     }
53     void show()
54     {
55         cout << "\tA.a=" << a.a << endl;
56         cout << "\tB.b=" << b.b << endl;
57         cout << "\tC.c=" << c << endl;
58     }
59     ~C()
60     {
61         cout << "\tC::~C()" << endl;
62     }
63 };
64 
65 int main()
66 {
67     //一般写法:
68     //{
69         A a(1);
70         B b(2);
71         C c(a, b, 3);
72         c.show();
73     //}
74 
75     //直接构建
76     {
77     //1.对象的构建顺序只和声明顺序有关,有初始化参数列表无关
78     //2.和构造函数顺序相反   析构顺序
79     //C c(1, 2, 3);
80     ////二次析构是析构C中A,B
81     //c.show();
82     }
83 
84     system("pause");
85     return 0;
86 }
 1 //homework04.cpp
 2 #include <iostream>
 3 #include<string>
 4 #include<stdio.h>
 5 
 6 using namespace std;
 7 
 8 class student
 9 {
10 public:
11     char *name;
12     int age;
13     int fen;
14     char *sex;
15 
16     student(){};
17     student(char *n, int a, int f, char *s) :name(n), age(a), fen(f), sex(s){};
18     student(student &me)
19     {
20         age = me.age;
21         fen = me.fen;
22         name= new char[20];
23         sex = new char[6];
24         name = me.name;
25         sex = me.sex;
26         
27     }
28 
29     void input(/*char *n, int a, int f, char *s*/)
30     {
31         char *Name = new char[20];
32         cout << "请输入名字:";
33         cin >> Name;
34         name = Name;
35     
36         cout << "请输入年龄:";
37         cin >> age;
38 
39         cout << "请输入分数:";
40         cin >> fen;
41     /*    fflush(stdin);
42         getchar();*/
43         char *Sex = new char[20];
44         cout << "请输入性别:";
45         cin >> Sex;
46         sex = Sex;
47     
48     }
49     void show();
50 
51     /*~student()
52     {
53         delete[]name;
54         delete[]sex;    
55     }*/
56 protected:
57 
58 private:
59 };
60 
61 void student::show()
62 {
63     cout << "\t姓名 \t年龄 \t分数 \t性别" << endl;
64     cout << "\t" << name << "\t" << age << "\t" << fen << "\t" << sex << endl;
65 }
66 
67 int main()
68 {
69     {
70         student dabu("dabu", 25, 80, "man");
71         dabu.show();
72         cout << endl;
73 
74         student who;
75         student *pnew = &who;
76         pnew->input();
77         pnew->show();
78 
79     }
80 
81 
82 
83     
84     system("pause");
85     return 0;
86 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!