-seventy-
Welcome to my blog space.

构造函数

/*
    构造函数:
        1.是一种特殊的成员函数
        主要用来在创建对象时初始化对象,为对象的成员变量赋予初始值

        2.构造函数名和类名相同
        构造函数没有返回值类型,也没有返回值

        3.构造函数可以重载,需要满足函数重载的条件

        4.在创建一个新的对象的时候,会自动调用
        如果一个类中没有显式地给出构造函数,系统会自动的给出一个构造函数

        5.学会利用“成员初始化列表”,给成员初始化
*/
#include <iostream>
#include <string>
using namespace std;

class MyClass
{
public:
    int id;

private:
    int age;

public:
    MyClass();        // 无参构造函数
    MyClass(int n, int m);    // 含参构造函数
};

MyClass::MyClass()
{
    cout << "999" << endl;
}

MyClass::MyClass(int n, int m)
{
    cout << id << endl;
}



int main()
{
    MyClass obj_0;    // obj_0 或者 obj_0()    都能激活无参构造函数
    MyClass obj_1(5, 18);    // 实例化对象的同时,激活有参构造函数

    return 0;
}
#include <iostream>
#include <string>
using namespace std;

class MyClass
{
public:
    const int id;// 注意!与C语言不同,在 C++ 中,const 是将被修饰的东西变成常量,即:不可改变
    const int num;

public:
    MyClass(int i) :id(i),num(5)    // 成员初始化列表
    {

    }

};


int main()
{
    MyClass obj(3);
    cout << obj.id << endl;
    cout << obj.num << endl;

    return 0;
}

析构函数

/*
    析构函数:
    1.是一种特殊的函数,主要作用是在对象生命周期结束时进行清理,系统可以自动调用析构函数
    2.析构函数可以主动通过对象调用,析构函数必须是公有属性下,在对象生命周期结束时会自动调用析构函数
    3.不是因为调用了析构函数导致生命周期结束,而是因为生命周期结束时会自动调用析构函数首先
*/
#include <iostream>
#include <string>
using namespace std;

class MyClass
{
public:
    MyClass();
    ~MyClass();    // 声明:析构函数

};
MyClass::MyClass()
{

}
MyClass::~MyClass()    // 析构函数
{
    cout << "析构" << endl;
}

int main()
{
    MyClass obj;    // 对象obj生命周期结束时,系统会自动调用析构函数

    // 对于堆区里面的内存空间,因为需要自己申请,自己释放,所以没释放前,析构函数不会执行
    MyClass* p = new MyClass;
    delete p;
    p = NULL;

    return 0;
}

拷贝构造 [点击]

/*
    拷贝构造:
        1.拷贝构造是一种特殊的构造函数,通过“拷贝构造”函数完成一个复制的过程。
        2.特殊:参数是本类的对象的引用。
        3.先是构造函数,才有可能是拷贝构造函数。没写的话,系统给,将成员一一对应赋值,也可以自定义。
*/


#include <iostream>
#include <string>
using namespace std;

class MyClass
{
    MyClass(){}
    ~MyClass(){}
    MyClass(const MyClass& obj){}    // 拷贝构造函数的第一个参数是当前类的对象的引用
    MyClass(const MyClass& obj, int n){}
    MyClass(const MyClass& obj, int n,int m){}
};


int main()
{
    return 0;
}
/*
    调用时机:
        1.使用一个对象给另一个对象进行初始化
        2.使用一个对象构造另一个对象
        3.函数的参数是类的对象
        4.函数的返回值是类的对象
*/

#include <iostream>
#include <string>
using namespace std;

class Human
{
public:
    string m_name;
    int m_age;

public:
    void showInfo();
    // 下面的函数即使不写,也会默认存在
    Human();    // 构造函数
    Human(Human& obj);    // 拷贝构造函数
    ~Human();    // 析构

};

void Human::showInfo()
{
    cout << m_name << endl;
    cout << m_age << endl;
}

Human::Human()
{
    m_name = "KiKi[默认名]";
    m_age = 0;
}

Human::Human(Human& obj)
{
    m_name = obj.m_name;
    m_age = obj.m_age;
    cout << "执行了 Human(Human& obj) 函数" << endl;
}

Human::~Human()
{

}

void func_1(Human obj)    // 函数的参数是类的对象
{

}

Human func_2()
{
    Human obj;
    return obj;        // 函数的返回值是类的对象
}

int main()
{
    Human obj_0;
    obj_0.showInfo();

    cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";

    obj_0.m_name = "小明";
    obj_0.m_age = 28;
    obj_0.showInfo();

    cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";

    Human obj_1(obj_0);        // 等价于 Human obj_1 = obj_0;
    obj_1.showInfo();

    cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";

    // 3.函数的参数是类的对象
    // 4.函数的返回值是类的对象
    func_1(obj_1);
    func_2();

    return 0;
}

/*
    浅拷贝与深拷贝:
        1.浅拷贝:默认的都是浅拷贝
        2.浅拷贝,无脑抄袭
        3.深拷贝:需要自己根据实际情况实现
        4.深拷贝:有点脑子
*/

 

posted on 2023-07-18 17:12  -seventy-  阅读(10)  评论(0)    收藏  举报