构造函数的作用为初始化对象的数据成员
C++ 中调用不带参数的构造函数必须按照下面这样来定义,后面不可以带();
带()会误识别为函数的声明。

Class mclass;

代码清单5.1
  1. #include <iostream>
  2. using namespace std;
  3. class Person
  4. {
  5. private:
  6. char *name;
  7. int age;
  8. char *work;
  9. public:
  10. Person() { cout << "Pserson()" << endl; }
  11. Person(char *name)
  12. {
  13. cout << "Pserson(char *)" << endl;
  14. this->name = name;
  15. }
  16. Person(char *name, int age, char *work = "none")
  17. {
  18. cout << "Pserson(char*, int)" << endl;
  19. this->name = name;
  20. this->age = age;
  21. this->work = work;
  22. }
  23. void setName(char *n)
  24. {
  25. name = n;
  26. }
  27. int setAge(int a)
  28. {
  29. if (a < 0 || a > 150)
  30. {
  31. age = 0;
  32. return -1;
  33. }
  34. age = a;
  35. return 0;
  36. }
  37. void printInfo(void)
  38. {
  39. cout << "name = " << name << ", age = " << age << ", work = " << work << endl;
  40. }
  41. };
  42. int main(int argc, char **argv)
  43. {
  44. Person per("zhangsan", 16);
  45. Person per2;
  46. per.printInfo();
  47. return 0;
  48. }

其中

Person(char *name, int age, char *work = "none")
    {
        cout << "Pserson(char*, int)" << endl;
        this->name = name;
        this->age = age;
        this->work = work;
    }

给了work这个成员一个默认参数。

代码清单5.2 通过new来创建类对象
  • new创建类对象需要指针接收,一处初始化,多处使用
  • new创建类对象使用完需delete销毁
  • new创建对象直接使用堆空间,而局部不用new定义类对象则使用栈空间
  • new对象指针用途广泛,比如作为函数返回值、函数参数等
  • 频繁调用场合并不适合new,就像new申请和释放内存一样
    int main(int argc, char **argv)
    {
      Person per("zhangsan", 16);
      Person per2;
      Person per3();
      Person *per4 = new Person;
      Person *per5 = new Person();
      Person *per6 = new Person[2];
      Person *per7 = new Person("lisi", 18, "student");
      Person *per8 = new Person("wangwu", 18);
      per.printInfo();
      per7->printInfo();
      per8->printInfo();
      delete per4;
      delete per5;
      delete []per6;
      delete per7;
      delete per8;
      return 0;
    }
    
    代码清单5.3 析构函数回收内存HEAP
    ```cpp

    include

    include

    include

    using namespace std; class Person { private: char name; int age; char work;

public: Person() { name = NULL; work = NULL; } Person(char *name) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->work = NULL; }

Person(char *name, int age, char *work = "none")
{
    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()
{
    if (this->name)
        delete this->name;
    if (this->work)
        delete this->work;
}

}

C++提供了一种快速释放内存的方法:析构函数。在构造函数中创建的HEAP,可以在析构函数中自动释放。<br />构造函数允许有多个,但析构函数只能有一个。且不带任何参数。
<a name="5f5c8443"></a>
###### 代码清单5.4 拷贝构造函数
默认的拷贝构造函数,会进行值拷贝,如下代码可以发现per2的数据成员的值与per完全一致。
> Class m_class1;
> Class m_class2(m_class1);

```cpp
...

int main(int argc, char **argv)
{
    Person per("zhangsan", 18);
    Person per2(per);
    per2.printInfo();
    return 0;
}

但是这样一来会存在一个非常致命的问题,两个类的数据成员指向了同一块内存,那么在析构时,这块内存会有可能析构两次。更致命的是,被拷贝的对象对原来的对象产生了依赖关系,如果原来对象被单独销毁,那么被拷贝的对象就会出现野指针。因此可以为拷贝构造函数创建一个构造函数, 为类的数据成员分配单独的内存。这种单独分配内存的操作又叫深拷贝。而不去分配内存的方式被称为浅拷贝。
深拷贝和浅拷贝的定义可以简单理解成:如果一个类拥有资源(堆,或者是其它系统资源),当这个类的对象发生复制过程的时候,这个过程就可以叫做深拷贝,反之对象存在资源,但复制过程并未复制资源的情况视为浅拷贝。
浅拷贝资源后在释放资源的时候会产生资源归属不清的情况导致程序运行出错。

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);
}

如果一旦定义了有参数构造函数,必须首先定义无参函数

代码清单5.4 构造函数调用构造函数
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;
}

运行结果

Pserson(char, int), name = bill, age= 40 Pserson(char, int), name = lily, age= 39 Student(int id, char _father, char _mother, int father_age = 40, int mother_age = 39) Person() name = lily work = none ~Person() name = bill work = none\

先调用成员的构造函数


构造顺序

  • 先基类后派生类
  • 先虚拟基类,后一般基类
  • 先对象类成员后自身成员