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

后端 未结 30 3052
盖世英雄少女心
盖世英雄少女心 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条回答
  • 2020-11-21 05:46

    Out of all these factors,it can be concluded that concept Class is highly suitable to represent real world objects rather than "Structures".Largely because OOP concepts used in class are highly practical in explaining real world scenarios therefore easier to merge them to reality.For an example,default inheritance is public for structs but if we apply this rule for real world,it's ridiculous.But in a class default inheritance is private which is more realistic.

    Anyways,what i need to justify is Class is a much broader,real world applicable concept whereas Structure is a primitive Concept with poor internal organization(Eventhough struct follows OOP concepts,they have a poor meaning)

    0 讨论(0)
  • 2020-11-21 05:48

    The difference between struct and class keywords in C++ is that, when there is no specific specifier on particular composite data type then by default struct or union is the public keywords that merely considers data hiding but class is the private keyword that considers the hiding of program codes or data. Always some programmers use struct for data and class for code sake. For more information contact other sources.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:51

    Classes are Reference types and Structures are Values types.
    When I say Classes are reference types,
    basically they will contain the address of an instance variables.

    For example:

    Class MyClass
    {
        Public Int DataMember;  //By default, accessibility of class data members 
                                //will be private. So I am making it as Public which 
                                //can be accessed outside of the class.
    }
    

    In main method,
    I can create an instance of this class using new operator that allocates memory for this class
    and stores the base address of that into MyClass type variable(_myClassObject2).

    Static Public void Main (string[] arg)
    {
        MyClass _myClassObject1 = new MyClass();
        _myClassObject1.DataMember = 10;
    
        MyClass _myClassObject2 = _myClassObject1;
        _myClassObject2.DataMember=20;
    }
    

    In the above program, MyClass _myClassObject2 = _myClassObject1; instruction indicates that both variables of type MyClass

    1. myClassObject1
    2. myClassObject2

    and will point to the same memory location.
    It basically assigns the same memory location into another variable of same type.

    So if any changes that we make in any one of the objects type MyClass will have an effect on another
    since both are pointing to the same memory location.

    "_myClassObject1.DataMember = 10;" at this line both the object’s data members will contain the value of 10.
    "_myClassObject2.DataMember = 20;" at this line both the object’s data member will contains the value of 20.
    Eventually, we are accessing datamembers of an object through pointers.

    Unlike classes, structures are value types. For example:

    Structure MyStructure
    {
        Public Int DataMember;  //By default, accessibility of Structure data 
                                //members will be private. So I am making it as 
                                //Public which can be accessed out side of the structure.
    }
    
    Static Public void Main (string[] arg)
    {
        MyStructure _myStructObject1 = new MyStructure();
        _myStructObject1.DataMember = 10;
    
        MyStructure _myStructObject2 = _myStructObject1;
        _myStructObject2.DataMember = 20;
    }
    

    In the above program,
    instantiating the object of MyStructure type using new operator and
    storing address into _myStructObject variable of type MyStructure and
    assigning value 10 to data member of the structure using "_myStructObject1.DataMember = 10".

    In the next line,
    I am declaring another variable _myStructObject2 of type MyStructure and assigning _myStructObject1 into that.
    Here .NET C# compiler creates another copy of _myStructureObject1 object and
    assigns that memory location into MyStructure variable _myStructObject2.

    So whatever change we make on _myStructObject1 will never have an effect on another variable _myStructObject2 of type MyStructrue.
    That’s why we are saying Structures are value types.

    So the immediate Base class for class is Object and immediate Base class for Structure is ValueType which inherits from Object.
    Classes will support an Inheritance whereas Structures won’t.

    How are we saying that?
    And what is the reason behind that?
    The answer is Classes.

    It can be abstract, sealed, static, and partial and can’t be Private, Protected and protected internal.

    0 讨论(0)
  • 2020-11-21 05:53

    It's just a convention. Structs can be created to hold simple data but later evolve time with the addition of member functions and constructors. On the other hand it's unusual to see anything other than public: access in a struct.

    0 讨论(0)
  • 2020-11-21 05:53

    The main difference between struct and class is that in struct you can only declare data variables of different data types while in class you can declare data variables,member functions and thus you can manipulate data variables through functions.

    -> another handy thing that i find in class vs struct is that while implementing files in a program if you want to make some operations of a struct again and again on every new set of operations you need to make a separate function and you need to pass object of struct after reading it from the file so as to make some operations on it . while in class if you make a function that does some operations on the data needed everytime..its easy you just have to read object from file and call the function..

    But it depennds on the programmer which way he/she finds suitable...according to me i prefer class everytime just because it supports OOPs and thats the reason it is implemented in almost every languages and its the wonderful feature of all time programming ;-)

    And yeah the most unforgotten difference i forgot to mention is that class supports data hiding and also supports operations that are performed on built in data types while struct doesnt !

    0 讨论(0)
提交回复
热议问题