构造函数和析构函数的基本语法

  • 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
  • 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。

构造函数

构造函数语法:类名(){ }

  1. 构造函数,没有返回值也不写void
  2. 函数名称与类名相同
  3. 构造函数可以有参数,因此可以发生重载
  4. 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次

析构函数

析构函数语法:~类名(){ }

  1. 析构函数,没有返回值也不写void
  2. 函数名称与类名相同,在名称前加上符号~
  3. 析构函数不可以有参数,因此不可以发生重载
  4. 程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
#include<iostream>
using namespace std;
class Person {
public:
    Person() {
        cout << "构造函数的调用" << endl;
    }

    ~Person() {
        cout << "析构函数的调用" << endl;
    }
};
int main()
{
    Person p1;
    return 0;
}

构造函数的分类和调用

按照有无参数可以分为有参构造和无参构造。
按照是否拷贝,分为拷贝构造和非拷贝构造(也就是普通的)。
无参构造不能写括号,否则编译器会认为声明了一个函数。不会创建对象

#include<iostream>
using namespace std;
class Person {
public:
    int age = 18;
    //无参构造
    Person() {
        cout << "构造函数1的调用" << endl;
    }
    //有参构造
    Person(int age) {
        this->age = age;
        cout << "构造函数2的调用" << endl;
    }
    //拷贝构造
    Person(const Person& p)
    {
        age = p.age;
        cout << "构造函数3的调用" << endl;
    }
    ~Person() {
        cout << "析构函数的调用" << endl;
    }
};
int main()
{
    //括号法调用
    //无参构造不能写括号
    //Person p1();
    //正常调用无参构造。
    Person p1;
    //调用有参构造。
    Person p2(12);
    //调用拷贝构造。
    Person p3(p1);


    //显式调用
    //Person(10) 匿名对象。这行代码结束就会销毁。
    Person p4 = Person(10);
    //上面的Person(10)是一个匿名对象,不要利用拷贝函数初始化匿名对象。编译器会认为是Person(p2)== Person p2;
    Person(p2);

    //隐式转化法,相当于Person p5=Person(10);
    Person p5 = 10;
    return 0;
}

拷贝构造函数的调用时机

C++中拷贝构造函数调用时机通常有三种情况

  1. 使用一个已经创建完毕的对象来初始化一个新对象
  2. 值传递的方式给函数参数传值
  3. 以值方式返回局部对象(被编译器优化了看不到)
#include<iostream>
using namespace std;
#include<cmath>
class Person {
public:
    int age = 0;
    Person() {
        cout << "无参构造函数" << endl;
    }
    Person(int age)
    {
        cout << "有参构造的调用" << endl;
        this->age = age;
    }
    Person(const Person& p) {
        this->age = p.age;
        cout << "拷贝构造的调用" << endl;
    }
    ~Person() {
        cout << "析构函数的调用" << endl;
    }
};
void test01() {
    Person p1(20);
    Person p2(p1);
}
void dowork(Person p) {

}
void test02() {
    Person p;
    dowork(p);
}
Person dowork2() {
    Person p1;
    cout << (int*)&p1 << endl;
    return p1;
}
void test03() {
    Person p = dowork2();
    cout << (int*)&p << endl;
}
int main()
{

    test01();
    cout << "------------------" << endl;
    test02();
    cout << "------------------" << endl;
    test03();
    return 0;
}

构造函数的调用规则

默认情况下,C++编译器至少给一个类添加3个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝

构造函数调用规则如下:

  • 如果用户定义有参构造函数,C++不在提供默认无参构造,但是会提供默认拷贝构造
  • 如果用户定义拷贝构造函数,C++不会再提供其他构造函数

    # include<iostream>
    using namespace std;
    class Person {
    public:
      int age = 0;
      Person() {
          cout << "默认构造" << endl;
      }
      Person(int age) {
          this->age = age;
          cout << "有参构造" << endl;
      }
      Person(const Person& p) {
          this->age = p.age;
          cout << "拷贝构造" << endl;
      }
      ~Person() {
          cout << "析构函数" << endl;
      }
    };
    int main()
    {
      Person p1(20);
      Person p2(p1);
      cout << p2.age << endl;
    }

