C语言的struct定义了一组变量的集合,C编译器并不认为这是一种新的类型。

C++中的struct是一个新类型的定义声明。

struct Student

{

char name[100];

int age;

};

void main()

{

Student s1={"wang",1};

Student s2={"wang",2};

}

上面程序我们用.c文件,编译报错。

这个时候c编译器不认为Student是一种新的类型,我们必须在Student前面加上struct关键字!

struct Student

{

char name[100];

int age;

};

void main()

{

struct Student s1={"wang",1};

struct Student s2={"wang",2};

}

C++对struct关键字进行了功能增强。

我们将同样的在c编译器下无法编译的程序放到.cpp文件中,发现是可以编译通过的!也就是说在C++中认为struct定义了一个新的类型,这个新的类型可以来定义新的变量。

#include<iostream>

using namespace std;

struct Student

{

char name[100];

int age;

};

void main()

{

Student s1={"wang",1};

Student s2={"wang",2};

system("pause");

}

另外呢,C++不单对struct关键字进行了类型增强,struct关键字和class关键字完成的功能是一样的,当然也有不一样的地方,区别后面再说。

在结构体里面也可以加上访问数据权限:public、protected等。

#include<iostream>

using namespace std;

struct Student

{

public:

char name[100];

int age;

private:

int a;

};

void main()

{

struct Student s1

system("pause");

}

长按解锁

解锁更多精彩内幕

依法编程

微信:Lightspeed-Tech

技术驱动生活

最新文章

  1. 解决:ERROR: Cannot launch Jack server
  2. C# 编程实现非自相交多边形质心
  3. vue隐藏APP启动时显示的{{}}
  4. 了解Sql Server的执行计划
  5. MongoDB学习笔记四:索引
  6. ADO.NET完整增删改
  7. [C/C++]C/C++相关网站
  8. LeapMotion(2):追踪五指
  9. 1218. Episode N-th: The Jedi Tournament(bfs)
  10. firefox浏览器删除插件
  11. java_Collection 类集
  12. PHP session 跨子域问题总结
  13. iOS开发面试题整理 (三)
  14. ALEX WANG(王培沂)品牌_时尚频道_凤凰网
  15. Linux 下安装 SVN服务器
  16. GWAS基因芯片数据预处理:质量控制(quality control)
  17. MapReduce论文学习
  18. wps 批量调整图片大小 宏
  19. [ACM_数据结构] 线段树模板
  20. javascript 方法总结(Array篇)

热门文章

  1. 关于HTML
  2. python/Django(增、删、改、查)操作
  3. Tumblr:我们是如何从 PHP 5 升级到 PHP 7 的
  4. 老男孩python学习之作业二---三级菜单
  5. spark2.1:flatMap的用法
  6. 分析ajax请求抓取今日头条关键字美图
  7. ConcurrentHashMap基于JDK1.8源码剖析
  8. 玩转Ecs服务器之搭建Ftp
  9. 使用YOLOv2进行图像检测
  10. [LeetCode] Reverse String II 翻转字符串之二