2-6:构造函数与类成员的初始化

1、构造函数一大功能就是初始化成员变量
  (1)默认构造函数不带参,无初始化功能
  (2)若无其他构造函数,则默认构造函数可以省略。但若有哪怕1个其他构造函数,则默认构造函数不能省,必须写上。
  (3)栈上分配对象时,若使用默认构造函数,则对象变量后面不加空的(),若用带参构造才需要加(初始化参数)


2、C++的成员初始化列表
  (1)一般用于带参构造函数中,用来给属性传参赋值
  (2)成员初始化列表和构造函数之间用冒号间隔,多个列表项之间用逗号间隔
  (3)初始化列表可以替代构造函数内的赋值语句,达到同样效果


3、构造函数使用参数默认值
  (1)class声明时可以给函数形参赋值一个默认值,实际调用时若不传参就使用默认值
  (2)方法实现时形参可以不写默认值,但是实际是按照声明时的默认值规则的
  (3)有默认值情况,要注意实际调用不能有重载歧义,否则编译不能通过
  (4)所有参数都带默认值的构造函数,1个可以顶多个构造函数(举例说明)

person.hpp

 1 #ifndef __PERSON_H__
 2 #define __PERSON_H__
 3 
 4 #include <string>
 5 using namespace std;
 6 
 7 namespace MAN
 8 {
 9 
10 
11     // 声明这个类
12     class person
13     {
14         // 访问权限
15     public:
16         // 属性
17         string name;            // 名字
18         int age;                // 年龄
19         bool male;                // 性别,男为true,女为false
20         int* pInt;                // 只是分配了p本身的4字节内存,并没有分配p指向的空间内存
21 
22         // 构造和析构
23         person();                // 默认构造函数
24         //person(string myname);    // 自定义构造函数
25         person(string myname = "tianyu", int myage = 20, bool mymale = false);
26 
27         ~person();                // 默认析构函数
28 
29         // 方法
30         void eat(void);
31         void work(void);
32         void sleep(void);
33         void print(void);
34 
35     private:
36 
37     };
38 
39 }            // end of namespace MAN
40 
41 #endif

person.cpp

#include "person.hpp"
#include <iostream>

using namespace std;

// class的成员函数中可以引用class的成员变量,但是要考虑public和private这些
void MAN::person::eat(void)
{
    cout << name << " eat" << endl;
}

void MAN::person::work(void)
{
    if (this->male)
    {
        cout << this->name << " coding" << endl;
    }
    else
    {
        cout << this->name << " shopping" << endl;
    }
}


void MAN::person::sleep(void)
{
    cout << this->name << " sleep" << endl;
}

//打印出对象的成员的值
void MAN::person::print(void)
{
    cout << "name = " << name << endl;
    cout << "age  = " << age  << endl;
    cout << "male = " << male << endl;
}

MAN::person::person()
{
    // 默认构造函数是空的
    cout << "default constructor" << endl;
}

/*
MAN::person::person(string myname):name(myname)
{
    // 默认构造函数是空的
    //this->name = name;            // 构造对象后,同时对对象中的name属性进行初始化

    // 在构造函数中对class中需要分配动态内存的指针进行动态分配
//    this->pInt = new int(55);
    this->pInt = new int[10];        // 分配了10个元素的数组

    cout << "userdefined constructor" << endl;
}
*/

MAN::person::person(string myname, int myage, bool mymale) :name(myname), age(myage), male(mymale)
{
    this->pInt = new int[10];

    cout << "userdefined constructor" << endl;

}

MAN::person::~person()
{
    // 默认析构函数是空的
//    delete this->pInt;                // 释放单个内存
    delete[] this->pInt;            // 释放数组内存

    cout << "userdefined destructor" << endl;
}

main.cpp

#include "person.hpp"

using namespace MAN;


int main(void)
{
    // 人的一天的生活
    string s1 = "linux";
    person pPerson(s1);            // 创建了一个person的对象,分配在栈上

    return 0;
}
原文地址:https://www.cnblogs.com/y4247464/p/13747858.html