结构里都是public类型,而class还有private等

解决方案 »

  1.   

    In C++, a structure is the same as a class except that its members are public by default.Look at this:
    struct _MyStru:CObject            // Declare _MyStru structure
    {
       int x;               // Define members x and y
       int y;
    } spot = { 20, 40 };    This is OK.
      

  2.   

    他们的区别是:
    1 CALSS类中对数据进行了封装,其他对象要直接访问其私有数据是不行的,而STRUCT中的数据就可以直接访问。
    2 CLASS有继承的概念,而STRUCT没有
    3 CLASS中的成员函数有虚拟、重载、动态束定等概念,而STRUCT中没有。
    4 ......
      总之,他们的区别基本上就是OOP和结构化程序设计的区别。建议你去看看有关OOP的书。
      

  3.   

    struct 中的成员缺省是 public 的,而
    class  中的成员缺省是 private 的struct 不支持构造/析构,不支持继承...
      

  4.   

    在C++中struct 一样可以继承也支持构造/析构
      

  5.   

    要说的,阿凯都说了,我想,再要强调的是,安全问题,CLASS可以更好地封装和隐蔽数据。
      

  6.   

    在c\c++里好象struct一样可以继承也支持构造/析构的~~
    记不清楚啦...
      

  7.   

    在c++中,struct 和 class根本就是一回事,唯一的区别在于:struct 中的成员缺省是 public 的,而class  中的成员缺省是 private 的,可以这么说:class 是从 struct 发展而来的。之所以将struct和class都保留,是因为:
    1、提出class是为了强调一种概念。
    2、保留struct是为了照顾到大多数人的习惯。
      

  8.   

    jerrycao(jerry)说得非常精确, 举点例子大家才明白吧:typedef struct _SS1 {
    int mBar;
    } SS1, *PSS1;typedef class _CS1 {
    public:
    int mBar;
    } CS1, *PCS1;class CObject {
    public:
    int mRookies;
    };struct SFoo : protected CObject {
    private:
     int mBar;
    public:
     void Fire();
    };看来大家的C++都急需补课 :)
      

  9.   

    jerrycao很对
    c++的struct很强,可不比c了,
    只是
    struct 中的成员缺省是 public 的
    class  中的成员缺省是 private 的
    只要你愿意,不用class...没人会这么做吧:)
      

  10.   

    jerry is right,but I think many of us should improve our lessons
      

  11.   

    区别在于CLASS的缺省声明为private,Struct缺省声明为public