C++面向对象入门(三)class和struct的区别

默认成员||域访问权限不同,class中的成员默认访问权限为私有,struct中的成员默认访问权限为公共

 

代码示例:

#include<iostream>

using namespace std;

/**
 * 类中成员默认访问权限为私有
 */
class C1{
    int a;
};

/**
 * 结构体内属性默认访问权限为公共
 */
struct C2{
    int a;
};

int main() {
    C1 c1;
//    c1.a = 1;
    //error: 'int C1::a' is private
    C2 c2;
    c2.a = 2;
    system("pause");

    return 0;
}

 

posted @ 2020-08-09 11:40  DNoSay  阅读(119)  评论(0编辑  收藏  举报