#include <iostream>
using namespace std;
class test{
private:
int a;
//放在私有中,有助于保护,通过set和get方法得到其值
public:
test(){
//无参构造函数
a = 10;
}
test(int a){
//有参构造函数
this->a = a;
}
test(const test &obj){
//有参构造函数 完成对象的初始化
cout << "我也是构造函数!"<< endl;
}
~test(){
//结束时自动调用
cout << "我是析构函数!"<< endl;
getchar();
//测试
}
int geta(){
return a;
}
};
int main()
{
test t1(20);
//构造函数自动调用
test t2();
test t3(t1);
cout << t1.geta() << endl;
test t4 = 30;
cout << t4.geta() << endl;
test t5 = test(40);
//手动调用构造函数
cout << t5.geta() << endl;
getchar();
return 0;
}