鱼C论坛

 找回密码
 立即注册
查看: 1297|回复: 0

[技术交流] C++旅程第六站------构造函数

[复制链接]
发表于 2020-5-9 22:35:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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、程序在调用对象销毁前自动调用析构,无须手动调用,而且只会调用一次。
  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. class Costume
  5. {
  6.         private:
  7.                 string kind;
  8.                 double price_f,price_n,discount;
  9.                 void setprice_n() {price_n = price_f * discount;}
  10.         
  11.         public:
  12.                 //构造函数:类对象被创建时,编译系统对象分配内存空间,并自动调用该构造函数->由构造函数完成成员的初始化工作
  13.                 //注意:构造函数的参数名不能跟类成员相同---因为构造函数的参数是赋给类成员的值.
  14.                  Costume(string k,double f,double d);//有参构造函数
  15.                                  Costume();//无参构造函数;
  16.                                  Costume(const Costume& p); //拷贝构造函数:就是使用同一类中之前创建的对象来初始化新创建的对象----跟复制一样
  17.                 //析构函数:在函数消亡时自动被调用,释放内存;在类名前加上'~'.
  18.                 ~Costume();
  19.                 void show();
  20. };

  21. Costume::Costume(string k,double f,double d)//有参构造函数
  22. {
  23.         cout << "The class of clothes : " << k << "!!!!!!" << endl;
  24.         
  25.         kind = k;
  26.         if(f < 0)
  27.         {
  28.                 cout << "Number of price_f is error,price_f set to 1.\n";
  29.                 price_f = 1.0;
  30.         }
  31.         else
  32.                 price_f = f;
  33.         discount = d;
  34.         setprice_n();
  35. }

  36. Costume::Costume()//无参构造函数
  37. {
  38.         kind = "none";
  39.         price_f = 1.0;
  40.         discount = 1.0;
  41.         setprice_n();
  42. }

  43. Costume::Costume(const Costume& p)//拷贝构造函数
  44. {
  45.         kind = p.kind;
  46.         price_f = p.price_f;
  47.         discount = p.discount;
  48.         setprice_n();
  49. }

  50. Costume::~Costume()
  51. {
  52.         cout << "------------Done-----------" << endl;
  53. }

  54. void Costume::show()
  55. {
  56.         cout << "------------Costume----------" << endl;
  57.         cout << "kind : " << kind << endl;
  58.         cout << "price : " << price_n << endl;
  59. }

  60. int main()
  61. {
  62.         cout << "--------------括号法调用构造函数--------------" << endl;
  63.         Costume cloth1;//默认构造函数调用---不要加(),编译器会认为是一个函数的声明,不会认为在创建对象
  64.         cloth1.show();
  65.         Costume cloth2("skirt",666.6,0.4);//有参构造函数调用
  66.         cloth2.show();
  67.         Costume cloth3(cloth2);//拷贝构造函数调用
  68.                 cloth3.show();
  69.                
  70.         cout << "---------------显示法调用构造函数---------------" << endl;
  71.         Costume shirt1;//默认构造函数调用
  72.         shirt1.show();
  73.         Costume shirt2 = Costume("agrag",354,4.4);//有参构造函数调用
  74.         shirt2.show();
  75.         Costume shirt3 = Costume(shirt2);//拷贝构造函数调用
  76.         shirt3.show();
  77.         
  78.         
  79.         Costume ("agarg",435.4,3.4);//匿名对象---当前行执行结束后,系统会立刻回收掉匿名对象
  80.                
  81.         return 0;
  82. }
复制代码

           深拷贝与浅拷贝
           浅拷贝:简单的赋值拷贝操作。 =
           深拷贝:在堆区重新申请空间,进行拷贝操作。  自己创建的 *-----一定要记得释放空间

  1. #include<iostream>

  2. using namespace std;

  3. class Car
  4. {
  5.         public:
  6.                 int m_time;
  7.                 int *m_size;
  8.                
  9.                 Car()
  10.                 {
  11.                         cout << "Car的默认构造函数调用!!!!" << endl;
  12.                 }
  13.                
  14.                 Car(int time,int size)
  15.                 {
  16.                         m_time = time;
  17.                         m_size = new int(size);
  18.                         cout << "Car的有参构造函数调用!!!!" << endl;
  19.                 }
  20.                
  21.                 ~Car()
  22.                 {
  23.                         //析构函数代码---将堆区开辟数据做释放操作
  24.                         if(m_size != NULL)
  25.                         {
  26.                                 delete m_size;
  27.                                 m_size = NULL;
  28.                         }
  29.                         cout << "Car的析构函数调用!!!!" << endl;
  30.                 }
  31.                
  32.                 Car(const Car &h)
  33.                 {
  34.                         cout << "Car拷贝构造函数调用!!!!" << endl;
  35.                         //浅拷贝操作
  36.                         m_time = h.m_time;
  37.                         //深拷贝操作
  38.                         m_size = new int(*h.m_size);
  39.                 }
  40.                
  41. };

  42. int main()
  43. {
  44.         Car c1(22,78);
  45.         cout << "c1汽车的使用时长是: " << c1.m_time << "汽车的质量为: " << *c1.m_size << endl;
  46.        
  47.         Car c2(c1);
  48.         cout << "c2汽车的使用时长是: " << c2.m_time << "汽车的质量为: " << *c2.m_size << endl;
  49.        
  50.         return 0;
  51. }
复制代码


          初始化列表
  1. #include<iostream>

  2. using namespace std;

  3. class Door
  4. {
  5.         private:
  6.                 int m_A;
  7.                 int m_B;
  8.                 int m_C;
  9.                
  10.         public:
  11.                 //初始化列表--构造函数():属性1(值1),属性2(值2)......{}
  12.                 Door(int a,int b,int c):m_A(a), m_B(b), m_C(c){}
  13.                
  14.                 void Print()
  15.                 {
  16.                         cout << "long : " << m_A << endl;
  17.                         cout << "width : " << m_B << endl;
  18.                         cout << "high : " << m_C << endl;
  19.                 }
  20. };

  21. int main()
  22. {
  23.         Door p(64,46,57);
  24.         p.Print();
  25.        
  26.         return 0;
  27. }
复制代码
        C++类中的成员可以是另一个类的对象,我们称该成员为对象成员。
  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. class Bag
  5. {
  6.         public:
  7.                 string m_color;
  8.                
  9.                 Bag(string color)
  10.                 {
  11.                         cout << "Bag的构造函数调用!!!" << endl;
  12.                         m_color = color;
  13.                 }
  14.                
  15.                 ~Bag()
  16.                 {
  17.                         cout << "Bag的析构函数调用!!!!" << endl;
  18.                 }
  19. };

  20. class Student
  21. {
  22.         public:
  23.                 string m_name;
  24.                 Bag m_type;//Bag类中对象作为Student类中的成员
  25.                
  26.                 Student(string name,string color) : m_name(name), m_type(color)
  27.                 {
  28.                         cout << "Student的构造函数调用!!!!" << endl;
  29.                 }
  30.                
  31.                 ~Student()
  32.                 {
  33.                         cout << "Student的析构函数调用!!!!" << endl;
  34.                 }
  35. };

  36. //当其他类对象作为本类成员,构造时候先调用构造类对象,在构造自身,析构的顺序与构造相反。
  37. int main()
  38. {
  39.         Student p("李明","蓝色");
  40.         cout << p.m_name << "拥有一个" << p.m_type.m_color << "的书包!!!" << endl;
  41.        
  42.         return 0;
  43. }
复制代码

                                                渣渣一个望各位大佬赐教









小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 20:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表