#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person()
{
cout << "moren function" << endl;
}
Person(int Age)
{
cout << "This is calling a argument function!!!" << endl;
age = Age;
}
Person(const Person &p)
{
cout << "kaobei function !!!" << endl;
age = p.age;
}
~Person()
{
cout << "This is destructio function!!!" << endl;
}
int age;
};
void test()
{
Person p1(20);
Person p2(p1);
cout << "The p2's age is " << p2.age << endl;
}
void work(Person p)
{
cout << "test1" << endl;
}
void test1()
{
Person p3;
work(p3);
}
Person dowork()
{
Person person;
cout << (int *)&person << endl;
return person;
}
void test03()
{
Person p = dowork();
cout << "P address:" << (int *)&p << endl;
}
int main()
{
test03();
return 0;
}
```cpp
#include <iostream>
#include <string>
using namespace std;
/*
构造函数调用机制
默认条件下 c++编译器至少给一个类添加三个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝函数,对属性值进行拷贝, 你不写编译器会总动进行赋值
构造函数调用规则:
1.如果用户自己定义了有参构造函数,c++不会提供默认的无参构造,但是会提供默认的拷贝函数
2.如果用户自己定义了拷贝函数,c++不会提供其他构造函数
*/
class Person
{
public:
// 无参构造函数
Person()
{
cout << "wucan gouzaohanshu" << endl;
}
// 有参构造函数
Person(int Age)
{
age = Age;
cout << "There is a argument gouzao function!" << endl;
}
// 拷贝构造函数
Person(const Person &p)
{
age = p.age;
cout << "This is kaobei gouzao function!!" << endl;
}
// 析构函数
~Person()
{
cout << "This is destruction function!!!" << endl;
}
int age;
};
void test1()
{
Person p; // 默认无参构造函数
p.age = 18;
// 显示拷贝构造函数
Person p2(p);
cout << "p2's age is " << p2.age << endl;
}
int main()
{
test1();
return 0;
}