判断题 第 1 题
题目: 构造函数被调用的次数是 1 次。
class Test { public: Test() { cout << "T "; } };
int main() { Test a; Test b = a; }
答案: 错
解析:
代码执行过程如下:
Test a; // 调用构造函数
Test b = a; // 调用拷贝构造函数
一共调用了 2 次构造相关函数。
判断题 第 2 题
题目: 封装:把数据和操作数据的方法放在一起,并隐藏细节。
答案: 对
解析:
封装的核心在于将数据(private)隐藏起来,仅通过公共接口(public)进行操作。
示例:
class BankAccount {
private:
int money; // 外人不能直接碰
public:
void deposit(int x) { money += x; }
};
判断题 第 3 题
题目: 下面代码能正确统计二叉树中叶子结点数量。
int countLeaf(TreeNode* root) {
if (!root) return 0;
if (!root->left && !root->right) return 1;
return countLeaf(root->left) + countLeaf(root->right);
}


