C++(05)

在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;

class Person
{
public:
    Person()
    {
       cout << "Person构造函数的调用" << endl;
    }

    ~Person()
    {
        cout << "Person析构函数调用" << endl;
    }
};

void test()
{
    Person p;  //在栈上的数据,test()执行完毕,释放这个对象

}
int main()
{
    //test();
    Person p;
    return 0;
}

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;


//构造函数分类
// 按照参数分类   无参构造(默认构造)和有参构造
// 按照类型分类   普通构造和拷贝构造

class Person
{

public:
    Person()
    {
        cout << "Person无参构造函数调用" << endl;
    }
    Person(int a)
    {
        age = a;
        cout << "Person有参构造函数调用" << endl;
    }
    //拷贝构造
    Person(const Person &p)
    {
        //将传入的人的属性拷贝到我身上
        age  = p.age;
        cout << "Person拷贝构造函数调用" << endl;
    }
    ~Person()
    {
        cout << "Person析构函数调用" << endl;
    }
    int age;
};
//调用
void test()
{
    /*
    //1.括号法
    Person p1;   //默认构造函数的调用
    Person p2(10);  //有参构造函数
    Person p3(p2);  // 拷贝构造函数
    //注意事项
    //调用默认构造函数,不能加(),否则编译器会认为是函数的声明,不会认为在创建对象

    cout << "P2的年龄为:" << p2.age << endl;
    cout << "P3的年龄为:" << p3.age << endl;
    */
    //2.显示法
    Person p1;   //无参构造
    Person p2 = Person(10);   //有参构造
    Person p3 = Person(p2);    //拷贝构造

    Person(10);  //匿名对象  特点:当前行结束后,系统会立即回收匿名对象
    //注意事项
    //不要用拷贝构造函数,初始化匿名对象, 编译器会认为:Person(p3) == Person p3;
    //Person(p3);

    //3.隐式转换法
    Person p4 = 10;   //相当于Person p2 = Person(10);  :有参构造
    Person p5 = p4;    //拷贝构造
}
int main()
{
    test();
    return 0;
}

在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;

class Person
{

public:
    Person()
    {
        cout << "Person默认构造函数调用" << endl;
    }

    Person(int a)
    {
        m_Age = a;
        cout << "Person有参构造函数调用" << endl;
    }

    Person(const Person &p)
    {
        m_Age = p.m_Age;
        cout << "Person拷贝构造函数调用" << endl;
    }
    ~Person()
    {
        cout << "Person析构函数调用" << endl;
    }
    int m_Age;
};

//1.使用一个已经创建完毕的对象来初始化一个新对象
void test1()
{
    Person p1(20);
    Person p2(p1);

    cout << "p2的年龄:" << p2.m_Age << endl;
}

//2.值传递的方式给函数参数传值
void doWork(Person p)
{

}
void test2()
{
    Person p;
    doWork(p);
}

//3.值方式返回局部对象
Person doWork1()
{
    Person p1;
    return p1;
}
void test3()
{
    Person p = doWork1();
}

int main()
{
    //test1();
    //test2();
    test3();
    return 0;
}

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;


class Person
{
    public:
    /*Person()
    {
        cout << "Person的默认构造函数" << endl;
    }*/
    Person(int age)
    {
        cout << "Person的有参构造函数" << endl;
        m_Age = age;
    }
    /*Person(const Person &p)
    {
        m_Age = p.m_Age;
        cout << "Person的拷贝构造函数" << endl;
    }*/
    ~Person()
    {
        cout << "Person的析构函数" << endl;
    }
    int m_Age;
};

/*void test()
{
    Person p;
    p.m_Age = 18;
    Person p2(p);
    cout << "p2的年龄:" << p2.m_Age << endl;
}*/
void test1()
{
    Person p(20);
    Person p2(p);
    cout << "p2的年龄:" << p2.m_Age << endl;
}
int main()
{
    //test();
    test1();
    return 0;
}

在这里插入图片描述在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;


class Person
{
    public:
    Person()
    {
        cout << "Person的默认构造函数" << endl;
    }
    Person(int age,int height)
    {
        m_Age = age;
        m_Height = new int(height);
        cout << "Person的有参构造函数" << endl;
    }
    Person(const Person &p)
    {
        cout << "Person的拷贝构造函数" << endl;
        m_Age = p.m_Age;
        //m_Height = p.m_Height;  编译器默认实现
        //深拷贝操作
        m_Height = new int(*p.m_Height);


    }

    ~Person()
    {
        //析构代码,将堆区开辟数据做释放操作
        if(m_Height != NULL)
        {
            delete m_Height;
            m_Height = NULL;
        }
        cout << "Person的析构函数" << endl;
    }
    int m_Age;
    int *m_Height;
};

void test()
{
    Person p1(18,180);
    cout << "P1的年龄为:"<< p1.m_Age <<  "身高为:" << *p1.m_Height<<endl;

    Person p2(p1);
    cout << "P2的年龄为:"<< p2.m_Age << "身高为:" << *p2.m_Height <<endl;

}

int main()
{
    test();
    return 0;
}

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;


class Person
{
public:
    //传统初始化
    /*Person(int a,int b,int c)
    {
        m_A = a;
        m_B = b;
        m_C = c;
    }*/
    //初始化列表

    Person(int a,int b,int c):m_A(a),m_B(b),m_C(c)
    {

    }

    int m_A;
    int m_B;
    int m_C;

};

void test()
{
    //Person p(10,20,30);
    Person p(30,20,10);
    cout << p.m_A <<p.m_B << p.m_C << endl;
}

int main()
{
    test();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值