C++类中的权限
在C++中
private成员函数只能在类内使用,是不对外开放的
public成员函数,既能在类内使用,又能在类外使用
private成员只能在类内使用
编译宏使用
#include<iostream>
#include <vector>
#include <algorithm>
#include <istream>
#include <string>
#include <sstream>
#include <set>
#ifdef BNode
struct BNode
{
/* data */
BNode* left_son;
BNode* right_son;
int data;
BNode(int a):data(a),left_son(nullptr),right_son(nullptr)
{
}
};
int find_depth(BNode* bn)
{
if(bn==nullptr/* && bn->right_son==nullptr*/)
{
return 0;
}
// depth++;
int left_depth = find_depth(bn->left_son);
int right_depth = find_depth(bn->right_son);
/* code */

本文介绍了C++中类的权限控制,包括private和public成员的使用,并展示了如何利用编译宏来控制代码块的编译。示例中包含了一个二叉树节点的定义,用于计算树的深度,以及一个查找非7的倍数的函数。同时,还给出了一个名为Base的类,演示了成员函数的权限使用。
最低0.47元/天 解锁文章
2354

被折叠的 条评论
为什么被折叠?



