
一只名叫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;};
本文探讨了C语言与C++中结构体(struct)的区别,重点介绍了如何在C++中使用类(class)来增强结构体的功能,并通过具体示例说明了访问控制权限的运用。
1690

被折叠的 条评论
为什么被折叠?



