1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include<iostream> 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 test01() { Person p1(10); Person p2(p1); cout << "p2 的年龄为:" << p2.age << endl; }
void dowork(Person p) {
} void test02() { Person p; dowork(p); }
Person dowork2() { Person p1; cout << (int)&p1 << endl; return p1; } void test03() { Person p = dowork2(); cout << (int)&p << endl; } int main() { cout << "test01" << endl; test01(); cout << "\ntest02" << endl; test02(); cout << "\ntest03" << endl; test03(); return 0; }
|