What are the differences between struct and class in C++?

后端 未结 30 3210
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:38

This question was already asked in the context of C#/.Net.

Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical d

30条回答
  •  -上瘾入骨i
    2020-11-21 05:50

    1. The members of a structure are public by default, the members of class are private by default.
    2. Default inheritance for Structure from another structure or class is public.Default inheritance for class from another structure or class is private.
    class A{    
    public:    
        int i;      
    };
    
    class A2:A{    
    };
    
    struct A3:A{    
    };
    
    
    struct abc{    
        int i;
    };
    
    struct abc2:abc{    
    };
    
    class abc3:abc{
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {    
        abc2 objabc;
        objabc.i = 10;
    
        A3 ob;
        ob.i = 10;
    
        //A2 obja; //privately inherited
        //obja.i = 10;
    
        //abc3 obss;
        //obss.i = 10;
    }
    

    This is on VS2005.

提交回复
热议问题