黑马程序员匠心之作-4.2对象的初始化和清理

目录:

4.2.1构造函数和析构函数 

4.2.2构造函数的分类和调用 

#include <iostream>
#include<string>
#include<ctime>
using namespace std;
class Person
{
    //构造函数分类:有参构造  无参构造
    //            普通构造  拷贝构造
public:

    Person()
    {
        cout << "调用无参构造函数" << endl;
    }
    Person(int a)
    {
        age = a;
        
        cout << "调用有参构造函数" << endl;
    }
    //拷贝构造函数
    Person(const Person& P)
    {
        //将传入的person的属性拷贝到目前的person的属性里面;
        age = P.age;
        cout << "调用拷贝构造函数" << endl;
    }
    ~Person()
    {
        cout << "调用析构函数" << endl;
    }
    int age;
};
void test()
{
    //调用方法

    //括号法
    Person P1;//默认调用方法;
    //注意事项:在使用默认调用方法时,不能加(),编译器会默认是函数的声明
    //Person p1();//错误写法,认为是一个返回值为Person类型,函数名为p1的函数声明;相当于void t1();

    Person P2(10);//有参构造
    Person P3(P2);//拷贝构造
    cout << "P2年龄:" << P2.age << endl;
    cout << "P3年龄:" << P3.age << endl;
    //显示法
    Person P4 = Person(10);//显示调用有参构造
   
    //Person(10);//匿名对象,特点,当前行结束之后系统会自动收回匿名对象;
    //注意事项:不要用拷贝构造初始化匿名对象
    //Person(P3);//编译器认为Person(P3)相当于Person P3,与之前的命名相同
    Person P5 = Person(P4);//显示调用拷贝构造 

    //隐士转换法
    Person P6 = 10;//相当于 Person P5 = Person(10);
    Person P7 = P6;
     

}
int main()
{
    test();
    system("pause");
    return 0;
}

 4.2.3何时调用拷贝函数

 

#include <iostream>
#include<string>
#include<ctime>
using namespace std;
class Person
{
    //构造函数分类:有参构造  无参构造
    //            普通构造  拷贝构造
public:

    Person()
    {
        cout << "调用无参构造函数" << endl;
    }
    Person(int a)
    {
        age = a;
        
        cout << "调用有参构造函数" << endl;
    }
    //拷贝构造函数
    Person(const Person& P)
    {
        //将传入的person的属性拷贝到目前的person的属性里面;
        age = P.age;
        cout << "调用拷贝构造函数" << endl;
    }
    ~Person()
    {
        cout << "调用析构函数" << endl;
    }
    int age;
};
void test01()//使用一个创建完毕的对象初始化另外一个新的对象
{
    
    Person P1;//默认调用方法;
    Person P2(P1);//拷贝构造
   
}
void diaoyong(Person p)
{

}
void test02()//值传递的方式给函数参数传值
{
    Person p;
    diaoyong(p);
}
Person work()
{
    Person p;
    return p;
}
void test03()//以值方式返回局部对象
{
    Person P = work();
}
int main()
{
    test01();
    test02();
    test03();
    system("pause");
    return 0;
}

4.2.4构造函数调用规则

 

 4.2.5深拷贝与浅拷贝

重点:

1、浅拷贝:编译器提供的等号复制;会产生堆区数据重复释放问题

     深拷贝:自己重新开辟堆区进行值得传递;解决浅拷贝问题

2、析构函数代码可以用来释放堆区数据。

 

 浅拷贝带来的问题:堆区的数据重复释放,栈区数据特点,现金后出,P2先释放掉了地址为0x0011这个地址的数据,P1再来释放的时候,出现了堆区地址重复释放的问题。

#include <iostream>
#include<string>
#include<ctime>
using namespace std;
class Person
{
  
public:

    Person()
    {
        cout << "调用无参构造函数" << endl;
    }
    Person(int a,int height)
    {
        age = a;
        m_height = new int(height);
        cout << "调用有参构造函数" << endl;
    }
    //拷贝构造函数
    Person(const Person& P)
    {
        //将传入的person的属性拷贝到目前的person的属性里面;
        age = P.age;
       // m_height = P.m_height;//浅拷贝操作:编译器自带的拷贝函数写法,会产生堆区重复释放的问题,
        //深拷贝操作:自己开辟一个新的堆区进行身高的值传递
        m_height = new int(*P.m_height);
        cout << "调用拷贝构造函数" << endl;
    }
    ~Person()
    {
        //析构代码,将堆区开辟的数据释放
        if (m_height != NULL)
        {
            delete m_height;
            m_height = NULL;
        }
        cout << "调用析构函数" << endl;
    }
    int age;
    int* m_height;
};
void test01()
{
    
    Person P1(18,167);
    cout << "P1的年龄:" << P1.age << " P1的身高:" << *P1.m_height << endl;
    Person P2(P1);//拷贝构造
    cout << "P2的年龄:" << P2.age << " P2的身高:" << *P2.m_height << endl;
}


int main()
{
    test01();
    
    system("pause");
    return 0;
}

4.2.6初始化列表

#include <iostream>
#include<string>
#include<ctime>
using namespace std;
//传统的初始化方法
//class Person
//{
//  
//public:
//
//   
//    Person(int a,int b)
//    {
//        m_a = a;
//        m_b = b;
//    }
//    
//    int m_a;
//    int m_b;
//};
//列表初始化方法;
class Person
{

public:


    Person(int a, int b):m_a(a),m_b(b)
       
    {
       
    }

    int m_a;
    int m_b;
};
void test01()
{
    
    Person P1(18,167);
    cout << "a:" << P1.m_a<< " b:" << P1.m_b << endl;
    
}


int main()
{
    test01();
    
    system("pause");
    return 0;
}

4.2.7类对象作为类成员 

 

 4.2.8静态成员

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

class Person
{

public:
    //静态成员函数
    static void func()
    {
        a = 100;//静态成员函数可以访问静态成员变量,静态成员a是共享的不属于某一个对象
        //b = 200;//错误,静态成员函数不能访问静态成员变量。非静态成员b不是共享的,
        cout << "static void 调用" << endl;
    }
    
    static int a;
    //int b;

private://静态成员函数同样有访问权限;若设置为私有,则类外仍然不能访问到;
    static void func2()
    {
       
    }
};
int Person::a = 100;
void test01()
{
    //静态成员函数的两种访问方式
    //1通过对象访问
    Person P;
    P.func();
    //通过类名访问
    Person::func();
}


int main()
{
    test01();
    
    system("pause");
    return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值