C++类注意事项
构造函数
当构造函数不包含参数时,不能直接使用普通的定义方式。
#include <iostream>
using namespace std;
class exam
{
public:
exam() :x(20){}
exam(int n) :x(n){}
int get_x()
{
return x;
}
private:
int x;
};
int main()
{
exam e1 = exam();
exam e2(12);
exam e3(78);
cout << e1.get_x() << endl;
cout << e2.get_x() << endl;
cout << e3.get_x() << endl;
return 0;
}