深拷贝和浅拷贝

  • 浅拷贝:简单的赋值拷贝操作
  • 深拷贝:在堆区重新申请空间,进行拷贝操作
# include<iostream>
using namespace std;
#include<cmath>
class Person
{
public:
    Person() {
        cout << "默认构造函数" << endl;
    }
    Person(int age,int height) {
        this->age = age;
        this->height=new int(height);
        cout << "有参构造函数" << endl;
    }
    Person(const Person& p) {
        this->age = age;
        this->height = new int(*p.height);
    }
    int age = 0;
    int* height = 0;
    ~Person() {
        cout << "析构函数调用" << endl;
        if (height != NULL) {
            delete height;
            height = NULL;
        }
    }
};

void test01() {
    Person p1(18,160);
    cout << p1.age << endl;
    cout << *p1.height << endl;
    Person p2(p1);
    cout << p2.age << endl;
    cout << *p2.height << endl;
    
}
int main()
{
    test01();
}

如上代码,如果拷贝的时候没有手动创建空间进行拷贝,默认的拷贝函数会直接赋值,导致同一块内存区域被释放了两次,程序也就会报错。
总结:如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题

初始化列表

作用:C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(值1),属性2(值2)...{}

# include<iostream>
using namespace std;
#include<cmath>
class Person
{
public:
    int a;
    int b;
    int c;
    Person(int c, int d, int e) :a(c), b(d), c(e)
    {

    }
};


int main()
{
    Person p(30, 20, 10);
    cout << p.a << endl;
    cout << p.b << endl;
    cout << p.c << endl;

}

要注意冒号的位置,在函数括号的后面。

类对象作为成员

C++类中的成员可以是另一个类的对象,我们称该成员为对象成员

# include<iostream>
using namespace std;
#include<string>
#include<cmath>
class Phone {
public:
    Phone(string name) {
        Pname = name;
        cout << "Phone的构造函数" << endl;
    }
    ~Phone() {
        cout << "Phone的析构函数" << endl;
    }
    string Pname;
};
class Person {
public:
    Person(string nm, string Pname) :name(nm), phone(Pname) {
        cout << "Person的构造函数" << endl;
    }
    ~Person() {
        cout << "Person的析构函数" << endl;
    }
    string name;
    Phone phone;
};

int main()
{
    Person p("张三", "iphone17promax");
    cout << p.name << endl;
    cout << p.phone.Pname << endl;
}

其中Phone先构造,后析构,Person后构造,先析构。

静态成员

静态成员变量

  • 所有对象共享同一份数据
  • 在编译阶段分配内存
  • 类内声明,类外初始化

    #include<iostream>
    using namespace std;
    #include<iomanip>
    class Person {
    public:
      static int m_A;
    private:
      static int m_B;
    };
    int Person::m_B = 200;
    int Person::m_A = 100;
    void test()
    {
      Person p;
      cout << p.m_A << endl;
      Person p2;
      p2.m_A = 200;
      cout << p.m_A << endl;
    }
    void func() {
      Person p;
      cout << p.m_A << endl;
      cout << Person::m_A << endl;
      //cout << Person::m_B << endl;类外访问不到私有成员
    }
    int main()
    {
      func();
      return 0;
    }

    其中Person::m_A代表的是Person作用域下的m_A.

静态成员函数

  • 所有对象享同一个函数
  • 静态成员函数只能访问静态成员变量

    #include<iostream>
    using namespace std;
    #include<iomanip>
    class Person {
    public:
      int age;
      static int name;
      static void func() {
          name = 200;
          //age = 100;静态成员函数不能访问非静态成员变量
          cout << "static void func的调用" << endl;
      }
    };
    int Person::name = 100;
    void test01() {
      Person p;
      p.func();
      Person::func();
    }
    int main()
    {
      test01();
      return 0;
    }