class 默认权限为私有
struct 默认权限为公共
示例代码:
#include <iostream>
using namespace std;
#include <string>
class Student
{
string name;
};
struct Teacher
{
string name;
};
int main()
{
Student s1;
// s1.name = "godv"; 报错访问不到
Teacher t1;
t1.name = "godv";
return 0;
}
本文通过示例代码对比了C++中class与struct的默认权限差异:class默认权限为私有,而struct默认权限为公共。通过具体实例展示了这两种类型在访问控制上的不同表现。

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



