C++ STRUCT 关键词

黎 浩然/ 17 5 月, 2022/ C/C++, 计算机/COMPUTER/ 0 comments

  • C语言的struct只能有成员变量,C++的struct可以有方法以及访问(public protected private)修饰符
  • C语言的结构体类型必须用struct关键字和struct类型名一起实例化,即struct typename variance
  • C语言没有继承等面向对象的概念
  • C语言中若结构体的类型名与函数名相同,可以正常运行!
#include<stdio.h>
struct Base {
    int v1;
    int v2;
    int v3; 
};

void Base(){
    printf("%s\\n","I am Base func");
}

int main() {
    struct Base base; // 必须用struct关键字和struct类型名一起实例化 
    base.v1=1;
    printf("%d\\n",base.v1);
    Base();
    return 0;
}
  • C++中若结构体的类型名与函数名相同,可以正常运行!但是实例化结构体类型时候必须带struct
  • C++中,struct 作为数据结构的实现体,它默认的数据访问控制是 public 的,而 class作为对象的实现体,它默认的成员变量访问控制是 private 的
Share this Post

Leave a Comment

您的邮箱地址不会被公开。 必填项已用 * 标注

*
*