#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//分类
//1、按照参数:无参构造、有参构造
//2、按照类型:普通构造、拷贝构造
class Person
{
public:
Person();
Person(int a);
Person(const Person &p);
~Person();
int m_Age;
private:
};
Person::Person()
{
cout << "无参构造" << endl;
}
Person::Person(int a)
{
cout << "有参构造" << endl;
m_Age = a;
}
Person::Person(const Person& p)
{
cout << "拷贝构造" << endl;
m_Age = p.m_Age;
}
Person::~Person()
{
cout << "析构调用" << endl;
}
void test01()
{
/*构造函数调用方式*/
//1 默认调用
Person P1;
//2 括号调用
Person P2(1);
cout << "P3的年龄" << P2.m_Age << endl;
Person P3(P2);
cout << "P3的年龄" << P3.m_Age << endl;
//3 显式法调用
Person P4 = Person(100);
Person P5 = Person(P4);
//4 隐式类型转换
Person P6 = 100;//相当于 Person P6 = Person(100);
Person P7 = P6;//相当于 Person P7 = Person(P6);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
C++基础学习DAY3-04 构造函数的分类和调用
最新推荐文章于 2024-06-29 21:35:37 发布