class在c语言中,C语言中的class的应用

C语言中的class的应用

答案:6  信息版本:手机版

解决时间 2019-10-04 20:09

已解决

2019-10-04 16:32

C语言中的class的应用

最佳答案

2019-10-04 17:57

1、C语言里没有class函数的概念,class是C++中的关键字。

2、C++是基于C的一种面向对象扩展,它在C原有结构体(struct)的基础上,扩充了struct的功能(增加了成员函数,以及访问控制,继承等),并增加了class这一新定义。实际上class和struct的唯一区别就是:struct中的默认访问控制权限是public,而class的默认访问控制权限是private。

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 right

int Bottom(); // get bottom

int Left(); // get left

int Top(); // get top

};

为了隐藏结构体内的成员,添加访问控制标识:

struct RecTangle{

private:

int width; int height;

int pos_x; int pos_y;

public:

int Right(); // get right

int Bottom(); // get bottom

int Left(); // get left

int Top(); // get top

};

如果用class来代替struct,则需要添加访问控制标识.

比如用class来定义类C结构体

class RecTangle{

public:

int width; int height;

int pos_x; int pos_y;

};

全部回答

1楼

2019-10-04 22:54

在一个文件中定义结构体,数据成员直接声明,方法成员声明一个函数指针,然后在结构外面定义这些方法成员就行了

2楼

2019-10-04 22:31

很想回答你但是我确实不知道怎么回答

3楼

2019-10-04 21:34

差不多。class除了能定义变量,还能定义函数。

内部变量是私有时,只有内部函数能操作

4楼

2019-10-04 20:22

你说的class是类的吧,在c语言中是没有这个概念的,我指的是编程,class是c++中引入的,与c语言中的结构体有类似的地方,但是又有很大的不同,class中可以定义成员变量还可以定义方法也就是函数(c中概念),但是结构体却不能定义方法,这是一大不同,另外就是类class定义对象,结构体是定义的值,两个是不同的定义,大的方向你要搞清楚class是面向对象编程中的概念,c语言中的结构体是面向过程编程中应用的,各自的用法都是用来编写程序,class的定义以及用法,你可以参考下c++相关基础会清楚些,

5楼

2019-10-04 19:31

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 right

int Bottom(); // get bottom

int Left(); // get left

int Top(); // get top

};

为了隐藏结构体内的成员,添加访问控制标识:

struct RecTangle{

private:

int width; int height;

int pos_x; int pos_y;

public:

int Right(); // get right

int Bottom(); // get bottom

int Left(); // get left

int Top(); // get top

};

如果用class来代替struct,则需要添加访问控制标识.

比如用class来定义类C结构体

class RecTangle{

public:

int width; int height;

int pos_x; int pos_y;

};

我要举报

如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

大家都在看

推荐资讯

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值