|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 这是她 于 2020-5-9 22:35 编辑
While there is life there is hope.
默认情况下,C++编译器至少给一个类添加3个函数
1、默认构造函数(无参,函数体为空)。
2、默认析构函数(无参,函数体为空)。
3、默认拷贝构造函数,对属性进行值拷贝。
构造函数调用规则如下:
1、如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造。
2、如果用户定义拷贝构造函数,C++不再会提供其他构造函数。
构造函数和析构函数
构造函数和析构函数都是必须要有的,如果我们自己不提供,编译器会提供一个空的实现的构造和析构;
构造函数: 类名()()
1、没有返回值也不写void;
2、函数名称与类名相同;
3、可以有参数,因此可以发生重载;
4、程序在调用对象的时候会自动调用构造,无须手动调用,而且只会调用一次。
析构函数: ~类名()()
1、没有返回值,也不写void;
2、函数名称与类名相同,在名称前面加上符号~;
3、不可以有参数,因此不可以重载;
4、程序在调用对象销毁前自动调用析构,无须手动调用,而且只会调用一次。
- #include<iostream>
- #include<string>
- using namespace std;
- class Costume
- {
- private:
- string kind;
- double price_f,price_n,discount;
- void setprice_n() {price_n = price_f * discount;}
-
- public:
- //构造函数:类对象被创建时,编译系统对象分配内存空间,并自动调用该构造函数->由构造函数完成成员的初始化工作
- //注意:构造函数的参数名不能跟类成员相同---因为构造函数的参数是赋给类成员的值.
- Costume(string k,double f,double d);//有参构造函数
- Costume();//无参构造函数;
- Costume(const Costume& p); //拷贝构造函数:就是使用同一类中之前创建的对象来初始化新创建的对象----跟复制一样
- //析构函数:在函数消亡时自动被调用,释放内存;在类名前加上'~'.
- ~Costume();
- void show();
- };
- Costume::Costume(string k,double f,double d)//有参构造函数
- {
- cout << "The class of clothes : " << k << "!!!!!!" << endl;
-
- kind = k;
- if(f < 0)
- {
- cout << "Number of price_f is error,price_f set to 1.\n";
- price_f = 1.0;
- }
- else
- price_f = f;
- discount = d;
- setprice_n();
- }
- Costume::Costume()//无参构造函数
- {
- kind = "none";
- price_f = 1.0;
- discount = 1.0;
- setprice_n();
- }
- Costume::Costume(const Costume& p)//拷贝构造函数
- {
- kind = p.kind;
- price_f = p.price_f;
- discount = p.discount;
- setprice_n();
- }
- Costume::~Costume()
- {
- cout << "------------Done-----------" << endl;
- }
- void Costume::show()
- {
- cout << "------------Costume----------" << endl;
- cout << "kind : " << kind << endl;
- cout << "price : " << price_n << endl;
- }
- int main()
- {
- cout << "--------------括号法调用构造函数--------------" << endl;
- Costume cloth1;//默认构造函数调用---不要加(),编译器会认为是一个函数的声明,不会认为在创建对象
- cloth1.show();
- Costume cloth2("skirt",666.6,0.4);//有参构造函数调用
- cloth2.show();
- Costume cloth3(cloth2);//拷贝构造函数调用
- cloth3.show();
-
- cout << "---------------显示法调用构造函数---------------" << endl;
- Costume shirt1;//默认构造函数调用
- shirt1.show();
- Costume shirt2 = Costume("agrag",354,4.4);//有参构造函数调用
- shirt2.show();
- Costume shirt3 = Costume(shirt2);//拷贝构造函数调用
- shirt3.show();
-
-
- Costume ("agarg",435.4,3.4);//匿名对象---当前行执行结束后,系统会立刻回收掉匿名对象
-
- return 0;
- }
复制代码
深拷贝与浅拷贝
浅拷贝:简单的赋值拷贝操作。 =
深拷贝:在堆区重新申请空间,进行拷贝操作。 自己创建的 *-----一定要记得释放空间
- #include<iostream>
- using namespace std;
- class Car
- {
- public:
- int m_time;
- int *m_size;
-
- Car()
- {
- cout << "Car的默认构造函数调用!!!!" << endl;
- }
-
- Car(int time,int size)
- {
- m_time = time;
- m_size = new int(size);
- cout << "Car的有参构造函数调用!!!!" << endl;
- }
-
- ~Car()
- {
- //析构函数代码---将堆区开辟数据做释放操作
- if(m_size != NULL)
- {
- delete m_size;
- m_size = NULL;
- }
- cout << "Car的析构函数调用!!!!" << endl;
- }
-
- Car(const Car &h)
- {
- cout << "Car拷贝构造函数调用!!!!" << endl;
- //浅拷贝操作
- m_time = h.m_time;
- //深拷贝操作
- m_size = new int(*h.m_size);
- }
-
- };
- int main()
- {
- Car c1(22,78);
- cout << "c1汽车的使用时长是: " << c1.m_time << "汽车的质量为: " << *c1.m_size << endl;
-
- Car c2(c1);
- cout << "c2汽车的使用时长是: " << c2.m_time << "汽车的质量为: " << *c2.m_size << endl;
-
- return 0;
- }
复制代码
初始化列表
- #include<iostream>
- using namespace std;
- class Door
- {
- private:
- int m_A;
- int m_B;
- int m_C;
-
- public:
- //初始化列表--构造函数():属性1(值1),属性2(值2)......{}
- Door(int a,int b,int c):m_A(a), m_B(b), m_C(c){}
-
- void Print()
- {
- cout << "long : " << m_A << endl;
- cout << "width : " << m_B << endl;
- cout << "high : " << m_C << endl;
- }
- };
- int main()
- {
- Door p(64,46,57);
- p.Print();
-
- return 0;
- }
复制代码 C++类中的成员可以是另一个类的对象,我们称该成员为对象成员。
- #include<iostream>
- #include<string>
- using namespace std;
- class Bag
- {
- public:
- string m_color;
-
- Bag(string color)
- {
- cout << "Bag的构造函数调用!!!" << endl;
- m_color = color;
- }
-
- ~Bag()
- {
- cout << "Bag的析构函数调用!!!!" << endl;
- }
- };
- class Student
- {
- public:
- string m_name;
- Bag m_type;//Bag类中对象作为Student类中的成员
-
- Student(string name,string color) : m_name(name), m_type(color)
- {
- cout << "Student的构造函数调用!!!!" << endl;
- }
-
- ~Student()
- {
- cout << "Student的析构函数调用!!!!" << endl;
- }
- };
- //当其他类对象作为本类成员,构造时候先调用构造类对象,在构造自身,析构的顺序与构造相反。
- int main()
- {
- Student p("李明","蓝色");
- cout << p.m_name << "拥有一个" << p.m_type.m_color << "的书包!!!" << endl;
-
- return 0;
- }
复制代码
渣渣一个 望各位大佬赐教
|
|