C++面向對象學習—class和struct區別
class:默認權限是私有 private
類內可以訪問,類外不可訪問。
struct:默認權限是公共 public
結構體內可以訪問,結構體外也可以訪問。
代碼演示
#include<iostream>
#include<string>
using namespace std;
class C1 {
int m_A;//默認權限是私有
};
struct C2
{
int m_A;//默認權限是公有
};
int main() {
//struct和class區別
//struct 默認權限是公共 public
//class 默認權限是私有 private
C1 c1;
//c1.m_A = 100;
C2 c2;
c2.m_A = 100;//在struct默認的權限是公共,因此可以訪問
system("pause");
return 0;
}