C和C++中struct的区别

C与C++中struct的区别

  1. 在c语言中,不能直接用结构体来声明变量,如果想在c语言中直接用结构体名定义变量,需要用到 typedef

    在c++中,可以直接用结构体来声明变量

    //c语言//
    //声明 
    struct stu
    {
        ...
    };
    //定义 
    struct stu student;
    
    typedef struct stu
    {
        ...
    }Stu;
    //定义
    1.Stu student;
    2.struct stu student;
    
    //c++//
    //声明
    struct stu
    {
        ...
    };
    //定义
    1.struct stu student;
    2.stu student;
    
  2. C语言中没有访问权限的设定,对外不隐藏数据

    C++中有对权限的设定private,public,protected

  3. C语言中的结构体不能继承,C++中可以

posted @ 2020-08-21 14:38  Chilk  阅读(260)  评论(0编辑  收藏  举报