c语言class作用,C语言中的class的应用?

本文探讨了C语言与C++中结构体(struct)的区别,重点介绍了如何在C++中使用类(class)来增强结构体的功能,并通过具体示例说明了访问控制权限的运用。

f9d4a91cf6540f9068c3cf937efae9b7.png

一只名叫tom的猫

C语言里没有class的概念,你可以写int class = 0;而不会有任何报错。C++是基于C的一种面向对象扩展,它在C原有结构体(struct)的基础上,扩充了struct的功能(增加了成员函数,以及访问控制,继承等),并增加了class这一新定义。实际上class和struct的唯一区别就是:struct中的默认访问控制权限是public,而class的默认访问控制权限是private。你可以定义一个类C的结构体struct RecTangle{int width; int height;int pos_x; int pos_y;};给他添加一些成员函数struct RecTangle{int width; int height;int pos_x; int pos_y;int Right(); // get rightint Bottom(); // get bottomint Left(); // get leftint Top(); // get top};为了隐藏结构体内的成员,添加访问控制标识:struct RecTangle{private:int width; int height;int pos_x; int pos_y;public:int Right(); // get rightint Bottom(); // get bottomint Left(); // get leftint Top(); // get top};如果用class来代替struct,则需要添加访问控制标识.比如用class来定义类C结构体class RecTangle{public:int width; int height;int pos_x; int pos_y;};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值